Files
orca/src/main/persistence-initial-load.test.ts
T
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

532 lines
16 KiB
TypeScript

import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import { writeFileSync, rmSync, mkdtempSync, mkdirSync } from 'node:fs'
import { join } from 'node:path'
import { tmpdir } from 'node:os'
import type { PersistedState } from '../shared/persisted-state-types'
import type { WorkspaceSessionState } from '../shared/workspace-session-state-types'
import { getDefaultPersistedState, getDefaultWorkspaceSession } from '../shared/constants'
import { closeTerminalTabInWorkspaceSession } from '../shared/workspace-session-terminal-tab-close'
import {
testState,
createStore,
writeDataFile,
readDataFile,
makeRepo,
makeProject,
makeProjectHostSetup
} from './persistence-test-harness'
import { TEST_LEAF_1 } from './persistence-session-fixtures'
// 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 })
})
// ── 1. Defaults when no file exists ──────────────────────────────────
it('returns empty repos when no data file exists', async () => {
const store = await createStore()
expect(store.getRepos()).toEqual([])
}, 15_000)
it('clone-reads and synchronously persists the main-owned Codex reset ledger', async () => {
const store = await createStore()
const ledger = {
version: 1 as const,
attempts: [
{
idempotencyKey: '11111111-1111-4111-8111-111111111111',
expectedScope: {
target: { runtime: 'host' as const, wslDistro: null },
accountId: 'account-host',
accountRevision: 42,
offerRevision: 'v1:offer'
},
state: 'providerPending' as const
}
]
}
store.replaceCodexResetCreditAttemptLedgerAndFlush(ledger)
const firstRead = store.getCodexResetCreditAttemptLedger()
firstRead.attempts.splice(0, 1)
expect(store.getCodexResetCreditAttemptLedger()).toEqual(ledger)
expect((readDataFile() as PersistedState).codexResetCreditAttemptLedger).toEqual(ledger)
})
it('rolls the in-memory Codex reset ledger back when its sync flush fails', async () => {
const store = await createStore()
const before = store.getCodexResetCreditAttemptLedger()
vi.spyOn(store, 'flushOrThrow').mockImplementationOnce(() => {
throw new Error('disk full')
})
expect(() =>
store.replaceCodexResetCreditAttemptLedgerAndFlush({
version: 1,
attempts: [
{
idempotencyKey: '11111111-1111-4111-8111-111111111111',
expectedScope: {
target: { runtime: 'host', wslDistro: null },
accountId: 'account-host',
accountRevision: 42,
offerRevision: 'v1:offer'
},
state: 'providerPending'
}
]
})
).toThrow('disk full')
expect(store.getCodexResetCreditAttemptLedger()).toEqual(before)
})
it('preserves a corrupt Codex reset ledger as a fail-closed read error', async () => {
writeDataFile({
...getDefaultPersistedState(testState.dir),
codexResetCreditAttemptLedger: {
version: 1,
attempts: [{ state: 'providerPending' }]
}
})
const store = await createStore()
expect(() => store.getCodexResetCreditAttemptLedger()).toThrow(
'Codex reset-credit attempt ledger is corrupt'
)
})
it('does not restore a terminal tab after its durable close flush returns', async () => {
const store = await createStore()
const worktreeId = 'repo-1::/tmp/worktree-1'
const tabId = 'terminal-1'
const session: WorkspaceSessionState = {
...getDefaultWorkspaceSession(),
activeWorktreeId: worktreeId,
activeTabId: tabId,
tabsByWorktree: {
[worktreeId]: [
{
id: tabId,
ptyId: 'pty-1',
worktreeId,
title: 'Terminal',
customTitle: null,
color: null,
sortOrder: 0,
createdAt: 1
}
]
},
terminalLayoutsByTabId: {
[tabId]: {
root: { type: 'leaf', leafId: TEST_LEAF_1 },
activeLeafId: TEST_LEAF_1,
expandedLeafId: null,
ptyIdsByLeafId: { [TEST_LEAF_1]: 'pty-1' }
}
},
activeTabIdByWorktree: { [worktreeId]: tabId },
defaultTerminalTabsAppliedByWorktreeId: { [worktreeId]: true }
}
store.setWorkspaceSession(session)
store.flushOrThrow()
const closed = closeTerminalTabInWorkspaceSession(
store.getWorkspaceSession(),
worktreeId,
tabId
)
store.setWorkspaceSession(closed.session)
store.flushOrThrow()
const reloaded = await createStore()
expect(reloaded.getWorkspaceSession().tabsByWorktree[worktreeId]).toEqual([])
expect(reloaded.getWorkspaceSession().terminalLayoutsByTabId[tabId]).toBeUndefined()
expect(
reloaded.getWorkspaceSession().defaultTerminalTabsAppliedByWorktreeId?.[worktreeId]
).toBe(true)
})
it('loads state from an explicit profile data file path', async () => {
const profileDataDirectory = join(testState.dir, 'profiles', 'local-default')
const profileDataFile = join(profileDataDirectory, 'orca-data.json')
mkdirSync(profileDataDirectory, { recursive: true })
writeDataFile({
schemaVersion: 1,
repos: [makeRepo({ id: 'legacy-root-repo', path: '/legacy' })]
})
writeFileSync(
profileDataFile,
JSON.stringify({
schemaVersion: 1,
repos: [makeRepo({ id: 'profile-repo', path: '/profile' })]
}),
'utf-8'
)
vi.resetModules()
const { Store, initDataPath } = await import('./persistence')
initDataPath()
const store = new Store({ dataFile: profileDataFile })
expect(store.getRepos().map((repo) => repo.id)).toEqual(['profile-repo'])
}, 15_000)
it('backfills project host setup compatibility records from legacy repos on load', async () => {
writeDataFile({
schemaVersion: 1,
repos: [
makeRepo({
id: 'local-repo',
path: '/Users/alice/orca',
displayName: 'Orca',
upstream: { owner: 'StablyAI', repo: 'Orca' }
}),
makeRepo({
id: 'remote-repo',
path: '/home/alice/orca',
displayName: 'orca',
connectionId: 'gpu-vm',
upstream: { owner: 'stablyai', repo: 'orca' }
})
]
})
const store = await createStore()
expect(store.getProjects()).toEqual([
expect.objectContaining({
id: 'github:stablyai/orca',
sourceRepoIds: ['local-repo', 'remote-repo']
})
])
expect(store.getProjectHostSetups()).toEqual([
expect.objectContaining({
id: 'local-repo',
projectId: 'github:stablyai/orca',
hostId: 'local',
path: '/Users/alice/orca'
}),
expect.objectContaining({
id: 'remote-repo',
projectId: 'github:stablyai/orca',
hostId: 'ssh:gpu-vm',
path: '/home/alice/orca'
})
])
store.flush()
const persisted = readDataFile() as PersistedState
expect(persisted.projects).toEqual(store.getProjects())
expect(persisted.projectHostSetups).toEqual(store.getProjectHostSetups())
})
it('preserves independent project host setup records on load', async () => {
const independentProject = makeProject({
id: 'cloud-project',
displayName: 'Cloud Project'
})
const independentSetup = makeProjectHostSetup({
id: 'cloud-project::gpu-vm',
projectId: independentProject.id,
hostId: 'runtime:gpu-vm',
repoId: '',
path: '/srv/cloud-project',
displayName: 'GPU VM'
})
writeDataFile({
...getDefaultPersistedState(testState.dir),
repos: [makeRepo({ id: 'r1', path: '/repo', displayName: 'Repo' })],
projects: [independentProject],
projectHostSetups: [independentSetup]
})
const store = await createStore()
expect(store.getProjects().map((project) => project.id)).toEqual(['repo:r1', 'cloud-project'])
expect(store.getProjectHostSetups().map((setup) => setup.id)).toEqual([
'r1',
'cloud-project::gpu-vm'
])
store.flush()
const persisted = readDataFile() as PersistedState
expect(persisted.projectHostSetups).toContainEqual(independentSetup)
})
it('updates and persists a project Windows runtime preference', async () => {
const project = makeProject({
id: 'project-1',
sourceRepoIds: ['r1'],
localWindowsRuntimePreference: { kind: 'inherit-global' }
})
writeDataFile({
...getDefaultPersistedState(testState.dir),
projects: [project],
projectHostSetups: [
makeProjectHostSetup({
id: 'setup-1',
projectId: project.id,
repoId: ''
})
]
})
const store = await createStore()
const updated = store.updateProject('project-1', {
localWindowsRuntimePreference: { kind: 'wsl', distro: 'Ubuntu' }
})
expect(updated?.localWindowsRuntimePreference).toEqual({ kind: 'wsl', distro: 'Ubuntu' })
store.flush()
const reloaded = await createStore()
expect(reloaded.getProjects()[0]?.localWindowsRuntimePreference).toEqual({
kind: 'wsl',
distro: 'Ubuntu'
})
})
it('carries project state and independent setups across a repo remote identity change', async () => {
const originProjectId = 'git:git.example.com/acme/app'
writeDataFile({
...getDefaultPersistedState(testState.dir),
repos: [
makeRepo({
id: 'r1',
path: '/repo',
displayName: 'App',
gitRemoteIdentity: {
canonicalKey: 'git.example.com/acme/app',
remoteName: 'origin',
remoteUrl: 'git@git.example.com:acme/app.git'
}
})
],
projects: [
makeProject({
id: originProjectId,
sourceRepoIds: ['r1'],
localWindowsRuntimePreference: { kind: 'wsl', distro: 'Ubuntu' }
})
],
projectHostSetups: [
makeProjectHostSetup({ id: 'r1', projectId: originProjectId, repoId: 'r1' }),
makeProjectHostSetup({
id: 'app::gpu-vm',
projectId: originProjectId,
hostId: 'runtime:gpu-vm',
repoId: '',
path: '/srv/app'
})
]
})
const store = await createStore()
// A re-probe that now prefers the `upstream` remote rewrites the derived project id.
store.updateRepo('r1', {
gitRemoteIdentity: {
canonicalKey: 'git.example.com/acme/app-upstream',
remoteName: 'upstream',
remoteUrl: 'git@git.example.com:acme/app-upstream.git'
}
})
const upstreamProjectId = 'git:git.example.com/acme/app-upstream'
expect(store.getProjects().map((project) => project.id)).toEqual([upstreamProjectId])
expect(store.getProjects()[0]?.localWindowsRuntimePreference).toEqual({
kind: 'wsl',
distro: 'Ubuntu'
})
expect(
store.getProjectHostSetups().find((setup) => setup.id === 'app::gpu-vm')?.projectId
).toBe(upstreamProjectId)
})
it('picks one predecessor project when several prior rows overlap the same repos', async () => {
const sharedIdentity = {
canonicalKey: 'git.example.com/acme/shared',
remoteName: 'origin',
remoteUrl: 'git@git.example.com:acme/shared.git'
}
writeDataFile({
...getDefaultPersistedState(testState.dir),
repos: [
makeRepo({
id: 'r1',
path: '/left',
displayName: 'Left',
gitRemoteIdentity: sharedIdentity
}),
makeRepo({
id: 'r2',
path: '/right',
displayName: 'Right',
gitRemoteIdentity: sharedIdentity
})
],
projects: [
makeProject({
id: 'git:git.example.com/acme/left',
sourceRepoIds: ['r1'],
updatedAt: 200,
localWindowsRuntimePreference: { kind: 'wsl', distro: 'Ubuntu' }
}),
makeProject({
id: 'git:git.example.com/acme/right',
sourceRepoIds: ['r2'],
updatedAt: 100,
localWindowsRuntimePreference: { kind: 'windows-host' }
})
],
projectHostSetups: [
makeProjectHostSetup({
id: 'r1',
projectId: 'git:git.example.com/acme/left',
repoId: 'r1'
}),
makeProjectHostSetup({
id: 'r2',
projectId: 'git:git.example.com/acme/right',
repoId: 'r2'
})
]
})
const store = await createStore()
// Equal repo overlap resolves by newest updatedAt; the loser's preference is never merged in.
expect(store.getProjects()).toEqual([
expect.objectContaining({
id: 'git:git.example.com/acme/shared',
sourceRepoIds: ['r1', 'r2'],
localWindowsRuntimePreference: { kind: 'wsl', distro: 'Ubuntu' }
})
])
})
it('migrates legacy WSL agent settings into the global Windows runtime default', async () => {
writeDataFile({
schemaVersion: 1,
repos: [],
worktreeMeta: {},
settings: {
localAgentRuntime: 'wsl',
localAgentWslDistro: 'Ubuntu'
}
})
const store = await createStore()
expect(store.getSettings().localWindowsRuntimeDefault).toEqual({
kind: 'wsl',
distro: 'Ubuntu'
})
store.flush()
expect((readDataFile() as PersistedState).settings.localWindowsRuntimeDefault).toEqual({
kind: 'wsl',
distro: 'Ubuntu'
})
})
it('migrates the legacy host account-runtime default to auto once', async () => {
writeDataFile({
schemaVersion: 1,
repos: [],
worktreeMeta: {},
settings: {
localAccountRuntime: 'host'
}
})
const store = await createStore()
expect(store.getSettings().localAccountRuntime).toBe('auto')
expect(store.getSettings().localAccountRuntimeDefaultedToAutoForAllUsers).toBe(true)
store.flush()
const persisted = (readDataFile() as PersistedState).settings
expect(persisted.localAccountRuntime).toBe('auto')
expect(persisted.localAccountRuntimeDefaultedToAutoForAllUsers).toBe(true)
})
it('preserves an explicit WSL account-runtime pin through the migration', async () => {
writeDataFile({
schemaVersion: 1,
repos: [],
worktreeMeta: {},
settings: {
localAccountRuntime: 'wsl',
localAccountWslDistro: 'Ubuntu'
}
})
const store = await createStore()
expect(store.getSettings().localAccountRuntime).toBe('wsl')
expect(store.getSettings().localAccountRuntimeDefaultedToAutoForAllUsers).toBe(true)
})
it('does not re-flip an explicit host pin chosen after migration', async () => {
writeDataFile({
schemaVersion: 1,
repos: [],
worktreeMeta: {},
settings: {
localAccountRuntime: 'host',
localAccountRuntimeDefaultedToAutoForAllUsers: true
}
})
const store = await createStore()
expect(store.getSettings().localAccountRuntime).toBe('host')
})
})