Files
orca/src/main/persistence-clipboard-selection-migration.test.ts
Neil 9367169888 refactor(tests): split every oversized test file off the max-lines suppression list (#14728)
* refactor(tests): split oversized test files off the max-lines suppression list

Every `*.test.ts`/`*.spec.ts` that carried an `eslint/oxlint-disable max-lines`
directive is now split into focused, behavior-scoped suites that fit the 800-line
test budget, with shared setup extracted into co-located `*-test-harness.ts` /
`*-test-fixtures.ts` modules (300-line budget). 83 files became ~930; the largest
output is 797 effective lines. `orca-runtime.test.ts` is intentionally untouched.

Test bodies were moved by scripted line-range slicing rather than retyped, so
assertions are byte-identical. The only permitted body edits were mechanical
rebinding where a shared value moved into a harness (e.g. `tmpHome` ->
`homes.tmpHome`).

Registries that enumerate test files were updated in lockstep:
- config/max-lines-baseline.txt: pruned 341 -> 258 entries (all 83 removed).
- config/reliability-gates.jsonc: 33 gates repointed at the split files, with
  assertionRefs split per file where a gate's coverage now spans several.
- .github/workflows/pr.yml: the real-zsh lane now lists the 4 split files that
  actually exercise zsh, so they keep running in the dedicated shell lane.

Also renamed agent-hooks `server-test-fixtures.ts` to `server.test-fixtures.ts`
so the global-fetch call-site audit keeps skipping it, and added `.js` extensions
to the CLI suites' dynamic harness imports (node16 resolution) to unbreak
`build:cli`.

Verification: full suite 52,449 passing vs 52,448 at baseline with zero
assertions lost; `pnpm lint`, `pnpm typecheck`, and `pnpm build:cli` all exit 0;
the terminal-pane e2e spec runs 31/31 headless.

* refactor(tests): split hook-idle arbitration suite that oxfmt pushed over budget

The pre-commit oxfmt pass reflowed pty-connection-hook-idle-arbitration.test.ts
to 811 effective lines, 11 over the test budget. Split the hook-completion side
effect and replacement-agent veto cases into their own suite; both files now sit
well under the cap and the 15 tests are unchanged.

* test: port upstream test changes into the split files after rebase

Rebasing onto main surfaced 27 tests that main had added to files this branch
deleted, plus edits to tests that had already moved. Taking the deletion side of
those modify/delete conflicts would have dropped that coverage silently, so each
upstream change is ported into the split file that now owns the behavior — for
example main's six orchestration mailbox tests land across orchestration-runs,
-send, and -check.

Also repoints `orchestration.notification-mailbox-consistency`, a gate main added
after this branch's gate remap, at those same three split files, and re-prunes
the max-lines baseline against main's (257 entries).

Verified: all 27 upstream test titles present; full suite 52,761 passing with the
only diff vs baseline being 12 tests main itself removed and 3 that moved from
skipped to passing; lint and typecheck exit 0.

* fix(test): flush pending continuations before tearing down terminal test globals

CI shard 5/16 failed on both Node 24 and 26 with `ReferenceError: window is not
defined` from pty-connection.ts, surfacing through
pty-connection-daemon-snapshot-replay.test.ts.

The reattach/settle chains `await` a real promise and then touch `window.api`.
Under fake timers those continuations cannot run, so they only become schedulable
once restoreTerminalTestGlobals() switches back to real timers — which previously
happened immediately before `delete globalThis.window`, so a late continuation
threw and failed the whole file. Flush async ticks in that window instead.

This is latent in the source rather than new: the pre-split 25k-line file kept
running other tests after these, which gave the chains time to settle before
teardown. Splitting the file moved teardown directly behind them.

* fix(test): keep an inert window after terminal test teardown instead of deleting it

The async-tick flush was not enough: the reattach/settle chain can resolve after
teardown regardless of how long we drain, so CI shard 5/16 still failed with
`ReferenceError: window is not defined` from pty-connection.ts.

A real renderer never loses `window`, so deleting it was the artificial part.
Swap in an inert proxy whose properties resolve to callables and whose calls
resolve to undefined, making a late `window.api.pty.*` call a harmless no-op.
The next test replaces it wholesale via installTerminalTestGlobals(), and no test
asserts that `window` is absent.
2026-08-15 00:54:20 -07:00

353 lines
12 KiB
TypeScript

import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import { rmSync, mkdtempSync } from 'node:fs'
import { join } from 'node:path'
import { tmpdir } from 'node:os'
import {
testState,
createStore,
withPlatform,
writeDataFile,
readDataFile
} from './persistence-test-harness'
// Stub the ~/.ssh/config parser so the SSH-import test drives the real Store with deterministic hosts, not the operator's actual ~/.ssh/config.
const { loadUserSshConfigMock, sshConfigHostsToTargetsMock } = vi.hoisted(() => ({
loadUserSshConfigMock: vi.fn(),
sshConfigHostsToTargetsMock: vi.fn()
}))
vi.mock('./ssh/ssh-config-parser', () => ({
loadUserSshConfig: loadUserSshConfigMock,
sshConfigHostsToTargets: sshConfigHostsToTargetsMock
}))
const { trackMock, getCohortAtEmitMock } = vi.hoisted(() => ({
trackMock: vi.fn(),
getCohortAtEmitMock: vi.fn()
}))
vi.mock('electron', () => ({
app: {
getPath: () => testState.dir
},
safeStorage: {
isEncryptionAvailable: () => true,
encryptString: (plaintext: string) => Buffer.from(`encrypted:${plaintext}`, 'utf-8'),
decryptString: (ciphertext: Buffer) => {
const decoded = ciphertext.toString('utf-8')
if (!decoded.startsWith('encrypted:')) {
throw new Error('invalid ciphertext')
}
return decoded.slice('encrypted:'.length)
}
}
}))
vi.mock('./telemetry/client', () => ({
track: trackMock
}))
vi.mock('./telemetry/cohort-classifier', () => ({
getCohortAtEmit: getCohortAtEmitMock
}))
describe('Store', () => {
beforeEach(() => {
testState.dir = mkdtempSync(join(tmpdir(), 'orca-test-'))
trackMock.mockReset()
getCohortAtEmitMock.mockReset()
getCohortAtEmitMock.mockReturnValue({ nth_repo_added: 2 })
})
afterEach(() => {
rmSync(testState.dir, { recursive: true, force: true })
})
it('migrates the legacy floating terminal disabled default to enabled', async () => {
writeDataFile({
schemaVersion: 1,
repos: [],
worktreeMeta: {},
settings: { floatingTerminalEnabled: false },
ui: {},
githubCache: { pr: {}, issue: {} },
workspaceSession: {}
})
const store = await createStore()
expect(store.getSettings().floatingTerminalEnabled).toBe(true)
expect(store.getSettings().floatingTerminalDefaultedForAllUsers).toBe(true)
})
it('preserves a post-migration floating terminal opt-out', async () => {
writeDataFile({
schemaVersion: 1,
repos: [],
worktreeMeta: {},
settings: {
floatingTerminalEnabled: false,
floatingTerminalDefaultedForAllUsers: true
},
ui: {},
githubCache: { pr: {}, issue: {} },
workspaceSession: {}
})
const store = await createStore()
expect(store.getSettings().floatingTerminalEnabled).toBe(false)
expect(store.getSettings().floatingTerminalDefaultedForAllUsers).toBe(true)
})
it('migrates the legacy OSC 52 clipboard disabled default to enabled', async () => {
// Why this migration exists: the old off default was persisted for every
// profile, so without the one-shot flip #10567 would only fix new installs.
writeDataFile({
schemaVersion: 1,
repos: [],
worktreeMeta: {},
settings: { terminalAllowOsc52Clipboard: false },
ui: {},
githubCache: { pr: {}, issue: {} },
workspaceSession: {}
})
const store = await createStore()
expect(store.getSettings().terminalAllowOsc52Clipboard).toBe(true)
expect(store.getSettings().terminalAllowOsc52ClipboardDefaultedOnForAllUsers).toBe(true)
})
it('persists the OSC 52 clipboard migration stamp back to disk', async () => {
// Why round-trip a store first: on a bare legacy profile ~30 other migrations also
// set loadNeedsSave, so the save happens regardless and this migration's own dirty
// flag goes untested. Re-loading a profile the new build already wrote leaves OSC 52
// as the only unmigrated key — which is exactly the upgrade case that matters.
// Why no flush(): flush() writes unconditionally, so only the debounced load-path
// save proves this migration marked the state dirty by itself.
writeDataFile({
schemaVersion: 1,
repos: [],
worktreeMeta: {},
settings: {},
ui: {},
githubCache: { pr: {}, issue: {} },
workspaceSession: {}
})
const migrated = await createStore()
migrated.flush()
const settled = readDataFile() as { settings: Record<string, unknown> }
settled.settings.terminalAllowOsc52Clipboard = false
delete settled.settings.terminalAllowOsc52ClipboardDefaultedOnForAllUsers
writeDataFile(settled)
vi.useFakeTimers()
try {
const store = await createStore()
// Why over-advance: the debounce is exactly 1000ms, so an exact-fit advance turns a
// future debounce raise into a confusing no-write instead of a loud failure.
vi.advanceTimersByTime(5000)
await store.waitForPendingWrite()
} finally {
vi.useRealTimers()
}
const persisted = readDataFile() as {
settings?: {
terminalAllowOsc52Clipboard?: boolean
terminalAllowOsc52ClipboardDefaultedOnForAllUsers?: boolean
}
}
expect(persisted.settings?.terminalAllowOsc52Clipboard).toBe(true)
expect(persisted.settings?.terminalAllowOsc52ClipboardDefaultedOnForAllUsers).toBe(true)
})
it('preserves a post-migration OSC 52 clipboard opt-out', async () => {
writeDataFile({
schemaVersion: 1,
repos: [],
worktreeMeta: {},
settings: {
terminalAllowOsc52Clipboard: false,
terminalAllowOsc52ClipboardDefaultedOnForAllUsers: true
},
ui: {},
githubCache: { pr: {}, issue: {} },
workspaceSession: {}
})
const store = await createStore()
expect(store.getSettings().terminalAllowOsc52Clipboard).toBe(false)
expect(store.getSettings().terminalAllowOsc52ClipboardDefaultedOnForAllUsers).toBe(true)
})
it('arms the one-shot notice when the OSC 52 flip overrides a persisted off', async () => {
writeDataFile({
schemaVersion: 1,
repos: [],
worktreeMeta: {},
settings: { terminalAllowOsc52Clipboard: false },
ui: {},
githubCache: { pr: {}, issue: {} },
workspaceSession: {}
})
const store = await createStore()
expect(store.getUI().osc52ClipboardDefaultOnNoticePending).toBe(true)
})
it('leaves the OSC 52 notice disarmed for a profile with no persisted value', async () => {
// Why: the notice explains an overridden choice; a fresh profile made no choice.
writeDataFile({
schemaVersion: 1,
repos: [],
worktreeMeta: {},
settings: {},
ui: {},
githubCache: { pr: {}, issue: {} },
workspaceSession: {}
})
const store = await createStore()
expect(store.getUI().osc52ClipboardDefaultOnNoticePending).toBe(false)
})
it('keeps the OSC 52 notice armed on disk until the renderer clears it', async () => {
// Why the disk assertion: the flip happens during load, before any window exists, and
// once the settings stamp lands the arming predicate is false forever — so the on-disk
// ui flag is the only thing that survives a crash before the toast renders.
writeDataFile({
schemaVersion: 1,
repos: [],
worktreeMeta: {},
settings: { terminalAllowOsc52Clipboard: false },
ui: {},
githubCache: { pr: {}, issue: {} },
workspaceSession: {}
})
const armed = await createStore()
armed.flush()
const armedOnDisk = readDataFile() as {
ui?: { osc52ClipboardDefaultOnNoticePending?: boolean }
}
expect(armedOnDisk.ui?.osc52ClipboardDefaultOnNoticePending).toBe(true)
const reloaded = await createStore()
expect(reloaded.getUI().osc52ClipboardDefaultOnNoticePending).toBe(true)
reloaded.updateUI({ osc52ClipboardDefaultOnNoticePending: false })
reloaded.flush()
const cleared = await createStore()
// Why re-check after the stamp: a cleared notice must not be resurrected by a later load.
expect(cleared.getUI().osc52ClipboardDefaultOnNoticePending).toBe(false)
})
it('migrates the legacy Linux primary-selection default to enabled', async () => {
await withPlatform('linux', async () => {
writeDataFile({
schemaVersion: 1,
repos: [],
worktreeMeta: {},
settings: { primarySelectionMiddleClickPaste: false },
ui: {},
githubCache: { pr: {}, issue: {} },
workspaceSession: {}
})
const store = await createStore()
expect(store.getSettings().primarySelectionMiddleClickPaste).toBe(true)
expect(store.getSettings().primarySelectionMiddleClickPasteDefaultedForLinux).toBe(true)
expect(store.getSettings().primarySelectionMiddleClickPasteDefaultedForTerminalDefaults).toBe(
true
)
})
})
it('preserves a post-migration Linux primary-selection opt-out', async () => {
await withPlatform('linux', async () => {
writeDataFile({
schemaVersion: 1,
repos: [],
worktreeMeta: {},
settings: {
primarySelectionMiddleClickPaste: false,
primarySelectionMiddleClickPasteDefaultedForLinux: true
},
ui: {},
githubCache: { pr: {}, issue: {} },
workspaceSession: {}
})
const store = await createStore()
expect(store.getSettings().primarySelectionMiddleClickPaste).toBe(false)
expect(store.getSettings().primarySelectionMiddleClickPasteDefaultedForLinux).toBe(true)
expect(store.getSettings().primarySelectionMiddleClickPasteDefaultedForTerminalDefaults).toBe(
true
)
})
})
it('migrates the legacy macOS primary-selection default to enabled', async () => {
await withPlatform('darwin', async () => {
writeDataFile({
schemaVersion: 1,
repos: [],
worktreeMeta: {},
settings: { primarySelectionMiddleClickPaste: false },
ui: {},
githubCache: { pr: {}, issue: {} },
workspaceSession: {}
})
const store = await createStore()
expect(store.getSettings().primarySelectionMiddleClickPaste).toBe(true)
expect(store.getSettings().primarySelectionMiddleClickPasteDefaultedForLinux).toBe(false)
expect(store.getSettings().primarySelectionMiddleClickPasteDefaultedForTerminalDefaults).toBe(
true
)
})
})
it('preserves a post-migration macOS primary-selection opt-out', async () => {
await withPlatform('darwin', async () => {
writeDataFile({
schemaVersion: 1,
repos: [],
worktreeMeta: {},
settings: {
primarySelectionMiddleClickPaste: false,
primarySelectionMiddleClickPasteDefaultedForTerminalDefaults: true
},
ui: {},
githubCache: { pr: {}, issue: {} },
workspaceSession: {}
})
const store = await createStore()
expect(store.getSettings().primarySelectionMiddleClickPaste).toBe(false)
expect(store.getSettings().primarySelectionMiddleClickPasteDefaultedForTerminalDefaults).toBe(
true
)
})
})
it('keeps the primary-selection default disabled on Windows profiles', async () => {
await withPlatform('win32', async () => {
writeDataFile({
schemaVersion: 1,
repos: [],
worktreeMeta: {},
settings: { primarySelectionMiddleClickPaste: false },
ui: {},
githubCache: { pr: {}, issue: {} },
workspaceSession: {}
})
const store = await createStore()
expect(store.getSettings().primarySelectionMiddleClickPaste).toBe(false)
expect(store.getSettings().primarySelectionMiddleClickPasteDefaultedForTerminalDefaults).toBe(
false
)
})
})
})