From c450e830de6ebd7a3944accc0ba0eadcf68c14a9 Mon Sep 17 00:00:00 2001 From: Tunglies <77394545+Tunglies@users.noreply.github.com> Date: Wed, 11 Nov 2026 07:26:44 +0800 Subject: [PATCH] remove useless test --- .../connection-relative-time.test.ts | 40 ---------- .../connection/connection-table.test.ts | 75 ------------------ src/hooks/use-connection-data.test.ts | 79 ------------------- 3 files changed, 194 deletions(-) delete mode 100644 src/components/connection/connection-relative-time.test.ts delete mode 100644 src/components/connection/connection-table.test.ts delete mode 100644 src/hooks/use-connection-data.test.ts diff --git a/src/components/connection/connection-relative-time.test.ts b/src/components/connection/connection-relative-time.test.ts deleted file mode 100644 index 2b24ce123..000000000 --- a/src/components/connection/connection-relative-time.test.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { useSyncExternalStore } from 'react' -import { afterEach, expect, it, vi } from 'vitest' - -vi.mock('dayjs', () => ({ default: () => ({ from: () => '' }) })) -vi.mock('react', () => ({ - memo: (component: unknown) => component, - useSyncExternalStore: vi.fn((_subscribe, getSnapshot) => getSnapshot()), -})) - -afterEach(() => { - vi.useRealTimers() - vi.unstubAllGlobals() -}) - -it('keeps at most one interval while visible and none while hidden or unsubscribed', async () => { - vi.useFakeTimers() - vi.stubGlobal('window', globalThis) - const visibility = Object.assign(new EventTarget(), { hidden: true }) - vi.stubGlobal('document', visibility) - const { RelativeTime } = await import('./connection-relative-time') - RelativeTime({ start: '' }) - const [subscribe] = vi.mocked(useSyncExternalStore).mock.calls[0] - const unsubscribeFirst = subscribe(() => {}) - const unsubscribeSecond = subscribe(() => {}) - expect(vi.getTimerCount()).toBe(0) - visibility.hidden = false - visibility.dispatchEvent(new Event('visibilitychange')) - expect(vi.getTimerCount()).toBe(1) - visibility.hidden = true - visibility.dispatchEvent(new Event('visibilitychange')) - expect(vi.getTimerCount()).toBe(0) - visibility.hidden = false - visibility.dispatchEvent(new Event('visibilitychange')) - expect(vi.getTimerCount()).toBe(1) - unsubscribeFirst() - unsubscribeSecond() - expect(vi.getTimerCount()).toBe(0) - visibility.dispatchEvent(new Event('visibilitychange')) - expect(vi.getTimerCount()).toBe(0) -}) diff --git a/src/components/connection/connection-table.test.ts b/src/components/connection/connection-table.test.ts deleted file mode 100644 index 0cffbfd42..000000000 --- a/src/components/connection/connection-table.test.ts +++ /dev/null @@ -1,75 +0,0 @@ -import { createElement } from 'react' -import { renderToStaticMarkup } from 'react-dom/server' -import { expect, it, vi } from 'vitest' - -import { formatConnectionTraffic } from './connection-row-view' -import { ConnectionTable } from './connection-table' - -const sorting = vi.hoisted(() => ({ id: 'download', desc: false })) - -vi.mock('react', async (importOriginal) => ({ - ...(await importOriginal()), - useState: (initial: unknown) => [ - initial === null ? sorting : initial, - vi.fn(), - ], -})) -vi.mock('foxact/use-local-storage', () => ({ - useLocalStorage: (_key: string, initial: unknown) => [initial, vi.fn()], -})) -vi.mock('react-i18next', () => ({ - useTranslation: () => ({ t: (key: string) => key }), -})) -vi.mock('./connection-column-manager', () => ({ - ConnectionColumnManager: () => null, -})) -vi.mock('./connection-relative-time', () => ({ - RelativeTime: () => null, -})) -vi.mock('./connection-row-view', async (importOriginal) => { - const original = - await importOriginal() - return { - ...original, - formatConnectionTraffic: vi.fn(original.formatConnectionTraffic), - } -}) - -it('formats only virtual rows when sorting numeric columns above the cache bound', () => { - const connections: IConnectionsItem[] = Array.from( - { length: 2_101 }, - (_, i) => ({ - id: `${i}`, - metadata: { - network: 'tcp', - type: 'HTTP', - host: `host-${i}`, - sourceIP: '', - sourcePort: '', - destinationPort: '443', - }, - chains: [], - rule: '', - rulePayload: '', - start: '', - upload: (i * 3) % 2_101, - download: (i * 5) % 2_101, - curUpload: (i * 7) % 2_101, - curDownload: (i * 11) % 2_101, - }), - ) - - for (const column of ['download', 'upload', 'dlSpeed', 'ulSpeed']) { - sorting.id = column - vi.mocked(formatConnectionTraffic).mockClear() - renderToStaticMarkup( - createElement(ConnectionTable, { - connections, - onShowDetail: vi.fn(), - columnManagerOpen: false, - onCloseColumnManager: vi.fn(), - }), - ) - expect(formatConnectionTraffic).toHaveBeenCalledTimes(24) - } -}) diff --git a/src/hooks/use-connection-data.test.ts b/src/hooks/use-connection-data.test.ts deleted file mode 100644 index ad39b6c0f..000000000 --- a/src/hooks/use-connection-data.test.ts +++ /dev/null @@ -1,79 +0,0 @@ -import { useSyncExternalStore } from 'react' -import { afterEach, expect, it, vi } from 'vitest' - -const socket = vi.hoisted(() => ({ - addListener: - vi.fn< - (listener: (message: { type: string; data: string }) => void) => void - >(), - close: async () => {}, -})) - -vi.mock('react', () => ({ - useCallback: (callback: unknown) => callback, - useMemo: (factory: () => unknown) => factory(), - useSyncExternalStore: vi.fn((_subscribe, getSnapshot) => getSnapshot()), -})) -vi.mock('tauri-plugin-mihomo-api', () => ({ - MihomoWebSocket: { connect_connections: async () => socket }, -})) - -afterEach(() => { - vi.useRealTimers() - vi.unstubAllGlobals() -}) - -it('reuses unchanged snapshot and list references without notifying subscribers', async () => { - vi.useFakeTimers() - vi.setSystemTime(10_000) - vi.stubGlobal('window', globalThis) - const { useConnectionData: readConnectionData } = await import( - './use-connection-data' - ) - readConnectionData() - const [subscribe, read] = vi - .mocked( - useSyncExternalStore< - ReturnType['response']['data'] - >, - ) - .mock.calls.at(-1)! - const listener = vi.fn() - const unsubscribe = subscribe(listener) - await Promise.resolve() - const onMessage = socket.addListener.mock.calls[0][0] - const connections: IConnectionsItem[] = [ - { - id: 'a', - metadata: { - network: 'tcp', - type: 'HTTP', - host: 'example.com', - sourceIP: '', - sourcePort: '', - destinationPort: '443', - }, - chains: [], - rule: '', - rulePayload: '', - start: '', - upload: 0, - download: 0, - }, - ] - const send = (uploadTotal = 0) => { - vi.advanceTimersByTime(500) - onMessage({ - type: 'Text', - data: JSON.stringify({ connections, uploadTotal, downloadTotal: 0 }), - }) - return read() - } - const first = send() - expect(send()).toBe(first) - expect(listener).toHaveBeenCalledTimes(1) - const totalsOnly = send(100) - expect(totalsOnly.activeConnections).toBe(first.activeConnections) - expect(totalsOnly.closedConnections).toBe(first.closedConnections) - unsubscribe() -})