Files
orca/src/preload/browser-find-subscriptions.test.ts
T
OrcaWinandOrcaWin 0efc842028 [comment audit] Shorten preload comments (#32257)
* chore: audit preload root comments and track repository coverage

* docs: track repository comment audit coverage

* chore: keep comment audit tracker local

* chore: shorten preload API contract comments

---------

Co-authored-by: OrcaWin <293788423+OrcaWin@users.noreply.github.com>
2026-10-23 02:16:59 -07:00

111 lines
4.0 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest'
import { createBrowserFindSubscriptions } from './browser-find-subscriptions'
const FIRST_SOURCE = {
browserPageId: 'page-1',
browserWorkspaceId: 'workspace-1'
}
const SAME_WORKSPACE_SOURCE = {
browserPageId: 'page-2',
browserWorkspaceId: FIRST_SOURCE.browserWorkspaceId
}
const SAME_PAGE_SOURCE = {
browserPageId: FIRST_SOURCE.browserPageId,
browserWorkspaceId: 'workspace-2'
}
describe('browser Find subscriptions', () => {
it('dispatches only to the exact browser page and workspace owner', () => {
const subscriptions = createBrowserFindSubscriptions()
const firstCallback = vi.fn()
const sameWorkspaceCallback = vi.fn()
const samePageCallback = vi.fn()
subscriptions.subscribe(FIRST_SOURCE, firstCallback)
subscriptions.subscribe(SAME_WORKSPACE_SOURCE, sameWorkspaceCallback)
subscriptions.subscribe(SAME_PAGE_SOURCE, samePageCallback)
subscriptions.dispatch(FIRST_SOURCE)
expect(firstCallback).toHaveBeenCalledOnce()
expect(sameWorkspaceCallback).not.toHaveBeenCalled()
expect(samePageCallback).not.toHaveBeenCalled()
})
it('dispatches a page-only target to every workspace holding that page', () => {
const subscriptions = createBrowserFindSubscriptions()
const firstCallback = vi.fn()
const sameWorkspaceCallback = vi.fn()
const samePageCallback = vi.fn()
subscriptions.subscribe(FIRST_SOURCE, firstCallback)
subscriptions.subscribe(SAME_WORKSPACE_SOURCE, sameWorkspaceCallback)
subscriptions.subscribe(SAME_PAGE_SOURCE, samePageCallback)
subscriptions.dispatch({ browserPageId: FIRST_SOURCE.browserPageId })
expect(firstCallback).toHaveBeenCalledOnce()
expect(samePageCallback).toHaveBeenCalledOnce()
expect(sameWorkspaceCallback).not.toHaveBeenCalled()
})
it('dispatches an explicitly undefined workspace as page-only, not as a miss', () => {
const subscriptions = createBrowserFindSubscriptions()
const callback = vi.fn()
subscriptions.subscribe(FIRST_SOURCE, callback)
// Structured-clone IPC preserves explicitly undefined properties.
subscriptions.dispatch({
browserPageId: FIRST_SOURCE.browserPageId,
browserWorkspaceId: undefined
})
expect(callback).toHaveBeenCalledOnce()
})
it('rejects malformed identities and blank ids', () => {
const subscriptions = createBrowserFindSubscriptions()
const callback = vi.fn()
subscriptions.subscribe(FIRST_SOURCE, callback)
subscriptions.dispatch(undefined)
subscriptions.dispatch(['page-1'])
subscriptions.dispatch({ browserWorkspaceId: FIRST_SOURCE.browserWorkspaceId })
subscriptions.dispatch({ browserPageId: '' })
subscriptions.dispatch({
browserPageId: FIRST_SOURCE.browserPageId,
browserWorkspaceId: ''
})
expect(callback).not.toHaveBeenCalled()
})
it('drops empty ownership entries when subscribers clean up', () => {
const subscriptions = createBrowserFindSubscriptions()
const callback = vi.fn()
const unsubscribe = subscriptions.subscribe(FIRST_SOURCE, callback)
expect(subscriptions.subscribedPageCount()).toBe(1)
unsubscribe()
subscriptions.dispatch(FIRST_SOURCE)
subscriptions.dispatch({ browserPageId: FIRST_SOURCE.browserPageId })
expect(callback).not.toHaveBeenCalled()
expect(subscriptions.subscribedPageCount()).toBe(0)
})
it('keeps the other workspaces on a page when one of them unsubscribes', () => {
const subscriptions = createBrowserFindSubscriptions()
const firstCallback = vi.fn()
const samePageCallback = vi.fn()
const unsubscribe = subscriptions.subscribe(FIRST_SOURCE, firstCallback)
subscriptions.subscribe(SAME_PAGE_SOURCE, samePageCallback)
unsubscribe()
subscriptions.dispatch(SAME_PAGE_SOURCE)
subscriptions.dispatch({ browserPageId: FIRST_SOURCE.browserPageId })
expect(firstCallback).not.toHaveBeenCalled()
expect(samePageCallback).toHaveBeenCalledTimes(2)
expect(subscriptions.subscribedPageCount()).toBe(1)
})
})