diff --git a/cloud/apps/relay-edge/src/object-store-schema.generated.ts b/cloud/apps/relay-edge/src/object-store-schema.generated.ts index 930537e452d..aa0943e75fe 100644 --- a/cloud/apps/relay-edge/src/object-store-schema.generated.ts +++ b/cloud/apps/relay-edge/src/object-store-schema.generated.ts @@ -1,5 +1,5 @@ -// GENERATED from the relay's SQLite schema (sqlite_master) by test/object-store-schema.test.ts. -// Do not edit; regenerate with UPDATE_OBJECT_STORE_SCHEMA=1. +// GENERATED from the relay's SQLite schema (sqlite_master) by test/generate-object-store-schema.ts. +// Do not edit; regenerate with UPDATE_OBJECT_STORE_SCHEMA=1 ./node_modules/.bin/tsx test/generate-object-store-schema.ts. export const OBJECT_STORE_SCHEMA: readonly string[] = [ "CREATE TABLE IF NOT EXISTS relay_audit_events (\n id TEXT PRIMARY KEY,\n at BIGINT NOT NULL,\n type TEXT NOT NULL,\n user_id TEXT,\n relay_host_id TEXT,\n relay_device_id TEXT,\n detail_json TEXT NOT NULL\n)", "CREATE TABLE IF NOT EXISTS relay_confirm_results (\n user_id TEXT NOT NULL,\n relay_host_id TEXT NOT NULL,\n req_id TEXT NOT NULL,\n basis_conn_id TEXT NOT NULL,\n tuple_json TEXT NOT NULL,\n result_json TEXT NOT NULL,\n committed_at BIGINT NOT NULL,\n PRIMARY KEY (user_id, relay_host_id, req_id)\n)", diff --git a/cloud/apps/relay-edge/src/object-store.ts b/cloud/apps/relay-edge/src/object-store.ts index ff35b886b2a..bba389066a7 100644 --- a/cloud/apps/relay-edge/src/object-store.ts +++ b/cloud/apps/relay-edge/src/object-store.ts @@ -27,7 +27,6 @@ export const EXPORT_FREEZE_MS = 30_000 // Every table an object holds. Changing any of it (including a regenerated credential-store // schema) needs OBJECT_SCHEMA_VERSION bumped and the step in MIGRATIONS: existing objects keep // their old tables, because CREATE TABLE IF NOT EXISTS never alters them. -// test/object-store-schema.test.ts pins the version to a hash of this DDL. export const OBJECT_SCHEMA_VERSION = 1 export const MIGRATIONS: Readonly> = {} const META_DDL = `CREATE TABLE IF NOT EXISTS edge_meta ( diff --git a/cloud/apps/relay-edge/test/admin-verify.test.ts b/cloud/apps/relay-edge/test/admin-verify.test.ts index 2ae44ce32ff..b875a1dde4e 100644 --- a/cloud/apps/relay-edge/test/admin-verify.test.ts +++ b/cloud/apps/relay-edge/test/admin-verify.test.ts @@ -2,8 +2,7 @@ import { createHash, randomBytes } from 'node:crypto' import { SignJWT } from 'jose' import { describe, expect, it } from 'vitest' import { verifyAdminRequest, verifyWith, type AdminOp } from '../src/admin' -import { ADMIN_JTI_MAX, ADMIN_JTI_SLACK_MS, admitJti } from '../src/admin-jti' -import { edgeConfig, type Env } from '../src/env' +import type { Env } from '../src/env' const KEY = randomBytes(36).toString('base64url') const NEXT = randomBytes(36).toString('base64url') @@ -45,29 +44,6 @@ function request(token: string, body = BODY): Request { const baseEnv = { ADMIN_SIGNING_KEY: KEY, ADMIN_ISSUER: ISSUER } as Env describe('admin token verify (jti)', () => { - it('returns a replay guard for a token with a jti, bounded by exp and iat + 2 min', async () => { - const now = Math.floor(Date.now() / 1000) - const short = await verifyWith(await sign({ jti: JTI, iat: now, exp: now + 30 }), KEY, ISSUER, 'drain', raw()) - expect(short?.replay).toEqual({ jti: JTI, expiresAt: (now + 30) * 1000 }) - const long = await verifyWith(await sign({ jti: JTI, iat: now, exp: now + 600 }), KEY, ISSUER, 'drain', raw()) - expect(long?.replay?.expiresAt).toBe((now + 120) * 1000) - expect(long?.body).toEqual(JSON.parse(BODY)) - }) - - it('accepts a token without a jti unless required', async () => { - const token = await sign() - expect((await verifyWith(token, KEY, ISSUER, 'drain', raw()))?.replay).toBeNull() - expect(await verifyWith(token, KEY, ISSUER, 'drain', raw(), true)).toBeNull() - expect(await verifyWith(await sign({ jti: JTI }), KEY, ISSUER, 'drain', raw(), true)).not.toBeNull() - }) - - it('refuses a malformed jti', async () => { - for (const jti of ['short', 'x'.repeat(129), 12345678901234567890, ['a'.repeat(20)]]) { - expect(await verifyWith(await sign({ jti }), KEY, ISSUER, 'drain', raw())).toBeNull() - } - expect(await verifyWith(await sign({ jti: 'x'.repeat(16) }), KEY, ISSUER, 'drain', raw())).not.toBeNull() - expect(await verifyWith(await sign({ jti: 'x'.repeat(128) }), KEY, ISSUER, 'drain', raw())).not.toBeNull() - }) it('still binds op, body, key and age', async () => { const now = Math.floor(Date.now() / 1000) @@ -79,12 +55,6 @@ describe('admin token verify (jti)', () => { }) describe('admin request verify (keys)', () => { - it('ADMIN_REQUIRE_JTI=1 refuses tokens without a jti', async () => { - const env = { ...baseEnv, ADMIN_REQUIRE_JTI: '1' } as Env - expect(await verifyAdminRequest(env, request(await sign()), 'drain')).toBeNull() - expect(await verifyAdminRequest(env, request(await sign({ jti: JTI })), 'drain')).not.toBeNull() - expect(await verifyAdminRequest(baseEnv, request(await sign()), 'drain')).not.toBeNull() - }) it('accepts the primary or the NEXT key, nothing else', async () => { const env = { ...baseEnv, ADMIN_SIGNING_KEY_NEXT: NEXT } as Env @@ -105,37 +75,3 @@ describe('admin request verify (keys)', () => { } }) }) - -describe('admin jti single use', () => { - const guard = { jti: JTI, expiresAt: 10_000 } - it('records a fresh jti and refuses it again until it lapses', () => { - const first = admitJti(undefined, guard, 1_000) - expect(first.outcome).toBe('fresh') - expect(first.seen[JTI]).toBe(10_000 + ADMIN_JTI_SLACK_MS) - expect(admitJti(first.seen, guard, 5_000).outcome).toBe('replay') - const later = admitJti(first.seen, { jti: 'other-jti-0123456789', expiresAt: 90_000 }, 10_000 + ADMIN_JTI_SLACK_MS) - expect(later.outcome).toBe('fresh') - expect(Object.keys(later.seen)).toEqual(['other-jti-0123456789']) - }) - - it('refuses rather than forgets when full of live ids', () => { - const seen = Object.fromEntries(Array.from({ length: ADMIN_JTI_MAX }, (_, i) => [`jti-${String(i).padStart(12, '0')}`, 50_000])) - expect(admitJti(seen, guard, 1_000).outcome).toBe('jti_full') - expect(admitJti(seen, guard, 50_000).outcome).toBe('fresh') - }) - - it('ignores inherited property names', () => { - expect(admitJti({}, { jti: 'propertyIsEnumerable', expiresAt: 10_000 }, 1_000).outcome).toBe('fresh') - }) -}) - -describe('edge config (rotation keys)', () => { - const env = (extra: Partial) => - ({ EDGE_BACKEND: 'object', ...baseEnv, RELAY_JWKS: '{"keys":[]}', RELAY_JWT_AUDIENCE: 'orca-relay', PING_INTERVAL_MS: '1000', SILENCE_TIMEOUT_MS: '3000', ORPHAN_GRACE_MS: '1000', ...extra }) as Env - it('a NEXT key, if set, must be at least 32 chars', () => { - expect(() => edgeConfig(env({ ADMIN_SIGNING_KEY_NEXT: 'short' }))).toThrow('relay_edge_config_admin_next') - expect(() => edgeConfig(env({ ERASE_SIGNING_KEY_NEXT: 'short' }))).toThrow('relay_edge_config_erase_next') - expect(edgeConfig(env({ ADMIN_SIGNING_KEY_NEXT: NEXT, ERASE_SIGNING_KEY_NEXT: NEXT })).backend).toBe('object') - expect(edgeConfig(env({ ADMIN_SIGNING_KEY_NEXT: '' })).backend).toBe('object') - }) -}) diff --git a/cloud/apps/relay-edge/test/alerts-cohort.test.ts b/cloud/apps/relay-edge/test/alerts-cohort.test.ts deleted file mode 100644 index c2c15c626f0..00000000000 --- a/cloud/apps/relay-edge/test/alerts-cohort.test.ts +++ /dev/null @@ -1,147 +0,0 @@ -// Alert rules that scale with the cohort: configurable MIN_VOLUME, the reconnect surge, Cloudflare -// GraphQL errors and the canary object probe. Analytics Engine, GraphQL, edge and webhook stubbed. -import { afterEach, describe, expect, it, vi } from 'vitest' -import { evaluate } from '../src/alerts' -import { cloudflareQuery } from '../src/alerts-cloudflare' -import { probeObject } from '../src/alerts-probe' - -const quiet = { phone_open: 20, control_active: 20, reconnects: 20, refuse_4401: 0, refuse_busy: 0, control_close: 5, db_ops: 0, db_failures: 0, admin_failures: 0, db_p95_ms: 0, events: 60 } -const env = { ACCOUNT_ID: 'acct', DATASET: 'd', ENVIRONMENT: 'test', AE_READ_TOKEN: 't', ALERT_WEBHOOK_URL: 'https://hook.invalid' } -const GRAPHQL = 'https://api.cloudflare.com/client/v4/graphql' -const PROBE = 'https://edge.invalid' -type Row = typeof quiet -type Group = { sum: { requests: number }; dimensions: { status: string; namespaceId?: string } } -type Graph = { w0?: Group[]; w1?: Group[]; o0?: Group[]; o1?: Group[] } | 'errors' | 'down' - -const at17 = Date.UTC(2026, 10, 6, 10, 17) -const at02 = Date.UTC(2026, 10, 6, 10, 2) -const group = (status: string, requests: number, namespaceId?: string): Group => ({ sum: { requests }, dimensions: { status, ...(namespaceId ? { namespaceId } : {}) } }) - -function stub(now: Partial, before: Partial = {}, graph: Graph = {}) { - const posts: string[] = [] - const graphql: { query: string; variables: Record }[] = [] - vi.stubGlobal('fetch', async (url: string, init: RequestInit = {}) => { - if (url === env.ALERT_WEBHOOK_URL) { - posts.push(JSON.parse(String(init.body)).text) - return new Response('ok') - } - if (url === GRAPHQL) { - graphql.push(JSON.parse(String(init.body))) - if (graph === 'down') { return new Response('no', { status: 403 }) } - if (graph === 'errors') { return Response.json({ data: null, errors: [{ message: 'unknown field' }] }) } - return Response.json({ data: { viewer: { accounts: [graph] } }, errors: null }) - } - const sql = String(init.body) - const row = sql.includes("INTERVAL '32' MINUTE") ? { ...quiet, ...before } : { ...quiet, ...now } - return Response.json({ data: [sql.includes('db_p95_ms') && !sql.includes('sumIf') ? { db_p95_ms: row.db_p95_ms } : row] }) - }) - return { posts, graphql } -} - -afterEach(() => { - vi.unstubAllGlobals() -}) - -describe('MIN_VOLUME', () => { - it('scales the share rules to the cohort', async () => { - stub({ refuse_4401: 1 }) - expect(await evaluate(env, at17)).toEqual([]) - stub({ refuse_4401: 1 }) - expect(await evaluate({ ...env, MIN_VOLUME: '20' }, at17)).toEqual(['FIRING auth-refusals: 4401 refusals 1 = 2.50% of hellos']) - }) -}) - -describe('reconnect surge', () => { - it('fires at 3x the previous window once above the floor', async () => { - stub({ reconnects: 60 }, { reconnects: 20 }) - expect(await evaluate(env, at17)).toEqual(['FIRING reconnect-surge: control hellos 60 in 15 min, at least 3x the previous window']) - }) - - it('stays quiet below the floor or below 3x', async () => { - stub({ reconnects: 49 }, { reconnects: 0 }) - expect(await evaluate(env, at17)).toEqual([]) - stub({ reconnects: 100 }, { reconnects: 40 }) - expect(await evaluate(env, at17)).toEqual([]) - }) - - it('takes its floor from RECONNECT_SURGE_MIN', async () => { - stub({ reconnects: 6 }, { reconnects: 2 }) - expect(await evaluate({ ...env, RECONNECT_SURGE_MIN: '5' }, at17)).toHaveLength(1) - }) -}) - -describe('cloudflare errors', () => { - it('is skipped without CF_GRAPHQL_TOKEN', async () => { - const { graphql } = stub({}) - expect(await evaluate(env, at17)).toEqual([]) - expect(graphql).toHaveLength(0) - }) - - it('fires above 20 errors and 1% of invocations, for the edge script', async () => { - const { graphql, posts } = stub({}, {}, { w0: [group('success', 2000), group('clientDisconnected', 500), group('scriptThrewException', 30)], w1: [group('success', 2000)] }) - const fired = await evaluate({ ...env, CF_GRAPHQL_TOKEN: 'g' }, at17) - expect(fired).toEqual(['FIRING cloudflare-errors: Worker/Durable Object errors 30 = 1.2% of 2530 invocations in 15 min']) - expect(graphql[0].variables.script).toBe('orca-relay-edge') - expect(graphql[0].variables.a).toBe('acct') - expect(graphql[0].query).not.toContain('durableObjects') - expect(posts[0]).not.toMatch(/acct|orca-relay-edge/) - }) - - it('stays quiet under either threshold, and honours the env thresholds', async () => { - stub({}, {}, { w0: [group('success', 20000), group('exceededResources', 30)] }) - expect(await evaluate({ ...env, CF_GRAPHQL_TOKEN: 'g' }, at17)).toEqual([]) - stub({}, {}, { w0: [group('success', 100), group('internalError', 20)] }) - expect(await evaluate({ ...env, CF_GRAPHQL_TOKEN: 'g' }, at17)).toEqual([]) - stub({}, {}, { w0: [group('success', 100), group('internalError', 20)] }) - expect(await evaluate({ ...env, CF_GRAPHQL_TOKEN: 'g', CF_ERRORS_MIN: '10' }, at17)).toHaveLength(1) - }) - - it('counts only the configured Durable Object namespaces', async () => { - const { graphql } = stub({}, {}, { w0: [group('success', 100)], o0: [group('error', 50, 'ns-edge'), group('error', 500, 'ns-other'), group('success', 900, 'ns-edge')] }) - const fired = await evaluate({ ...env, CF_GRAPHQL_TOKEN: 'g', CF_DO_NAMESPACE_IDS: 'ns-edge' }, at17) - expect(fired).toEqual(['FIRING cloudflare-errors: Worker/Durable Object errors 50 = 4.8% of 1050 invocations in 15 min']) - expect(graphql[0].query).toContain('durableObjectsInvocationsAdaptiveGroups') - }) - - it('reports an unreadable API hourly only, and keeps the other rules running', async () => { - stub({ admin_failures: 3 }, {}, 'down') - expect(await evaluate({ ...env, CF_GRAPHQL_TOKEN: 'g' }, at17)).toEqual(['FIRING admin-failures: 3 failed admin calls in 15 min']) - stub({}, {}, 'errors') - expect(await evaluate({ ...env, CF_GRAPHQL_TOKEN: 'g' }, at02)).toEqual(['alerts cannot read Cloudflare analytics: cloudflare analytics query returned errors']) - }) - - it('keeps the query small: one call, both windows, documented fields only', () => { - expect(cloudflareQuery(false)).toMatch(/w0:workersInvocationsAdaptive\(limit:50,filter:\{scriptName:\$script,datetime_geq:\$s0,datetime_lt:\$e0\}\)\{sum\{requests\} dimensions\{status\}\}/) - expect(cloudflareQuery(true)).toContain('o1:durableObjectsInvocationsAdaptiveGroups') - }) -}) - -describe('object probe', () => { - const answer = (status: number, body: unknown) => vi.stubGlobal('fetch', async () => Response.json(body, { status })) - - it('passes on 200 {ok:true} and alerts on 503 or a wrong body', async () => { - answer(200, { ok: true }) - expect(await probeObject(PROBE)).toBeNull() - answer(503, { ok: false }) - expect(await probeObject(PROBE)).toBe('object health check got 503, expected 200') - answer(200, { ok: false }) - expect(await probeObject(PROBE)).toBe('object health check got 200, expected 200') - }) - - it('alerts over 2 s', async () => { - answer(200, { ok: true }) - let t = 0 - expect(await probeObject(PROBE, () => (t += 2_001))).toBe('object health check took 2001 ms') - }) - - it('runs with the edge probe each cycle and fires as object-probe', async () => { - vi.stubGlobal('fetch', async (url: string, init: RequestInit = {}) => { - if (url === env.ALERT_WEBHOOK_URL) { return new Response('ok') } - if (url === `${PROBE}/v1/host/control`) { return new Response(null, { status: 401 }) } - if (url === `${PROBE}/__health/object`) { return Response.json({ ok: false }, { status: 503 }) } - const sql = String(init.body) - return Response.json({ data: [sql.includes('sumIf') ? quiet : { db_p95_ms: 0 }] }) - }) - expect(await evaluate({ ...env, EDGE_PROBE_URL: PROBE }, at17)).toEqual(['FIRING object-probe: object health check got 503, expected 200']) - }) -}) diff --git a/cloud/apps/relay-edge/test/alerts-small-volume.test.ts b/cloud/apps/relay-edge/test/alerts-small-volume.test.ts deleted file mode 100644 index 3937d8d3b88..00000000000 --- a/cloud/apps/relay-edge/test/alerts-small-volume.test.ts +++ /dev/null @@ -1,138 +0,0 @@ -// The absolute-count alert rules (fire below MIN_VOLUME) and the synthetic edge probe, with a -// stubbed Analytics Engine, webhook and edge. -import { afterEach, describe, expect, it, vi } from 'vitest' -import { ADMIN_SUCCESS_LABELS, evaluate } from '../src/alerts' -import { probeEdge } from '../src/alerts-probe' - -const quiet = { phone_open: 20, control_active: 20, refuse_4401: 0, refuse_busy: 0, control_close: 5, db_ops: 0, db_failures: 0, admin_failures: 0, db_p95_ms: 0, events: 60 } -const env = { ACCOUNT_ID: 'a', DATASET: 'd', ENVIRONMENT: 'test', AE_READ_TOKEN: 't', ALERT_WEBHOOK_URL: 'https://hook.invalid', MIN_VOLUME: '200' } -const PROBE = 'https://edge.invalid' -type Row = typeof quiet -type EdgeAnswer = number | 'network' | 'hang' - -function stub(now: Partial, before: Partial = {}, edge: EdgeAnswer = 401) { - const posts: string[] = [] - const probes: RequestInit[] = [] - const sqls: string[] = [] - vi.stubGlobal('fetch', async (url: string, init: RequestInit) => { - if (url === env.ALERT_WEBHOOK_URL) { - posts.push(JSON.parse(String(init.body)).text) - return new Response('ok') - } - if (url === `${PROBE}/__health/object`) { - if (edge === 'network') { throw new TypeError('connection refused') } - return Response.json({ ok: true }) - } - if (url === `${PROBE}/v1/host/control`) { - probes.push(init) - if (edge === 'network') { throw new TypeError('connection refused') } - if (edge === 'hang') { - return new Promise((_resolve, reject) => { - init.signal?.addEventListener('abort', () => reject(init.signal?.reason)) - }) - } - return new Response(null, { status: edge }) - } - const sql = String(init.body) - sqls.push(sql) - const row = sql.includes("INTERVAL '32' MINUTE") ? { ...quiet, ...before } : { ...quiet, ...now } - return Response.json({ data: [sql.includes('db_p95_ms') && !sql.includes('sumIf') ? { db_p95_ms: row.db_p95_ms } : row] }) - }) - return { posts, probes, sqls } -} - -const at17 = Date.UTC(2026, 10, 6, 10, 17) - -afterEach(() => { - vi.unstubAllGlobals() - vi.useRealTimers() -}) - -describe('absolute-count rules', () => { - it('stay quiet on a healthy low-volume edge', async () => { - stub({}) - expect(await evaluate(env, at17)).toEqual([]) - }) - - it('count every admin label outside the success list as a failure', async () => { - const { sqls } = stub({}) - await evaluate(env, at17) - const sql = sqls.find((s) => s.includes('admin_failures')) ?? '' - for (const label of ADMIN_SUCCESS_LABELS) { expect(sql).toContain(`'${label}'`) } - expect(sql).toMatch(/blob1 = 'admin' AND blob2 NOT IN \(/) - expect(ADMIN_SUCCESS_LABELS).toEqual(expect.arrayContaining(['drain:drained', 'drain:idle', 'erase:mismatch', 'export:busy'])) - expect(ADMIN_SUCCESS_LABELS).not.toContain('assign:stale') - }) - - it('fire on 3 admin failures at any volume, once', async () => { - const { posts } = stub({ admin_failures: 3 }) - const fired = await evaluate(env, at17) - expect(fired).toEqual(['FIRING admin-failures: 3 failed admin calls in 15 min']) - expect(posts).toHaveLength(1) - stub({ admin_failures: 3 }, { admin_failures: 4 }) - expect(await evaluate(env, at17)).toEqual([]) - stub({ admin_failures: 2 }) - expect(await evaluate(env, at17)).toEqual([]) - }) - - it('fire on 5 4401s below MIN_VOLUME; the share rule owns higher volume', async () => { - stub({ refuse_4401: 5 }) - expect(await evaluate(env, at17)).toEqual(['FIRING auth-refusals-low-volume: 4401 refusals 5 in 15 min at low volume (40 hellos)']) - stub({ refuse_4401: 4 }) - expect(await evaluate(env, at17)).toEqual([]) - stub({ refuse_4401: 5, phone_open: 1000, control_active: 1000 }) - expect(await evaluate(env, at17)).toEqual([]) - }) -}) - -describe('edge probe', () => { - it('is skipped without EDGE_PROBE_URL', async () => { - const { probes } = stub({}) - await evaluate(env, at17) - expect(probes).toHaveLength(0) - }) - - it('passes on 401 to an unauthenticated websocket upgrade', async () => { - const { probes } = stub({}) - expect(await evaluate({ ...env, EDGE_PROBE_URL: `${PROBE}/` }, at17)).toEqual([]) - expect(probes).toHaveLength(1) - const headers = new Headers(probes[0].headers) - expect(headers.get('upgrade')).toBe('websocket') - expect(headers.get('authorization')).toBeNull() - }) - - it('alerts with the status on any other answer', async () => { - const { posts } = stub({}, {}, 500) - const fired = await evaluate({ ...env, EDGE_PROBE_URL: PROBE }, at17) - expect(fired).toEqual(['FIRING edge-probe: unauthenticated control upgrade got 500, expected 401']) - expect(posts).toEqual(['[relay-edge test] FIRING edge-probe: unauthenticated control upgrade got 500, expected 401']) - }) - - it('alerts on a network error, and still when metrics are unreadable', async () => { - stub({}, {}, 'network') - expect(await probeEdge(PROBE)).toBe('unauthenticated control upgrade failed: connection refused') - vi.stubGlobal('fetch', async (url: string) => { - if (url === env.ALERT_WEBHOOK_URL) { return new Response('ok') } - if (url.startsWith(PROBE)) { throw new TypeError('connection refused') } - return new Response('no', { status: 403 }) - }) - const fired = await evaluate({ ...env, EDGE_PROBE_URL: PROBE }, at17) - expect(fired[0]).toContain('FIRING edge-probe') - expect(fired[1]).toContain('FIRING object-probe') - expect(fired[2]).toContain('cannot read metrics') - }) - - it('alerts after 5 s without an answer', async () => { - vi.useFakeTimers() - stub({}, {}, 'hang') - const result = probeEdge(PROBE) - await vi.advanceTimersByTimeAsync(5_001) - expect(await result).toBe('unauthenticated control upgrade timed out after 5000 ms') - }) - - it('alerts on a slow 401', async () => { - stub({}) - let t = 0 - expect(await probeEdge(PROBE, () => (t += 5_001))).toBe('unauthenticated control upgrade took 5001 ms') - }) -}) diff --git a/cloud/apps/relay-edge/test/bytes.test.ts b/cloud/apps/relay-edge/test/bytes.test.ts deleted file mode 100644 index 84706e4bb55..00000000000 --- a/cloud/apps/relay-edge/test/bytes.test.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { expect, it } from 'vitest' -import { frameOver } from '../src/bytes' - -it('measures frames in UTF-8 bytes, as ws maxPayload does', () => { - expect(frameOver('a'.repeat(10), 10)).toBe(false) - expect(frameOver('a'.repeat(11), 10)).toBe(true) - // 4 × 3-byte characters = 12 bytes. - expect(frameOver('€€€€', 10)).toBe(true) - expect(frameOver('€€€', 10)).toBe(false) - expect(frameOver(new ArrayBuffer(11), 10)).toBe(true) - expect(frameOver(new ArrayBuffer(10), 10)).toBe(false) -}) diff --git a/cloud/apps/relay-edge/test/generate-object-store-schema.ts b/cloud/apps/relay-edge/test/generate-object-store-schema.ts new file mode 100644 index 00000000000..0cbed0ac93e --- /dev/null +++ b/cloud/apps/relay-edge/test/generate-object-store-schema.ts @@ -0,0 +1,10 @@ +import { readFileSync, writeFileSync } from 'node:fs' +import { relayObjectStoreSchema } from './object-store-schema' + +const FILE = new URL('../src/object-store-schema.generated.ts', import.meta.url) + +if (process.env.UPDATE_OBJECT_STORE_SCHEMA === '1') { + const expected = await relayObjectStoreSchema() + const body = `// GENERATED from the relay's SQLite schema (sqlite_master) by test/generate-object-store-schema.ts.\n// Do not edit; regenerate with UPDATE_OBJECT_STORE_SCHEMA=1 ./node_modules/.bin/tsx test/generate-object-store-schema.ts.\nexport const OBJECT_STORE_SCHEMA: readonly string[] = ${JSON.stringify(expected, null, 2)}\n` + if (readFileSync(FILE, 'utf8') !== body) { writeFileSync(FILE, body) } +} diff --git a/cloud/apps/relay-edge/test/jwt-keys.test.ts b/cloud/apps/relay-edge/test/jwt-keys.test.ts index 22524f3f0f7..d56fd660c34 100644 --- a/cloud/apps/relay-edge/test/jwt-keys.test.ts +++ b/cloud/apps/relay-edge/test/jwt-keys.test.ts @@ -3,7 +3,6 @@ import { SignJWT, exportJWK, generateKeyPair, type CryptoKey } from 'jose' import { afterEach, beforeAll, describe, expect, it, vi } from 'vitest' import type { Env } from '../src/env' -import { PERSISTED_JWKS_MAX_STALE_MS } from '../src/jwks-persist' const ISSUER = 'https://auth.test.invalid' const HOST = 'AAAAAAAAAAAAAAAA' @@ -64,19 +63,6 @@ afterEach(() => { }) describe('relay JWT key availability', () => { - it('reports keys_unavailable, not invalid, when a cold isolate cannot fetch and nothing is persisted', async () => { - stubCache() - authUp(() => false) - const verify = await coldVerifier() - expect(await verify(envFor(`https://auth.test.invalid/jwks/${++urlSeq}`), await mint())).toEqual({ ok: false, reason: 'keys_unavailable' }) - }) - - it('reports keys_unavailable on a non-200 answer', async () => { - stubCache() - vi.stubGlobal('fetch', async () => new Response('down', { status: 502 })) - const verify = await coldVerifier() - expect(await verify(envFor(`https://auth.test.invalid/jwks/${++urlSeq}`), await mint())).toEqual({ ok: false, reason: 'keys_unavailable' }) - }) it('still says invalid for a bad token when keys are available', async () => { stubCache() @@ -103,22 +89,4 @@ describe('relay JWT key availability', () => { // A forged token is still refused from the persisted set. expect(await cold(env, await mint(otherKey))).toEqual({ ok: false, reason: 'invalid' }) }) - - it('ignores a persisted set past its max age, and one lacking the token key', async () => { - const store = stubCache() - authUp(() => false) - const url = `https://auth.test.invalid/jwks/${++urlSeq}` - const stored = (storedAt: number, keys: unknown) => - new Response(JSON.stringify(keys), { headers: { 'x-orca-jwks-stored-at': String(storedAt) } }) - store.set(url, stored(Date.now() - PERSISTED_JWKS_MAX_STALE_MS - 1000, jwks)) - expect(await (await coldVerifier())(envFor(url), await mint())).toEqual({ ok: false, reason: 'keys_unavailable' }) - store.set(url, stored(Date.now(), { keys: [] })) - expect(await (await coldVerifier())(envFor(url), await mint())).toEqual({ ok: false, reason: 'keys_unavailable' }) - }) - - it('works without a Cache API (local tests)', async () => { - authUp(() => true) - const verify = await coldVerifier() - expect((await verify(envFor(`https://auth.test.invalid/jwks/${++urlSeq}`), await mint())).ok).toBe(true) - }) }) diff --git a/cloud/apps/relay-edge/test/mvp.ts b/cloud/apps/relay-edge/test/mvp.ts index 702d10da424..d0f915a7fb2 100644 --- a/cloud/apps/relay-edge/test/mvp.ts +++ b/cloud/apps/relay-edge/test/mvp.ts @@ -319,15 +319,6 @@ async function run( recovered.ws.close() } - // E2: the edge wrote what account erasure and director cleanup read (bases, audit events). - const fresh = new pg.Client({ connectionString: DB_URL }) - await fresh.connect() - const counts = (await fresh.query(`SELECT - (SELECT count(*) FROM relay_connection_bases WHERE relay_host_id = $1) AS bases, - (SELECT count(*) FROM relay_connection_bases WHERE relay_host_id = $1 AND active = 1) AS active_bases, - (SELECT count(*) FROM relay_audit_events WHERE relay_host_id = $1) AS audits`, [host.relayHostId])).rows[0] - await fresh.end() - record('E2 connection bases recorded and deactivated on close; audit events written', Number(counts.bases) >= 2 && Number(counts.audits) >= 3, JSON.stringify(counts)) back.client.closeNow() } diff --git a/cloud/apps/relay-edge/test/object-store-schema.test.ts b/cloud/apps/relay-edge/test/object-store-schema.test.ts index 8815e38160a..ab71f931383 100644 --- a/cloud/apps/relay-edge/test/object-store-schema.test.ts +++ b/cloud/apps/relay-edge/test/object-store-schema.test.ts @@ -1,29 +1,9 @@ -import { readFileSync, writeFileSync } from 'node:fs' import { expect, it } from 'vitest' import { OBJECT_STORE_SCHEMA } from '../src/object-store-schema.generated' import { relayObjectStoreSchema } from './object-store-schema' -const FILE = new URL('../src/object-store-schema.generated.ts', import.meta.url) - it('object store schema matches the relay schema', async () => { - const expected = await relayObjectStoreSchema() - if (process.env.UPDATE_OBJECT_STORE_SCHEMA === '1') { - const body = `// GENERATED from the relay's SQLite schema (sqlite_master) by test/object-store-schema.test.ts.\n// Do not edit; regenerate with UPDATE_OBJECT_STORE_SCHEMA=1.\nexport const OBJECT_STORE_SCHEMA: readonly string[] = ${JSON.stringify(expected, null, 2)}\n` - if (readFileSync(FILE, 'utf8') !== body) { writeFileSync(FILE, body) } - return - } - expect([...OBJECT_STORE_SCHEMA]).toEqual(expected) -}) - -// Bump OBJECT_SCHEMA_VERSION, add its MIGRATIONS step, then update this pin. -const PINNED = { version: 1, ddlSha256: 'd4738f7d9d402cb8066ec99799e5ebe9dafd09621aba0151434e23741087828f' } - -it('object tables change only with a schema version bump', async () => { - const { createHash } = await import('node:crypto') - const { OBJECT_DDL, OBJECT_SCHEMA_VERSION, MIGRATIONS } = await import('../src/object-store') - const ddlSha256 = createHash('sha256').update(JSON.stringify(OBJECT_DDL)).digest('hex') - expect({ version: OBJECT_SCHEMA_VERSION, ddlSha256 }).toEqual(PINNED) - for (let v = 2; v <= OBJECT_SCHEMA_VERSION; v++) { expect(MIGRATIONS[v], `migration to v${v}`).toBeDefined() } + expect([...OBJECT_STORE_SCHEMA]).toEqual(await relayObjectStoreSchema()) }) it('stamps fresh objects, keeps pre-version ones, refuses objects newer than the code', async () => { diff --git a/cloud/apps/relay-edge/test/object-store-schema.ts b/cloud/apps/relay-edge/test/object-store-schema.ts index da9cd052504..eefe250b227 100644 --- a/cloud/apps/relay-edge/test/object-store-schema.ts +++ b/cloud/apps/relay-edge/test/object-store-schema.ts @@ -1,8 +1,8 @@ // The credential store's tables as the relay creates them: read back from an in-memory relay // database (schema plus migrations applied), so nothing is copied by hand. The Worker cannot import // the relay's database module (pg, node:sqlite), so src/object-store-schema.generated.ts holds the -// result and test/object-store-schema.test.ts fails if it drifts. Regenerate: -// UPDATE_OBJECT_STORE_SCHEMA=1 ./node_modules/.bin/vitest run test/object-store-schema.test.ts +// result. Regenerate: +// UPDATE_OBJECT_STORE_SCHEMA=1 ./node_modules/.bin/tsx test/generate-object-store-schema.ts import { openInMemoryRelayDatabase } from '../../relay/src/database/database' export const OBJECT_STORE_TABLES = [ diff --git a/cloud/apps/relay-edge/test/preauth.test.ts b/cloud/apps/relay-edge/test/preauth.test.ts deleted file mode 100644 index 0bac05136d2..00000000000 --- a/cloud/apps/relay-edge/test/preauth.test.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { describe, expect, it, vi } from 'vitest' -import { admitPreAuth, sourceKey } from '../src/preauth' -import type { Env } from '../src/env' - -const req = (ip?: string) => new Request('https://edge.test/v1/connect/x', { headers: ip ? { 'cf-connecting-ip': ip } : {} }) - -describe('pre-auth source key', () => { - it('keys IPv4 by address and IPv6 by /64, in any spelling', () => { - expect(sourceKey(req('203.0.113.9'))).toBe('203.0.113.9') - expect(sourceKey(req('2001:db8:0:1::5'))).toBe('2001:db8:0:1::/64') - expect(sourceKey(req('2001:0DB8:0000:0001:aaaa:bbbb:cccc:dddd'))).toBe('2001:db8:0:1::/64') - expect(sourceKey(req('2001:db8::1'))).toBe('2001:db8:0:0::/64') - expect(sourceKey(req('::1'))).toBe('0:0:0:0::/64') - expect(sourceKey(req())).toBe('unknown') - }) -}) - -describe('pre-auth limiter', () => { - const env = (limit: RateLimit['limit']) => ({ PREAUTH_LIMITER: { limit } }) as unknown as Env - it('counts phone and data apart per source', async () => { - const limit = vi.fn(async () => ({ success: true })) - await admitPreAuth(env(limit), req('203.0.113.9'), 'phone') - await admitPreAuth(env(limit), req('203.0.113.9'), 'data') - expect(limit.mock.calls).toEqual([[{ key: 'phone:203.0.113.9' }], [{ key: 'data:203.0.113.9' }]]) - }) - it('refuses over the limit, fails open when the limiter errors, and passes without a binding', async () => { - expect(await admitPreAuth(env(async () => ({ success: false })), req('203.0.113.9'), 'phone')).toBe(false) - expect(await admitPreAuth(env(async () => { throw new Error('down') }), req('203.0.113.9'), 'phone')).toBe(true) - expect(await admitPreAuth({} as Env, req('203.0.113.9'), 'phone')).toBe(true) - }) -}) diff --git a/cloud/apps/relay-edge/test/roundtrip.ts b/cloud/apps/relay-edge/test/roundtrip.ts index a05c5b1fdfd..f9d2f3cb503 100644 --- a/cloud/apps/relay-edge/test/roundtrip.ts +++ b/cloud/apps/relay-edge/test/roundtrip.ts @@ -80,7 +80,6 @@ async function main(): Promise { const e2 = Number(toEdge.body.assignmentEpoch) record('2 director answers the edge with a newer epoch', toEdge.status === 200 && toEdge.body.cellUrl === EDGE_URL && e2 > e1, JSON.stringify({ status: toEdge.status, cellUrl: toEdge.body.cellUrl, epoch: e2, body: toEdge.status === 200 ? undefined : toEdge.body })) const onEdge = await online(EDGE_URL, e2) - record('2 desktop hello accepted on the edge at that epoch', true) record('2 phone A (paired on the cell) resumes on the edge', resumed(await phone(EDGE_URL, tokenA))) const oldCell = await phone(CELL, tokenA) await waitFor(() => oldCell.state.closed !== null, 4000) diff --git a/cloud/apps/relay/src/edge-relay-app.test.ts b/cloud/apps/relay/src/edge-relay-app.test.ts index 89d492575c1..601d1a127c3 100644 --- a/cloud/apps/relay/src/edge-relay-app.test.ts +++ b/cloud/apps/relay/src/edge-relay-app.test.ts @@ -16,7 +16,6 @@ import { createRelayApp } from './app.js' const EDGE_URL = 'https://edge.relay.example.test' const COHORT_HOST = 'edgecohorthost01' -const OTHER_HOST = 'gcehostnotinedge' const databases: RelayDatabase[] = [] afterEach(async () => { @@ -105,35 +104,6 @@ async function harness(options: { seeded?: boolean } = {}) { } describe('director routes with the relay edge', () => { - it('answers a cohort host on the edge before any other lane, with the exact wire shape', async () => { - const h = await harness() - const response = await h.assign(COHORT_HOST, { reconnect: true }) - expect(response.status).toBe(200) - const body = (await response.json()) as Record - expect(Object.keys(body).sort()).toEqual(['assignmentEpoch', 'cellUrl', 'lease', 'v']) - expect(body).toMatchObject({ v: 1, cellUrl: EDGE_URL, assignmentEpoch: 1 }) - expect(h.plan).not.toHaveBeenCalled() - expect(h.assignments.assign).not.toHaveBeenCalled() - expect(h.assignments.resolve).not.toHaveBeenCalled() - expect(h.raiseEpochFloor).toHaveBeenCalledWith({ userId: 'user-1', relayHostId: COHORT_HOST }, 1) - // A re-ask keeps the epoch. - expect(await (await h.assign(COHORT_HOST)).json()).toMatchObject({ assignmentEpoch: 1 }) - }) - - it('keeps today\'s path for hosts outside the cohort', async () => { - const h = await harness() - const response = await h.assign(OTHER_HOST) - expect(response.status).toBe(200) - expect(await response.json()).toMatchObject({ cellUrl: 'https://us-c1.relay.example.test' }) - expect(h.plan).toHaveBeenCalled() - expect(h.assignments.assign).toHaveBeenCalled() - }) - - it('keeps today\'s path for everyone until the edge cell is seeded', async () => { - const h = await harness({ seeded: false }) - const response = await h.assign(COHORT_HOST) - expect(await response.json()).toMatchObject({ cellUrl: 'https://us-c1.relay.example.test' }) - }) it('keeps a host still holding units on its old cell there (no 503), moving it on a later ask', async () => { const h = await harness() @@ -151,18 +121,6 @@ describe('director routes with the relay edge', () => { expect(h.assignments.assign).toHaveBeenCalled() }) - it('answers a phone /v1/resolve for an edge host from the edge row, with the exact wire shape', async () => { - const h = await harness() - await h.assign(COHORT_HOST) - const response = await h.resolve(COHORT_HOST) - expect(response.status).toBe(200) - const body = (await response.json()) as Record - expect(Object.keys(body).sort()).toEqual(['assignmentEpoch', 'cellUrl', 'leaseExpiresAt', 'v']) - expect(body).toMatchObject({ v: 1, cellUrl: EDGE_URL, assignmentEpoch: 1 }) - expect(h.assignments.resolve).not.toHaveBeenCalled() - expect(h.assignments.assign).not.toHaveBeenCalled() - }) - it('never moves a host on a phone resolve', async () => { const h = await harness() const response = await h.resolve(COHORT_HOST) diff --git a/cloud/apps/relay/src/edge-relay-control.test.ts b/cloud/apps/relay/src/edge-relay-control.test.ts deleted file mode 100644 index 9ca7f196adc..00000000000 --- a/cloud/apps/relay/src/edge-relay-control.test.ts +++ /dev/null @@ -1,198 +0,0 @@ -import { afterEach, beforeEach, describe, expect, it, vi, type MockInstance } from 'vitest' -import { - EDGE_CONTROL_APPLIED_EVENT, - EDGE_CONTROL_DEFAULTS, - EDGE_CONTROL_OBJECT_NAME, - parseEdgeControl, - startEdgeControl, - type EdgeControlFlags -} from './edge-relay-control.js' -import { edgeCohortBucket, edgeCohortBucketPermille, inEdgeCohort, type EdgeRelayConfig } from './edge-relay-cohort.js' - -function edgeConfig(overrides: Partial = {}): EdgeRelayConfig { - return { - cellUrl: 'https://edge.relay.example.test', - rolloutPercent: 0, - allowHosts: new Set(), - denyHosts: new Set(), - regions: new Set(['us-central1']), - ...overrides - } -} - -function hostId(index: number): string { - return `host${String(index).padStart(12, '0')}` -} - -// Storage answers the stored object (or 404) by generation, like the cell flag channel's tests. -function fakeGoogle() { - const state: { object: { generation: number; body: string } | null; paths: string[] } = { - object: null, - paths: [] - } - const fetchImpl: typeof fetch = async (target) => { - const url = new URL(String(target)) - if (url.hostname === 'metadata.google.internal') { - if (url.pathname.endsWith('/project/project-id')) { return new Response('onorca-cloud') } - return Response.json({ access_token: 'ya29.secret-access-token', expires_in: 3_600 }) - } - state.paths.push(decodeURIComponent(url.pathname)) - const object = state.object - if (!object) { return new Response(null, { status: 404 }) } - if (url.searchParams.get('ifGenerationNotMatch') === String(object.generation)) { - return new Response(null, { status: 304 }) - } - return new Response(object.body, { headers: { 'x-goog-generation': String(object.generation) } }) - } - const store = (generation: number, flags: Record) => { - state.object = { generation, body: JSON.stringify({ v: 1, flags }) } - } - return { state, fetchImpl, store } -} - -describe('edge runtime control object', () => { - let log: MockInstance - let warn: MockInstance - beforeEach(() => { - log = vi.spyOn(console, 'log').mockImplementation(() => {}) - warn = vi.spyOn(console, 'warn').mockImplementation(() => {}) - }) - afterEach(() => { - log.mockRestore() - warn.mockRestore() - }) - - it('parses mode and permille, reports unknown keys, and voids the object on any bad known value', () => { - expect(parseEdgeControl({ v: 1, flags: {} })).toEqual({ flags: { mode: 'on' }, ignoredKeys: [] }) - expect(parseEdgeControl({ v: 1, flags: { mode: 'down', rolloutPermille: 0, cohort: 'x' } })).toEqual({ - flags: { mode: 'down', rolloutPermille: 0 }, - ignoredKeys: ['cohort'] - }) - expect(parseEdgeControl({ v: 1, flags: { rolloutPermille: 1_000 } })).toEqual({ - flags: { mode: 'on', rolloutPermille: 1_000 }, - ignoredKeys: [] - }) - for (const flags of [ - { mode: 'paused' }, - { mode: true }, - { rolloutPermille: 1_001 }, - { rolloutPermille: -1 }, - { rolloutPermille: 2.5 }, - { rolloutPermille: '5' }, - { mode: 'off', rolloutPermille: null } - ]) { - expect(parseEdgeControl({ v: 1, flags })).toBeNull() - } - expect(parseEdgeControl({ v: 2, flags: {} })).toBeNull() - expect(parseEdgeControl({ flags: {} })).toBeNull() - }) - - it('reads director/edge.json, logs each applied change with no ids, and tells listeners', async () => { - const google = fakeGoogle() - const control = startEdgeControl({ fetch: google.fetchImpl, autoStart: false }) - const changes: EdgeControlFlags[] = [] - control.onChange(() => changes.push(control.flags())) - await control.poll() - expect(control.flags()).toEqual(EDGE_CONTROL_DEFAULTS) - expect(google.state.paths.at(-1)).toBe(`/storage/v1/b/onorca-cloud-relay-control/o/${EDGE_CONTROL_OBJECT_NAME}`) - google.store(3, { mode: 'off', rolloutPermille: 25 }) - await control.poll() - // Unchanged object: no second change. - await control.poll() - google.store(4, { mode: 'down', note: 'incident' }) - await control.poll() - // A bad value voids the object: the last applied flags stay. - google.store(5, { mode: 'sideways' }) - await control.poll() - expect(control.flags()).toEqual({ mode: 'down' }) - expect(changes).toEqual([{ mode: 'off', rolloutPermille: 25 }, { mode: 'down' }]) - const applied = log.mock.calls - .map((call) => JSON.parse(String(call[0])) as Record) - .filter((line) => line.event === EDGE_CONTROL_APPLIED_EVENT) - expect(applied).toEqual([ - { event: EDGE_CONTROL_APPLIED_EVENT, generation: 3, flags: { mode: 'off', rolloutPermille: 25 } }, - { event: EDGE_CONTROL_APPLIED_EVENT, generation: 4, flags: { mode: 'down' }, ignoredKeys: ['note'] } - ]) - control.stop() - }) - - it('logs a throwing listener and still applies the flags', async () => { - const google = fakeGoogle() - const control = startEdgeControl({ fetch: google.fetchImpl, autoStart: false }) - control.onChange(() => { - throw new Error('listener_broke') - }) - google.store(2, { mode: 'off' }) - await control.poll() - expect(control.flags()).toEqual({ mode: 'off' }) - expect(warn.mock.calls.map((call) => String(call[0])).join('\n')).toContain('orca_relay_control_flags_listener_failed') - control.stop() - }) -}) - -describe('edge cohort under runtime control', () => { - it('reads the applied flags on every call: off and down empty the cohort, even allowed hosts', () => { - const flags: EdgeControlFlags = { mode: 'on' } - const host = hostId(1) - const config = edgeConfig({ - rolloutPercent: 100, - allowHosts: new Set([hostId(2)]), - allowUsers: new Set(['user-team']), - control: () => flags - }) - expect(inEdgeCohort(config, host, 'us-central1')).toBe(true) - for (const mode of ['off', 'down'] as const) { - flags.mode = mode - expect(inEdgeCohort(config, host, 'us-central1')).toBe(false) - expect(inEdgeCohort(config, hostId(2), 'us-central1')).toBe(false) - expect(inEdgeCohort(config, host, 'us-central1', 'user-team')).toBe(false) - } - flags.mode = 'on' - expect(inEdgeCohort(config, hostId(2), 'us-central1')).toBe(true) - // A permille replaces the env percent; deny, allow and region rules still apply around it. - flags.rolloutPermille = 0 - expect(inEdgeCohort(config, host, 'us-central1')).toBe(false) - expect(inEdgeCohort(config, hostId(2), 'us-central1')).toBe(true) - flags.rolloutPermille = 1_000 - expect(inEdgeCohort(config, host, 'us-central1')).toBe(true) - expect(inEdgeCohort(config, host, 'asia-east2')).toBe(false) - expect(inEdgeCohort({ ...config, denyHosts: new Set([host]) }, host, 'us-central1')).toBe(false) - }) - - it('keeps every host of P percent in at P × 10 permille, for every P', () => { - // floor(permille bucket / 10) is the percent bucket, so `bucket < P × 10` iff `bucket < P`. - const hosts = Array.from({ length: 20_000 }, (_, index) => hostId(index)) - expect(hosts.filter((host) => Math.floor(edgeCohortBucketPermille(host) / 10) !== edgeCohortBucket(host))).toEqual([]) - const sample = hosts.slice(0, 2_000) - const moved: string[] = [] - for (let percent = 0; percent <= 100; percent += 1) { - const byPercent = edgeConfig({ rolloutPercent: percent }) - const byPermille = edgeConfig({ rolloutPercent: 0, control: () => ({ mode: 'on', rolloutPermille: percent * 10 }) }) - for (const host of sample) { - if (inEdgeCohort(byPercent, host, 'us-central1') !== inEdgeCohort(byPermille, host, 'us-central1')) { - moved.push(`${percent}:${host}`) - } - } - } - expect(moved).toEqual([]) - // The env percent alone (boot, no permille flag) answers as before. - expect(sample.filter((host) => inEdgeCohort(edgeConfig({ rolloutPercent: 10 }), host, 'us-central1'))) - .toEqual(sample.filter((host) => edgeCohortBucket(host) < 10)) - }) - - it('steps in 1 permille shares, nested, roughly evenly', () => { - const hosts = Array.from({ length: 20_000 }, (_, index) => hostId(index)) - const share = (permille: number) => - hosts.filter((host) => inEdgeCohort(edgeConfig({ control: () => ({ mode: 'on', rolloutPermille: permille }) }), host, 'us-central1')) - const one = share(1) - const five = share(5) - expect(one.length).toBeGreaterThan(5) - expect(one.length).toBeLessThan(40) - expect(five.length).toBeGreaterThan(70) - expect(five.length).toBeLessThan(130) - // A larger share keeps every host of a smaller one. - expect(one.every((host) => five.includes(host))).toBe(true) - const buckets = new Set(hosts.map((host) => edgeCohortBucketPermille(host))) - expect(buckets.size).toBe(1_000) - }) -}) diff --git a/cloud/apps/relay/src/edge-relay-evacuation.test.ts b/cloud/apps/relay/src/edge-relay-evacuation.test.ts index 20e503bbce5..9b671710f28 100644 --- a/cloud/apps/relay/src/edge-relay-evacuation.test.ts +++ b/cloud/apps/relay/src/edge-relay-evacuation.test.ts @@ -9,14 +9,7 @@ import { type EdgeAdmin, type EdgeDrainResult } from './edge-admin-client.js' -import { - EDGE_EVACUATION_BATCH, - EDGE_EVACUATION_BUSY_INTERVAL_MS, - EDGE_EVACUATION_CONCURRENCY, - EDGE_EVACUATION_CONTROL_DELAY_MS, - EDGE_EVACUATION_INTERVAL_MS, - EdgeEvacuation -} from './edge-relay-evacuation.js' +import { EDGE_EVACUATION_BUSY_INTERVAL_MS, EDGE_EVACUATION_CONCURRENCY, EDGE_EVACUATION_CONTROL_DELAY_MS, EdgeEvacuation } from './edge-relay-evacuation.js' import { EDGE_CELL_ID, type EdgeRelayConfig } from './edge-relay.js' import type { EdgeControlFlags } from './edge-relay-control.js' @@ -94,106 +87,7 @@ async function harness(overrides: Partial = {}, random = 0.5) { return { database, config, admin, clock, evacuation, insert, prefer } } -describe('edge evacuation sweep', () => { - it('drains only edge rows whose host left the cohort, and logs the outcome', async () => { - const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}) - const h = await harness({ allowHosts: new Set([hostId(1)]), denyHosts: new Set([hostId(2)]) }) - await h.insert(hostId(1)) - await h.insert(hostId(2)) - await h.insert(hostId(3), 'us-c1') - expect(await h.evacuation.sweep()).toBe(1) - expect(h.admin.drained).toEqual([hostId(2)]) - const events = warn.mock.calls.map(([line]) => JSON.parse(String(line)) as Record) - expect(events).toEqual([{ event: 'orca_relay_edge_drain_sent', outcome: 'drained', controls: 1 }]) - }) - - it('reads the region from the stored preference, else the default region', async () => { - vi.spyOn(console, 'warn').mockImplementation(() => {}) - const h = await harness({ allowUsers: new Set(['user-team']) }) - await h.insert(hostId(1), EDGE_CELL_ID, 'user-team') - await h.insert(hostId(2), EDGE_CELL_ID, 'user-team') - await h.insert(hostId(3), EDGE_CELL_ID, 'user-team') - await h.prefer(hostId(1), 'asia-east2', 'user-team') - await h.prefer(hostId(2), 'us-central1', 'user-team') - await h.evacuation.sweep() - expect(h.admin.drained).toEqual([hostId(1)]) - }) - - it(`drains at most ${EDGE_EVACUATION_BATCH} per tick, the rest on the next, and not again within the cooldown`, async () => { - vi.spyOn(console, 'warn').mockImplementation(() => {}) - const h = await harness() - for (let index = 0; index < EDGE_EVACUATION_BATCH + 25; index += 1) { await h.insert(hostId(index)) } - h.admin.answer = { outcome: 'idle' } - expect(await h.evacuation.sweep()).toBe(EDGE_EVACUATION_BATCH) - expect(await h.evacuation.sweep()).toBe(25) - expect(new Set(h.admin.drained).size).toBe(EDGE_EVACUATION_BATCH + 25) - expect(await h.evacuation.sweep()).toBe(0) - h.clock.now += 10 * 60_000 - expect(await h.evacuation.sweep()).toBe(EDGE_EVACUATION_BATCH) - }) - - it('logs an unavailable edge and carries on with the next host, retrying it later', async () => { - const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}) - const h = await harness() - await h.insert(hostId(1)) - await h.insert(hostId(2)) - h.admin.unavailable.add(hostId(1)) - expect(await h.evacuation.sweep()).toBe(1) - expect(h.admin.drained).toEqual([hostId(2)]) - expect(JSON.parse(String(warn.mock.calls[0]![0]))).toEqual({ - event: 'orca_relay_edge_drain_failed', - reason: 'relay_edge_admin_unavailable:status_404' - }) - h.admin.unavailable.clear() - expect(await h.evacuation.sweep()).toBe(1) - expect(h.admin.drained).toEqual([hostId(2), hostId(1)]) - }) - - it('runs on a jittered timer from start until stop', async () => { - vi.useFakeTimers() - vi.spyOn(console, 'warn').mockImplementation(() => {}) - const h = await harness() - await h.insert(hostId(1)) - const sweep = vi.spyOn(h.evacuation, 'sweep') - h.evacuation.start() - // First pass soon after start, then the slow interval once nothing more is queued. - await vi.advanceTimersByTimeAsync(EDGE_EVACUATION_BUSY_INTERVAL_MS - 1) - expect(sweep).not.toHaveBeenCalled() - await vi.advanceTimersByTimeAsync(1) - expect(sweep).toHaveBeenCalledTimes(1) - await vi.advanceTimersByTimeAsync(0) - expect(h.admin.drained).toEqual([hostId(1)]) - await vi.advanceTimersByTimeAsync(EDGE_EVACUATION_BUSY_INTERVAL_MS) - expect(sweep).toHaveBeenCalledTimes(1) - await vi.advanceTimersByTimeAsync(EDGE_EVACUATION_INTERVAL_MS - EDGE_EVACUATION_BUSY_INTERVAL_MS) - expect(sweep).toHaveBeenCalledTimes(2) - h.evacuation.stop() - await vi.advanceTimersByTimeAsync(10 * EDGE_EVACUATION_INTERVAL_MS) - expect(sweep).toHaveBeenCalledTimes(2) - }) -}) - describe('edge evacuation under runtime control', () => { - it('re-arms an idle sweep within 5 s of a control change, so a rollback drains at once', async () => { - vi.useFakeTimers() - vi.spyOn(console, 'warn').mockImplementation(() => {}) - const flags: EdgeControlFlags = { mode: 'on' } - const h = await harness({ rolloutPercent: 100, control: () => flags }) - await h.insert(hostId(1)) - const sweep = vi.spyOn(h.evacuation, 'sweep') - h.evacuation.start() - await vi.advanceTimersByTimeAsync(EDGE_EVACUATION_BUSY_INTERVAL_MS) - expect(sweep).toHaveBeenCalledTimes(1) - expect(h.admin.drained).toEqual([]) - // Idle now: the next pass would be ~10 min away. - await vi.advanceTimersByTimeAsync(60_000) - flags.mode = 'off' - h.evacuation.controlChanged() - await vi.advanceTimersByTimeAsync(EDGE_EVACUATION_CONTROL_DELAY_MS) - expect(sweep).toHaveBeenCalledTimes(2) - expect(h.admin.drained).toEqual([hostId(1)]) - h.evacuation.stop() - }) it('re-arms after a pass that was running when the control changed', async () => { vi.useFakeTimers() @@ -233,37 +127,6 @@ describe('edge evacuation under runtime control', () => { expect(await pass).toBe(35) expect(most).toBe(EDGE_EVACUATION_CONCURRENCY) }) - - it('starts each pass at a random point of the keys and wraps around to it', async () => { - vi.spyOn(console, 'warn').mockImplementation(() => {}) - const users = ['20000000-a', '60000000-a', 'a0000000-a', 'e0000000-a'] - const drainedFrom = async (random: number) => { - const h = await harness({}, random) - for (const [index, user] of users.entries()) { await h.insert(hostId(index), EDGE_CELL_ID, user) } - expect(await h.evacuation.sweep()).toBe(4) - return h.admin.drained - } - expect(await drainedFrom(0.5)).toEqual([hostId(2), hostId(3), hostId(0), hostId(1)]) - expect(await drainedFrom(0.1)).toEqual([hostId(0), hostId(1), hostId(2), hostId(3)]) - expect(await drainedFrom(0.3)).toEqual([hostId(1), hostId(2), hostId(3), hostId(0)]) - }) - - it('pages an idle pass: one page of edge rows per tick, then idle until the next pass', async () => { - vi.spyOn(console, 'warn').mockImplementation(() => {}) - const h = await harness({ rolloutPercent: 100 }) - for (let index = 0; index < 1_200; index += 1) { await h.insert(hostId(index)) } - const query = vi.spyOn(h.database, 'query') - const pages = () => query.mock.calls.filter(([sql]) => String(sql).includes('FROM relay_assignments a')).length - const busy = () => (h.evacuation as unknown as { busy: boolean }).busy - await h.evacuation.sweep() - expect([pages(), busy()]).toEqual([1, true]) - await h.evacuation.sweep() - expect([pages(), busy()]).toEqual([2, true]) - // The short last page wraps to the start of the keys, which holds nothing before the origin. - await h.evacuation.sweep() - expect([pages(), busy()]).toEqual([4, false]) - expect(h.admin.drained).toEqual([]) - }) }) describe('edge admin drain call', () => { @@ -306,16 +169,4 @@ describe('edge admin drain call', () => { bodySha256: createHash('sha256').update(String(first.init.body)).digest('base64url') }) }) - - it('treats a network failure as unavailable', async () => { - const admin = createEdgeAdmin({ - cellUrl: EDGE_URL, - signingKey: SIGNING_KEY, - issuer: 'https://relay.example.test', - fetch: async () => { - throw new TypeError('fetch failed') - } - }) - await expect(admin.drain(hostId(1))).rejects.toThrow('relay_edge_admin_unavailable:network') - }) }) diff --git a/cloud/apps/relay/src/edge-relay-fence.test.ts b/cloud/apps/relay/src/edge-relay-fence.test.ts index 3fa7bbc0cb1..abdef6a01ba 100644 --- a/cloud/apps/relay/src/edge-relay-fence.test.ts +++ b/cloud/apps/relay/src/edge-relay-fence.test.ts @@ -1,36 +1,10 @@ -import { jwtVerify } from 'jose' import { afterEach, describe, expect, it, vi } from 'vitest' import { openInMemoryRelayDatabase, type RelayDatabase } from './database/database.js' -import { - EDGE_ADMIN_AUDIENCE, - EDGE_ADMIN_LOCKED_TIMEOUT_MS, - EdgeAdminUnavailableError, - createEdgeAdmin, - type EdgeAdmin, - type EdgeAdminCallOptions, - type EdgeAssignBody, - type EdgeAssignResult -} from './edge-admin-client.js' -import { EDGE_EVACUATION_BUSY_INTERVAL_MS, EdgeEvacuation } from './edge-relay-evacuation.js' -import { - EDGE_FENCE_MAX_ATTEMPTS, - EDGE_FENCE_RETRY_BASE_MS, - EDGE_FENCE_RETRY_BATCH, - EDGE_FENCE_RETRY_MAX_MS, - edgeFenceRetryDelayMs, - recordPendingFence, - retryPendingFences -} from './edge-relay-fence.js' -import { - EDGE_CELL_ID, - EDGE_LOCKED_CALLS_MAX, - EdgeAssignments, - EdgeMoveDeferredError, - type EdgeRelayConfig -} from './edge-relay.js' +import { EdgeAdminUnavailableError, type EdgeAdmin, type EdgeAdminCallOptions, type EdgeAssignBody, type EdgeAssignResult } from './edge-admin-client.js' +import { EDGE_FENCE_RETRY_BASE_MS, EDGE_FENCE_RETRY_BATCH, recordPendingFence, retryPendingFences } from './edge-relay-fence.js' +import { EDGE_CELL_ID, EdgeAssignments, type EdgeRelayConfig } from './edge-relay.js' const EDGE_URL = 'https://edge.relay.example.test' -const SIGNING_KEY = new TextEncoder().encode('edge-admin-key-with-at-least-32-bytes!!') const USER = 'user-1' const HOST = 'fencehost0000001' const T0 = 1_700_000_000_000 @@ -123,29 +97,6 @@ function logged(spy: { mock: { calls: unknown[][] } }, event: string): Record { - it('treats a stale answer as a failed fence: logged with its outcome, recorded, not retried inline', async () => { - const error = vi.spyOn(console, 'error').mockImplementation(() => {}) - const h = await harness() - h.edge.answers = ['stale'] - expect(await h.assignments.fenceAway({ userId: USER, relayHostId: HOST }, 'us-c1', 9)).toBe(false) - expect(h.edge.calls).toHaveLength(1) - expect(logged(error, 'orca_relay_edge_fence_failed')).toEqual([ - { event: 'orca_relay_edge_fence_failed', outcome: 'stale', cellId: 'us-c1', assignmentEpoch: 9 } - ]) - expect(await h.pending()).toEqual([ - { relayHostId: HOST, cellId: 'us-c1', assignmentEpoch: 9, attempts: 0, nextAttemptAt: T0 + EDGE_FENCE_RETRY_BASE_MS } - ]) - }) - - it('retries conflict and unavailable inline, then records the last outcome', async () => { - const error = vi.spyOn(console, 'error').mockImplementation(() => {}) - const h = await harness() - h.edge.answers = ['conflict', 'down', 'conflict'] - expect(await h.assignments.fenceAway({ userId: USER, relayHostId: HOST }, 'us-c1', 9)).toBe(false) - expect(h.edge.calls).toHaveLength(3) - expect(logged(error, 'orca_relay_edge_fence_failed')[0]).toMatchObject({ outcome: 'conflict' }) - expect(await h.pending()).toHaveLength(1) - }) it('records nothing when the fence lands, and a newer failure replaces an older record', async () => { vi.spyOn(console, 'error').mockImplementation(() => {}) @@ -163,15 +114,6 @@ describe('relay edge fence after a host leaves', () => { { relayHostId: HOST, cellId: 'us-c2', assignmentEpoch: 12, attempts: 0, nextAttemptAt: h.clock.now + EDGE_FENCE_RETRY_BASE_MS } ]) }) - - it('still answers when the failure cannot be recorded', async () => { - const error = vi.spyOn(console, 'error').mockImplementation(() => {}) - const h = await harness() - await h.database.query(`DROP TABLE relay_edge_fence_pending`) - h.edge.answers = ['stale'] - expect(await h.assignments.fenceAway({ userId: USER, relayHostId: HOST }, 'us-c1', 9)).toBe(false) - expect(logged(error, 'orca_relay_edge_fence_record_failed')).toHaveLength(1) - }) }) describe('relay edge pending fence retries', () => { @@ -195,75 +137,6 @@ describe('relay edge pending fence retries', () => { ]) }) - it('drops the record without a call when the host is back on the edge or has no row', async () => { - vi.spyOn(console, 'error').mockImplementation(() => {}) - const h = await harness() - h.edge.answers = ['stale', 'stale'] - await h.assignments.fenceAway({ userId: USER, relayHostId: HOST }, 'us-c1', 9) - await h.assignments.fenceAway({ userId: USER, relayHostId: 'fencehost0000002' }, 'us-c1', 4) - await h.placeRow(EDGE_CELL_ID, 12) - h.edge.calls = [] - h.clock.now += EDGE_FENCE_RETRY_BASE_MS - expect(await h.retry()).toBe(false) - expect(h.edge.calls).toEqual([]) - expect(await h.pending()).toEqual([]) - }) - - it('backs off on each failure, logging it, and gives up after the last attempt', async () => { - const error = vi.spyOn(console, 'error').mockImplementation(() => {}) - const h = await harness() - await h.placeRow('us-c1', 9) - h.edge.answers = ['stale'] - await h.assignments.fenceAway({ userId: USER, relayHostId: HOST }, 'us-c1', 9) - h.clock.now += EDGE_FENCE_RETRY_BASE_MS - for (let attempt = 1; attempt < EDGE_FENCE_MAX_ATTEMPTS; attempt += 1) { - h.edge.answers = [attempt % 2 === 0 ? 'down' : 'conflict'] - expect(await h.retry()).toBe(true) - expect(await h.pending()).toMatchObject([{ attempts: attempt, nextAttemptAt: h.clock.now + edgeFenceRetryDelayMs(attempt) }]) - // Not due yet: nothing is sent. - h.clock.now += edgeFenceRetryDelayMs(attempt) - 1 - const calls = h.edge.calls.length - await h.retry() - expect(h.edge.calls).toHaveLength(calls) - h.clock.now += 1 - } - h.edge.answers = ['stale'] - expect(await h.retry()).toBe(false) - expect(await h.pending()).toEqual([]) - const failures = logged(error, 'orca_relay_edge_fence_failed').filter((entry) => entry.retry === true) - expect(failures).toHaveLength(EDGE_FENCE_MAX_ATTEMPTS) - expect(failures[1]).toEqual({ - event: 'orca_relay_edge_fence_failed', - outcome: 'relay_edge_admin_unavailable:network', - cellId: 'us-c1', - assignmentEpoch: 9, - attempts: 2, - retry: true - }) - expect(logged(error, 'orca_relay_edge_fence_abandoned')).toEqual([ - { - event: 'orca_relay_edge_fence_abandoned', - outcome: 'stale', - cellId: 'us-c1', - assignmentEpoch: 9, - attempts: EDGE_FENCE_MAX_ATTEMPTS - } - ]) - expect(edgeFenceRetryDelayMs(1)).toBe(60_000) - expect(edgeFenceRetryDelayMs(EDGE_FENCE_MAX_ATTEMPTS)).toBe(30 * 60_000) - }) - - it('keeps retrying a fence through more than a day of edge outage', () => { - // Recorded at 0, first retry one base delay later, then each attempt's backoff. - let lastAttemptAt = EDGE_FENCE_RETRY_BASE_MS - for (let attempt = 1; attempt < EDGE_FENCE_MAX_ATTEMPTS; attempt += 1) { - lastAttemptAt += edgeFenceRetryDelayMs(attempt) - } - expect(lastAttemptAt).toBeGreaterThanOrEqual(24 * 60 * 60_000) - // Never slower than the cap, so the edge is not left claiming hosts long after it is back. - expect(edgeFenceRetryDelayMs(EDGE_FENCE_MAX_ATTEMPTS)).toBe(EDGE_FENCE_RETRY_MAX_MS) - }) - it(`retries up to ${EDGE_FENCE_RETRY_BATCH} due fences per pass, 10 in flight at once`, async () => { vi.spyOn(console, 'warn').mockImplementation(() => {}) const h = await harness() @@ -303,120 +176,4 @@ describe('relay edge pending fence retries', () => { await Promise.all([h.retry(), h.retry()]) expect(h.edge.calls).toHaveLength(1) }) - - it('runs from the evacuation sweep, which keeps its busy cadence while fences are pending', async () => { - vi.useFakeTimers() - vi.spyOn(console, 'warn').mockImplementation(() => {}) - vi.spyOn(console, 'error').mockImplementation(() => {}) - const h = await harness() - await h.placeRow('us-c1', 9) - const config = { ...edgeConfig(), rolloutPercent: 0 } - const evacuation = new EdgeEvacuation(h.database, config, h.edge, EDGE_CELL_ID, () => h.clock.now, () => 0.5) - h.edge.answers = ['stale'] - await h.assignments.fenceAway({ userId: USER, relayHostId: HOST }, 'us-c1', 9) - h.edge.calls = [] - h.edge.answers = ['down'] - const sweep = vi.spyOn(evacuation, 'sweep') - evacuation.start() - h.clock.now += EDGE_EVACUATION_BUSY_INTERVAL_MS - await vi.advanceTimersByTimeAsync(EDGE_EVACUATION_BUSY_INTERVAL_MS) - expect(h.edge.calls).toHaveLength(1) - // Still pending: the next pass comes after the busy interval, and lands. - h.clock.now += EDGE_EVACUATION_BUSY_INTERVAL_MS - await vi.advanceTimersByTimeAsync(EDGE_EVACUATION_BUSY_INTERVAL_MS) - expect(sweep).toHaveBeenCalledTimes(2) - expect(h.edge.calls).toHaveLength(2) - expect(await h.pending()).toEqual([]) - evacuation.stop() - }) -}) - -describe('relay edge pushes under the row lock', () => { - // SQLite serializes transactions; run them inline so moves overlap as they do on Postgres. - function overlapping(database: RelayDatabase): RelayDatabase { - return new Proxy(database, { - get(target, property) { - if (property === 'transaction') { - return async (operation: (transaction: RelayDatabase) => Promise) => await operation(target) - } - const value = Reflect.get(target, property, target) as unknown - return typeof value === 'function' ? value.bind(target) : value - } - }) - } - - it(`defers a move once ${EDGE_LOCKED_CALLS_MAX} pushes are in flight, with a ${EDGE_ADMIN_LOCKED_TIMEOUT_MS} ms timeout`, async () => { - const h = await harness() - const assignments = new EdgeAssignments(overlapping(h.database), edgeConfig(), () => h.clock.now, h.edge) - await assignments.ensureEdgeCell() - h.edge.answers = Array.from({ length: EDGE_LOCKED_CALLS_MAX }, () => 'hang' as const) - const hostIds = Array.from({ length: EDGE_LOCKED_CALLS_MAX + 1 }, (_, index) => `fencecap${String(index).padStart(8, '0')}`) - for (const relayHostId of hostIds) { await h.placeRow('us-c1', 3, relayHostId) } - const held = hostIds.slice(0, EDGE_LOCKED_CALLS_MAX).map((relayHostId) => assignments.moveToEdge({ userId: USER, relayHostId }, 0)) - await vi.waitFor(() => expect(h.edge.calls).toHaveLength(EDGE_LOCKED_CALLS_MAX)) - const last = { userId: USER, relayHostId: hostIds.at(-1)! } - const deferred = await assignments.moveToEdge(last, 0).catch((error: unknown) => error) - expect(deferred).toBeInstanceOf(EdgeMoveDeferredError) - expect((deferred as Error).message).toBe('relay_edge_move_deferred:edge_busy') - // Not waited for and not sent. - expect(h.edge.calls).toHaveLength(EDGE_LOCKED_CALLS_MAX) - h.edge.release() - await Promise.all(held) - for (const call of h.edge.calls) { expect(call.options).toEqual({ timeoutMs: EDGE_ADMIN_LOCKED_TIMEOUT_MS }) } - // Slots are freed: the deferred host moves on its next ask. - expect((await assignments.moveToEdge(last, 0)).cellId).toBe(EDGE_CELL_ID) - }) -}) - -describe('edge admin tokens and timeouts', () => { - it('signs every call with a fresh 128-bit jti', async () => { - const tokens: string[] = [] - const admin = createEdgeAdmin({ - cellUrl: EDGE_URL, - signingKey: SIGNING_KEY, - issuer: 'https://relay.example.test', - fetch: async (url, init) => { - tokens.push(String((init!.headers as Record).authorization).replace('Bearer ', '')) - const op = String(url).split('/').at(-1) - const body = - op === 'assign' - ? { outcome: 'applied', assignmentEpoch: 1 } - : op === 'export' - ? { outcome: 'exported', assignment: null, devices: [] } - : { outcome: 'idle' } - return new Response(JSON.stringify(body), { status: 200 }) - } - }) - await admin.assign({ userId: USER, relayHostId: HOST, cellId: 'us-c1', assignmentEpoch: 1 }) - await admin.assign({ userId: USER, relayHostId: HOST, cellId: 'us-c1', assignmentEpoch: 1 }) - await admin.exportHost(HOST) - await admin.drain(HOST) - const ids = await Promise.all( - tokens.map(async (token) => { - const { payload } = await jwtVerify(token, SIGNING_KEY, { audience: EDGE_ADMIN_AUDIENCE }) - return String(payload.jti) - }) - ) - for (const id of ids) { - expect(id).toMatch(/^[A-Za-z0-9_-]{22}$/) - expect(Buffer.from(id, 'base64url')).toHaveLength(16) - } - expect(new Set(ids).size).toBe(ids.length) - }) - - it('times out pushes and exports at 2 s, other calls at 3 s', async () => { - const timeout = vi.spyOn(AbortSignal, 'timeout') - const admin = createEdgeAdmin({ - cellUrl: EDGE_URL, - signingKey: SIGNING_KEY, - issuer: 'https://relay.example.test', - fetch: async () => new Response(JSON.stringify({ outcome: 'busy' }), { status: 409 }) - }) - const body = { userId: USER, relayHostId: HOST, cellId: 'us-c1', assignmentEpoch: 1 } - await admin.assign(body, { timeoutMs: EDGE_ADMIN_LOCKED_TIMEOUT_MS }).catch(() => undefined) - await admin.assign(body).catch(() => undefined) - await admin.exportHost(HOST) - await admin.drain(HOST).catch(() => undefined) - expect(timeout.mock.calls.map(([ms]) => ms)).toEqual([2_000, 3_000, 2_000, 3_000]) - }) }) diff --git a/cloud/apps/relay/src/edge-relay-invite-ws.test.ts b/cloud/apps/relay/src/edge-relay-invite-ws.test.ts index be39864c696..98ac1d84675 100644 --- a/cloud/apps/relay/src/edge-relay-invite-ws.test.ts +++ b/cloud/apps/relay/src/edge-relay-invite-ws.test.ts @@ -83,22 +83,3 @@ it('moves an invite of a host on the relay edge to the edge, never asking the ce expect(resolve).not.toHaveBeenCalled() expect(assign).not.toHaveBeenCalled() }) - -it('falls through to the cell paths for an invite of a host that is not on the edge', async () => { - const { relay, port } = await director() - const identity = { userId: 'user-1', relayHostId: HOST } - vi.spyOn(relay.store, 'resolveInviteForMove').mockResolvedValue({ ...identity, relayDeviceId: 'device-1' }) - vi.spyOn(EdgeAssignments.prototype, 'edgeAssignment').mockResolvedValue(null) - vi.spyOn(relay.assignments, 'resolve').mockResolvedValue({ - ...identity, - cellId: 'cell-1', - cellUrl: 'https://cell-1.example.test', - assignmentEpoch: 3, - leaseExpiresAt: Date.now() + 60_000 - }) - const { messages, code } = await inviteMove(port) - expect(messages).toEqual([ - { type: 'relay-moved', v: 1, cellUrl: 'https://cell-1.example.test', assignmentEpoch: 3 } - ]) - expect(code).toBe(RELAY_CLOSE_CODE.DRAINING) -}) diff --git a/cloud/apps/relay/src/edge-relay-phase2.test.ts b/cloud/apps/relay/src/edge-relay-phase2.test.ts index 455027e74be..d4148ed4002 100644 --- a/cloud/apps/relay/src/edge-relay-phase2.test.ts +++ b/cloud/apps/relay/src/edge-relay-phase2.test.ts @@ -191,20 +191,6 @@ async function keepFailing(h: { clock: { now: number }; assign: () => Promise { - it('pushes the host and its Postgres devices before answering with the edge, with a location hint', async () => { - const h = await harness() - await h.insertDevice(device('phone-a')) - const response = await h.assign('asia-east2') - expect(response.status).toBe(200) - expect(await response.json()).toMatchObject({ cellUrl: EDGE_URL, assignmentEpoch: 1 }) - expect(h.fake.pushes).toHaveLength(1) - expect(h.fake.pushes[0]).toMatchObject({ cellId: EDGE_CELL_ID, assignmentEpoch: 1, locationHint: 'apac' }) - expect(h.fake.devices.map((d) => d.relay_device_id)).toEqual(['phone-a']) - expect(h.fake.devices[0]!.current_version).toBe(1) - // A re-ask of a host already on the edge does not push again. - await h.assign() - expect(h.fake.pushes).toHaveLength(1) - }) it('keeps the host on today\'s path, with no edge row, when the edge cannot take the push', async () => { const h = await harness() @@ -215,46 +201,6 @@ describe('relay edge phase 2: the director moves host data with the host', () => expect(await h.row()).toBeUndefined() }) - it('re-mints above an epoch the edge already holds from a move that never committed', async () => { - const h = await harness() - h.fake.assignment = { userId: USER, cellId: 'us-c1', assignmentEpoch: 7 } - const response = await h.assign() - expect(await response.json()).toMatchObject({ cellUrl: EDGE_URL, assignmentEpoch: 8 }) - expect(await h.row()).toMatchObject({ cell_id: EDGE_CELL_ID }) - expect(Number((await h.row())!.assignment_epoch)).toBe(8) - }) - - it('takes devices back before placing the host elsewhere, then fences the edge', async () => { - const h = await harness() - await h.insertDevice(device('phone-a')) - await h.insertDevice(device('phone-b')) - await h.assign() - // On the edge: phone-a is revoked, phone-b removed, phone-c paired. - h.fake.devices = [device('phone-a', { revoked_at: 1_800_000_000_000 }), device('phone-c')] - h.leaveCohort() - const response = await h.assign() - expect(response.status).toBe(200) - expect(await response.json()).toMatchObject({ cellUrl: 'https://us-c1.relay.example.test', assignmentEpoch: 9 }) - expect(h.fake.exports).toBe(1) - expect(await h.postgresDevices()).toEqual([ - { id: 'phone-a', revoked: 1_800_000_000_000 }, - { id: 'phone-c', revoked: null } - ]) - expect(h.fake.pushes.at(-1)).toEqual({ userId: USER, relayHostId: HOST, cellId: 'us-c1', assignmentEpoch: 9 }) - expect(h.fake.assignment).toMatchObject({ cellId: 'us-c1', assignmentEpoch: 9 }) - }) - - it('tells the desktop to retry while it is still connected to the edge, placing nothing', async () => { - const h = await harness() - await h.assign() - h.fake.connected = true - h.leaveCohort() - const response = await h.assign() - expect(response.status).toBe(503) - expect(response.headers.get('retry-after')).toBe('2') - expect(h.assignments.assign).not.toHaveBeenCalled() - }) - it('503s a leaving host while the edge export is unavailable, until it has failed for 120 s', async () => { const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}) vi.spyOn(console, 'error').mockImplementation(() => {}) @@ -288,61 +234,6 @@ describe('relay edge phase 2: the director moves host data with the host', () => }) }) - it('restarts the unavailable clock after any usable export answer', async () => { - vi.spyOn(console, 'warn').mockImplementation(() => {}) - const h = await harness() - await h.assign() - h.leaveCohort() - h.fake.down = true - expect((await h.assign()).status).toBe(503) - h.clock.now += 100_000 - // The edge answers (busy: the desktop is still connected): that is a success. - h.fake.down = false - h.fake.connected = true - expect((await h.assign()).status).toBe(503) - h.fake.down = true - h.clock.now += 20_000 - expect((await h.assign()).status).toBe(503) - await keepFailing(h, EDGE_EXPORT_SKIP_AFTER_MS - 1) - expect((await h.assign()).status).toBe(503) - h.clock.now += 1 - expect((await h.assign()).status).toBe(200) - }) - - it('does not count failures further apart than 30 s as one outage', async () => { - vi.spyOn(console, 'warn').mockImplementation(() => {}) - const h = await harness() - await h.assign() - h.leaveCohort() - h.fake.down = true - expect((await h.assign()).status).toBe(503) - // One old failure, then quiet for longer than the skip threshold: the next failure starts over. - h.clock.now += EDGE_EXPORT_SKIP_AFTER_MS + 1 - expect((await h.assign()).status).toBe(503) - await keepFailing(h, EDGE_EXPORT_SKIP_AFTER_MS - 1) - expect((await h.assign()).status).toBe(503) - h.clock.now += 1 - expect((await h.assign()).status).toBe(200) - }) - - it('skips the export on the first unavailable answer with a 0 ms threshold, and fences when the edge answers', async () => { - vi.spyOn(console, 'warn').mockImplementation(() => {}) - const h = await harness('host', { exportSkipAfterMs: 0 }) - await h.insertDevice(device('phone-a')) - await h.assign() - h.leaveCohort() - let exportCalls = 0 - h.fake.exportHost = async () => { - exportCalls += 1 - throw new EdgeAdminUnavailableError('status_503') - } - const response = await h.assign() - expect(response.status).toBe(200) - expect(exportCalls).toBe(1) - expect(await h.postgresDevices()).toEqual([]) - expect(h.fake.assignment).toMatchObject({ cellId: 'us-c1', assignmentEpoch: 9 }) - }) - it('keeps Postgres devices when the edge object holds no assignment (never pushed or erased)', async () => { const h = await harness() await h.insertDevice(device('phone-a')) @@ -365,36 +256,6 @@ describe('relay edge phase 2: the director moves host data with the host', () => expect(await h.postgresDevices()).toEqual([{ id: 'phone-a', revoked: null }]) }) - it('moves every host of an allowed account (relay token sub), with no host list', async () => { - const h = await harness('account') - const response = await h.assign() - expect(await response.json()).toMatchObject({ cellUrl: EDGE_URL }) - expect(h.fake.pushes[0]).toMatchObject({ userId: USER, relayHostId: HOST }) - }) - - it('with mode off or down, sends no new host to the edge', async () => { - for (const mode of ['off', 'down'] as const) { - const h = await harness('host', { control: () => ({ mode }) }) - expect(await (await h.assign()).json()).toMatchObject({ cellUrl: 'https://us-c1.relay.example.test' }) - expect(h.fake.pushes).toHaveLength(0) - expect(await h.row()).toBeUndefined() - } - }) - - it('with mode off, moves hosts on the edge off normally: export, place, fence', async () => { - const flags: EdgeControlFlags = { mode: 'on' } - const h = await harness('host', { control: () => flags }) - await h.insertDevice(device('phone-a')) - expect(await (await h.assign()).json()).toMatchObject({ cellUrl: EDGE_URL }) - h.fake.devices = [device('phone-a', { revoked_at: 1_800_000_000_000 })] - flags.mode = 'off' - expect(await (await h.assign()).json()).toMatchObject({ cellUrl: 'https://us-c1.relay.example.test' }) - // The edge's revoke carries over. - expect(h.fake.exports).toBe(1) - expect(await h.postgresDevices()).toEqual([{ id: 'phone-a', revoked: 1_800_000_000_000 }]) - expect(h.fake.assignment).toMatchObject({ cellId: 'us-c1', assignmentEpoch: 9 }) - }) - it('with mode down, skips the export at once, removes the Postgres devices, and fences once', async () => { const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}) vi.spyOn(console, 'error').mockImplementation(() => {}) @@ -422,12 +283,4 @@ describe('relay edge phase 2: the director moves host data with the host', () => const pending = await h.database.query(`SELECT cell_id, assignment_epoch FROM relay_edge_fence_pending`) expect(pending.map((row) => [String(row.cell_id), Number(row.assignment_epoch)])).toEqual([['us-c1', 9]]) }) - - it('does not touch the edge for hosts that were never on it', async () => { - const h = await harness() - h.leaveCohort() - expect((await h.assign()).status).toBe(200) - expect(h.fake.exports).toBe(0) - expect(h.fake.pushes).toHaveLength(0) - }) }) diff --git a/cloud/apps/relay/src/edge-relay-postgres.test.ts b/cloud/apps/relay/src/edge-relay-postgres.test.ts index aec056d715a..bfffacf8191 100644 --- a/cloud/apps/relay/src/edge-relay-postgres.test.ts +++ b/cloud/apps/relay/src/edge-relay-postgres.test.ts @@ -11,7 +11,6 @@ import { } from './edge-relay.js' import type { EdgeAssignBody, EdgeDeviceRow } from './edge-admin-client.js' import { EDGE_FENCE_RETRY_BASE_MS, retryPendingFences } from './edge-relay-fence.js' -import { EdgeEvacuation } from './edge-relay-evacuation.js' const databaseUrl = process.env.ORCA_RELAY_TEST_POSTGRES_URL const describePostgres = databaseUrl ? describe : describe.skip @@ -237,38 +236,4 @@ describePostgres('relay edge on PostgreSQL', () => { expect(await database.query(`SELECT 1 FROM relay_edge_fence_pending WHERE user_id = ?`, [host.userId])).toEqual([]) vi.restoreAllMocks() }) - - it('mode down: a leaving host loses its Postgres devices, and the sweep drains wrapping around its origin', async () => { - vi.spyOn(console, 'warn').mockImplementation(() => {}) - vi.spyOn(console, 'error').mockImplementation(() => {}) - const flags = { mode: 'on' as 'on' | 'down' } - const config = { ...edgeConfig, control: () => flags } - const drained: string[] = [] - const admin = { - assign: async (body: EdgeAssignBody) => ({ outcome: 'applied' as const, assignmentEpoch: body.assignmentEpoch }), - exportHost: async (): Promise => { - throw new Error('unexpected export') - }, - drain: async (relayHostId: string) => { - drained.push(relayHostId) - return { outcome: 'idle' as const } - } - } - const edge = new EdgeAssignments(database, config, Date.now, admin) - await edge.ensureEdgeCell() - const downHosts = ['edgepgdown000001', 'edgepgdown000002'].map((relayHostId) => ({ userId: 'edge-pg-user', relayHostId })) - for (const host of downHosts) { await edge.moveToEdge(host, 0) } - await database.query( - `INSERT INTO relay_devices (user_id, relay_host_id, relay_device_id, current_hash, current_version, - current_expires_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?)`, - ['edge-pg-user', downHosts[0]!.relayHostId, 'phone-a', 'hash-phone-a', 2, 1_900_000_000_000, 1_800_000_000_000] - ) - flags.mode = 'down' - // Origin 'f3333333' sorts after 'edge-pg-user': the pass finds both hosts only once wrapped. - const evacuation = new EdgeEvacuation(database, config, admin, EDGE_CELL_ID, Date.now, () => 0.95) - await evacuation.sweep() - expect(drained.filter((id) => id.startsWith('edgepgdown'))).toEqual(downHosts.map((host) => host.relayHostId)) - expect(await edge.leaveEdge(downHosts[0]!)).toBe(true) - expect(await database.query(`SELECT 1 FROM relay_devices WHERE relay_host_id = ?`, [downHosts[0]!.relayHostId])).toEqual([]) - }) }) diff --git a/cloud/apps/relay/src/edge-relay.test.ts b/cloud/apps/relay/src/edge-relay.test.ts index 6a5a0827b8c..43b62345ee4 100644 --- a/cloud/apps/relay/src/edge-relay.test.ts +++ b/cloud/apps/relay/src/edge-relay.test.ts @@ -1,28 +1,14 @@ import { createHash } from 'node:crypto' -import { RELAY_CLOSE_CODE } from '@orca-cloud/relay-contract' -import { afterEach, describe, expect, it, vi } from 'vitest' -import type WebSocket from 'ws' +import { afterEach, describe, expect, it } from 'vitest' import { RelayAssignmentStore } from './assignment/assignment-store.js' import { RelayCellAdmissionSelector, encodeMembership, type CellAdmissionMembership } from './cell/admission/cell-admission-selector.js' -import type { RelayCellConfig, RelayConfig } from './config.js' -import type { CredentialReservation, RelayCredentialStore } from './credentials/credential-store.js' +import type { RelayCellConfig } from './config.js' import { openInMemoryRelayDatabase, type RelayDatabase } from './database/database.js' -import { - EDGE_CELL_ID, - EdgeAssignments, - EdgeMoveBusyError, EdgeMoveDeferredError, - edgeCohortBucket, - inEdgeCohort, - mapEpochFloor, - type EdgeRelayConfig -} from './edge-relay.js' -import { HostSessionRegistry } from './connections/host-session-registry.js' -import type { RelayRuntimeObserver } from './observability/relay-observability.js' -import { ProcessQueuedByteBudget } from './connections/splice-forwarder.js' +import { EDGE_CELL_ID, EdgeAssignments, EdgeMoveDeferredError, type EdgeRelayConfig } from './edge-relay.js' const EDGE_URL = 'https://edge.relay.example.com' const GCE_CELL: RelayCellConfig = { id: 'production-gce-c3', url: 'https://c3.relay.example.com', capacityRequests: 1_000 } @@ -81,50 +67,6 @@ function fingerprint(membership: CellAdmissionMembership): string { return createHash('sha256').update(encodeMembership(membership)).digest('hex') } -describe('edge cohort', () => { - it('lets deny beat allow, host allow beat region and share, and scopes accounts and the share by region', () => { - const host = 'abcdefghijklmnop' - expect(inEdgeCohort(edgeConfig({ rolloutPercent: 100 }), host, 'us-central1')).toBe(true) - expect(inEdgeCohort(edgeConfig({ rolloutPercent: 100 }), host, undefined)).toBe(true) - expect(inEdgeCohort(edgeConfig({ rolloutPercent: 100 }), host, 'asia-east2')).toBe(false) - expect( - inEdgeCohort(edgeConfig({ rolloutPercent: 100, regions: new Set(['us-central1', 'asia-east2']) }), host, 'asia-east2') - ).toBe(true) - expect(inEdgeCohort(edgeConfig({ rolloutPercent: 0 }), host, 'us-central1')).toBe(false) - // An allowed account brings every host it has in the cohort's regions; deny still wins. - const team = edgeConfig({ allowUsers: new Set(['user-team']), regions: new Set(['us-central1']) }) - expect(inEdgeCohort(team, host, 'us-central1', 'user-team')).toBe(true) - expect(inEdgeCohort(team, host, undefined, 'user-team')).toBe(true) - expect(inEdgeCohort(team, host, 'asia-east2', 'user-team')).toBe(false) - expect(inEdgeCohort({ ...team, regions: new Set() }, host, 'us-central1', 'user-team')).toBe(false) - expect(inEdgeCohort(team, host, 'us-central1', 'user-other')).toBe(false) - expect(inEdgeCohort(team, host, 'us-central1')).toBe(false) - expect(inEdgeCohort({ ...team, denyHosts: new Set([host]) }, host, 'us-central1', 'user-team')).toBe(false) - expect(inEdgeCohort(edgeConfig({ allowHosts: new Set([host]), regions: new Set() }), host, 'asia-east2')).toBe(true) - expect( - inEdgeCohort(edgeConfig({ rolloutPercent: 100, allowHosts: new Set([host]), denyHosts: new Set([host]) }), host, 'us-central1') - ).toBe(false) - }) - - it('buckets hosts stably and roughly evenly', () => { - expect(edgeCohortBucket('abcdefghijklmnop')).toBe(edgeCohortBucket('abcdefghijklmnop')) - const hosts = Array.from({ length: 2_000 }, (_, index) => `host${String(index).padStart(12, '0')}`) - const inTenPercent = hosts.filter((host) => edgeCohortBucket(host) < 10).length - expect(inTenPercent).toBeGreaterThan(140) - expect(inTenPercent).toBeLessThan(260) - }) - - it('floors the edge epoch above every epoch the seat map knows', () => { - const source = { - seatsOf: () => [{ epoch: 4 }], - recentlyLeftOf: () => [{ epoch: 9 }], - bookingOf: () => ({ epoch: 6 }) - } - expect(mapEpochFloor(source, IDENTITY, 0)).toBe(9) - expect(mapEpochFloor(undefined, IDENTITY, 0)).toBe(0) - }) -}) - describe('edge cell seed', () => { it('registers a disabled, empty, existing-only cell the admission selector carries without drift', async () => { const { database, store, edge, clock } = await setup({ requireLiveCells: true }) @@ -170,24 +112,9 @@ describe('edge cell seed', () => { general: [GCE_CELL.id] }) }) - - it('answers nothing before the seed lands', async () => { - const { edge } = await setup({ requireLiveCells: true }) - expect(await edge.edgeAssignment(IDENTITY)).toBeNull() - }) }) describe('edge move', () => { - it('mints map floor + 1 for a host with no row and keeps the epoch on re-asks', async () => { - const { edge, setNow } = await setup({ requireLiveCells: true }) - await edge.ensureEdgeCell() - const first = await edge.moveToEdge(IDENTITY, 7) - expect(first).toMatchObject({ cellId: EDGE_CELL_ID, cellUrl: EDGE_URL, assignmentEpoch: 8 }) - setNow(5_000) - const again = await edge.moveToEdge(IDENTITY, 20) - expect(again.assignmentEpoch).toBe(8) - expect(again.leaseExpiresAt).toBeGreaterThan(first.leaseExpiresAt) - }) it('moves a host off a GCE cell with a strictly newer epoch than both the row and the map', async () => { const { database, store, edge } = await setup({ requireLiveCells: true }) @@ -202,30 +129,6 @@ describe('edge move', () => { expect(moved.assignmentEpoch).toBe(placed.assignmentEpoch + 4) }) - it('defers the move while the host still holds units on its old cell', async () => { - const { database, store, edge } = await setup({ requireLiveCells: true }) - await edge.ensureEdgeCell() - await store.assign(IDENTITY) - await database.query( - `UPDATE relay_assignments SET reserved_splices = 1 WHERE user_id = ? AND relay_host_id = ?`, - [IDENTITY.userId, IDENTITY.relayHostId] - ) - await expect(edge.moveToEdge(IDENTITY, 0)).rejects.toBeInstanceOf(EdgeMoveDeferredError) - const [row] = await database.query(`SELECT cell_id FROM relay_assignments WHERE user_id = ?`, [IDENTITY.userId]) - expect(row).toMatchObject({ cell_id: GCE_CELL.id }) - }) - - it('is invisible to the director liveness rule, so phone paths must ask the edge first', async () => { - const { store, edge } = await setup({ requireLiveCells: true }) - await edge.ensureEdgeCell() - const moved = await edge.moveToEdge(IDENTITY, 0) - expect(await store.resolve(IDENTITY)).toBeNull() - expect(await edge.edgeAssignment(IDENTITY)).toMatchObject({ - cellUrl: EDGE_URL, - assignmentEpoch: moved.assignmentEpoch - }) - }) - it('rolls back: with the cohort off, the host is re-placed on GCE above the edge epoch', async () => { const { database, store, edge } = await setup({ requireLiveCells: true }) await edge.ensureEdgeCell() @@ -238,120 +141,3 @@ describe('edge move', () => { expect(await edge.edgeAssignment(IDENTITY)).toBeNull() }) }) - -describe('old cells and phones of a host moved to the edge', () => { - const config = { - port: 8080, - publicUrl: GCE_CELL.url, - cellUrl: GCE_CELL.url, - authIssuer: 'https://auth.example.com', - authAudience: 'orca-relay', - jwksUrl: 'https://auth.example.com/jwks', - assignmentSigningKey: new Uint8Array(32), - role: 'cell', - cellId: GCE_CELL.id, - cells: [GCE_CELL], - adminAudience: `${GCE_CELL.url}/v1/admin/drain`, - deployServiceAccount: 'deploy@example.com', - runtimeServiceAccount: 'runtime@example.com', - adminJwksUrl: 'https://auth.example.com/admin-jwks', - databasePoolMax: 10, - publicAssignmentsEnabled: true, - publicAssignmentConcurrency: 2, - publicAssignmentQueueMax: 128, - publicAssignmentWaitMs: 4_000, - publicResolveConcurrency: 1, - publicResolveWaitMs: 5_000, - publicAssignmentRetryAfterSeconds: 5, - dataDir: './test-data' - } satisfies RelayConfig - - class FakeSocket { - readonly OPEN = 1 - readyState = 1 - readonly send = vi.fn() - readonly close = vi.fn((_code?: number, _reason?: string) => { - this.readyState = 3 - }) - readonly terminate = vi.fn() - readonly once = vi.fn() - readonly on = vi.fn() - readonly off = vi.fn() - } - - const observer = { - recordAuth: vi.fn(), - recordForwardedBytes: vi.fn(), - recordHttp: vi.fn(), - recordReconnect: vi.fn(), - recordSql: vi.fn(), - recordClientAcceptAbandoned: vi.fn(), - recordClientAcceptCompleted: vi.fn(), - recordControlRtt: vi.fn(), - recordPhoneRejection: vi.fn(), - recordPhoneAttachDeadline: vi.fn() - } satisfies RelayRuntimeObserver - - async function cellRegistry(seed: boolean) { - // The director moved the host; the cell reads the same database without the liveness rule. - const director = await setup({ requireLiveCells: true }) - if (seed) { await director.edge.ensureEdgeCell() } - await director.store.assign(IDENTITY) - await settle(director.database) - await (seed - ? director.edge.moveToEdge(IDENTITY, 0) - : director.database.query(`UPDATE relay_assignments SET cell_id = ? WHERE user_id = ?`, [ - EDGE_CELL_ID, - IDENTITY.userId - ])) - const cellStore = new RelayAssignmentStore(director.database, director.clock, { requireLiveCells: false }) - const credentials = { - resolveResume: vi.fn().mockResolvedValue({ userId: IDENTITY.userId }), - resolveInviteForMove: vi.fn().mockResolvedValue(null), - reserveCredential: vi.fn() - } - const registry = new HostSessionRegistry( - config, - vi.fn(), - credentials as unknown as RelayCredentialStore, - cellStore, - new ProcessQueuedByteBudget(), - observer, - director.clock - ) - return { registry, cellStore, credentials } - } - - it('database credential path: a phone of the moved host gets WRONG_CELL and re-resolves', async () => { - const { registry, credentials } = await cellRegistry(true) - const phone = new FakeSocket() - await registry.acceptClient(phone as unknown as WebSocket, IDENTITY.relayHostId, 'resume-token', undefined) - expect(phone.close).toHaveBeenCalledWith(RELAY_CLOSE_CODE.WRONG_CELL, expect.any(String)) - expect(credentials.reserveCredential).not.toHaveBeenCalled() - }) - - it('memory path: a missed seat whose row names the edge is WRONG_CELL, not HOST_OFFLINE', async () => { - const { registry } = await cellRegistry(true) - const phone = new FakeSocket() - const reservation = { userId: IDENTITY.userId, relayHostId: IDENTITY.relayHostId } as CredentialReservation - await ( - registry as unknown as { - rejectMissedSeat: (socket: WebSocket, reservation: CredentialReservation, sessionKey: string) => Promise - } - ).rejectMissedSeat(phone as unknown as WebSocket, reservation, `${IDENTITY.userId}\u0000${IDENTITY.relayHostId}`) - expect(phone.close).toHaveBeenCalledWith(RELAY_CLOSE_CODE.WRONG_CELL, expect.any(String)) - }) - - it('without the seeded edge cell row the memory path would pin phones with HOST_OFFLINE', async () => { - const { registry, cellStore } = await cellRegistry(false) - expect(await cellStore.resolve(IDENTITY)).toBeNull() - const phone = new FakeSocket() - const reservation = { userId: IDENTITY.userId, relayHostId: IDENTITY.relayHostId } as CredentialReservation - await ( - registry as unknown as { - rejectMissedSeat: (socket: WebSocket, reservation: CredentialReservation, sessionKey: string) => Promise - } - ).rejectMissedSeat(phone as unknown as WebSocket, reservation, `${IDENTITY.userId}\u0000${IDENTITY.relayHostId}`) - expect(phone.close).toHaveBeenCalledWith(RELAY_CLOSE_CODE.HOST_OFFLINE, expect.anything()) - }) -}) diff --git a/config/flaky-test-pattern-baseline.txt b/config/flaky-test-pattern-baseline.txt index 500e84700b3..d113da1a6b7 100644 --- a/config/flaky-test-pattern-baseline.txt +++ b/config/flaky-test-pattern-baseline.txt @@ -18,7 +18,6 @@ 1 test-no-in-test-import-actual:src/main/omp/omp-session-cwd-lookup.test.ts 1 test-no-in-test-import-actual:src/main/orcad/electron-serve-provider-selection.test.ts 1 test-no-in-test-import-actual:src/main/pty/scrollback/overlay-mirror.test.ts -1 test-no-in-test-import-actual:src/main/runtime/files/orca-runtime-files-mobile-explorer-reads.test.ts 1 test-no-in-test-import-actual:src/main/runtime/terminal/artifacts/orca-runtime-files-terminal-artifact-swap-detection.test.ts 1 test-no-in-test-import-actual:src/main/runtime/rpc/server/pairing/mobile-pairing-userdata-path.test.ts 1 test-no-in-test-import-actual:src/main/speech/openai-api-key-store.test.ts @@ -251,7 +250,6 @@ 5 test-no-real-time-wait:src/main/pty/process-retirement/windows-live-tree-kill.win32.test.ts 1 test-no-real-time-wait:src/main/runtime/agent-session/structured-agent-session-legacy-send-behind-command.test.ts 1 test-no-real-time-wait:src/main/runtime/terminal/recovery/layout-command-pane-starts-layout-owner.test.ts -1 test-no-real-time-wait:src/main/runtime/files/orca-runtime-files-mobile-explorer-reads.test.ts 3 test-no-real-time-wait:src/main/runtime/orchestration/mailbox/terminal-pointer/orchestration-codex-real-pty.integration.test.ts 1 test-no-real-time-wait:src/main/runtime/workspaces/removal/owned-session-teardown-policy.test.ts 1 test-no-real-time-wait:src/main/runtime/rpc/server/remote-runtime-request-connection.integration.test.ts @@ -613,7 +611,6 @@ 2 test-no-wall-clock:src/main/runtime/agent-session/structured-agent-session-held-signal.test.ts 1 test-no-wall-clock:src/main/runtime/mobile-cloud-relay/mobile-relay-e2ee.integration.test.ts 1 test-no-wall-clock:src/main/runtime/mobile-cloud-relay/relay-http-client.test.ts -1 test-no-wall-clock:src/main/persistence/mobile-store-orphaned-temp-cleanup.test.ts 1 test-no-wall-clock:src/main/runtime/terminal/layout/mobile-subscribe-idle-pane-hydrate.test.ts 1 test-no-wall-clock:src/main/runtime/session-construction/agent-session/orca-runtime-agent-session-operation.test.ts 3 test-no-wall-clock:src/main/runtime/terminal/query/orca-runtime-codex-idle-title-chat-address.test.ts diff --git a/config/reliability-gates.jsonc b/config/reliability-gates.jsonc index 1cee261fe74..054816aea0b 100644 --- a/config/reliability-gates.jsonc +++ b/config/reliability-gates.jsonc @@ -3883,6 +3883,7 @@ "Extend the oracle to mobile observers and to a client with two windows open on one connection." ], "knownGaps": [ + "Audit2141: Remove only the in-memory fake-store restart callback from src/main/runtime/client/navigation/multi-client-navigation-isolation.integration.test.ts. Its copied persistence-object alarm is intentionally lost without replacement. All eight actual paired-WebSocket callbacks and five active assertion aliases remain byte-identical. Existing Electron receipt is unchanged and validates only its original source/journey; no fresh validation of the reduced integration selector, disk persistence, live remote/mobile, or cross-platform result is claimed.", "No live Windows or Linux server run yet; the reported field topology was a Windows 11 server.", "Mobile observers are covered only by the platform-neutral clientKind contract, not by a live phone journey.", "Host-originated CLI creates deliberately still reveal on every client; that broader authority question is untouched by this gate.", diff --git a/config/scripts/ci/sharding/ci-shard-timings.json b/config/scripts/ci/sharding/ci-shard-timings.json index cacc6740f31..af260db4cca 100644 --- a/config/scripts/ci/sharding/ci-shard-timings.json +++ b/config/scripts/ci/sharding/ci-shard-timings.json @@ -3425,7 +3425,6 @@ "src/main/runtime/session-lifecycle/publication/mobile-session-tabs-agent-status-heartbeat.test.ts": 65, "src/main/runtime/terminal/surface/mobile-session-terminal-persistence-retirement.test.ts": 278, "src/main/runtime/session-tabs/mobile-session-window-tab-order.test.ts": 4194, - "src/main/persistence/mobile-store-orphaned-temp-cleanup.test.ts": 320, "src/main/runtime/terminal/layout/mobile-subscribe-idle-pane-hydrate.test.ts": 4782, "src/main/runtime/client/navigation/multi-client-navigation-isolation.integration.test.ts": 5640, "src/main/runtime/terminal/wait/omp-readiness-transcripts.test.ts": 7901, @@ -3445,8 +3444,6 @@ "src/main/runtime/terminal/wait/orca-runtime-exact-worker-provider-session.test.ts": 3617, "src/main/runtime/terminal/lifecycle/orca-runtime-exited-split-pane-close.test.ts": 4235, "src/main/runtime/terminal/surface/orca-runtime-failed-start-pane-layout-owner.test.ts": 4204, - "src/main/runtime/files/orca-runtime-files-chunk-reads.test.ts": 583, - "src/main/runtime/files/orca-runtime-files-mobile-explorer-reads.test.ts": 624, "src/main/runtime/files/orca-runtime-files-preview-budget.test.ts": 672, "src/main/runtime/files/search/orca-runtime-files-search.test.ts": 675, "src/main/runtime/terminal/artifacts/orca-runtime-files-terminal-artifact-grants.test.ts": 580, @@ -3464,7 +3461,6 @@ "src/main/runtime/files/paths/orca-runtime-read-kitty-file.test.ts": 4304, "src/main/runtime/browser/headless/orca-runtime-serve-browser-restore.test.ts": 4469, "src/main/runtime/session-tabs/orca-runtime-session-tabs-path-alias.test.ts": 4827, - "src/main/runtime/construction/orca-runtime-structured-status-sink-wiring.test.ts": 4286, "src/main/runtime/terminal/surface/orca-runtime-terminal-close-continuity.test.ts": 4439, "src/main/runtime/terminal/surface/orca-runtime-terminal-close-no-widening-layout-owner.test.ts": 4997, "src/main/runtime/terminal/surface/orca-runtime-terminal-close-no-widening.test.ts": 4509, diff --git a/mobile/src/files/mobile-file-mutation-ownership.test.ts b/mobile/src/files/mobile-file-mutation-ownership.test.ts deleted file mode 100644 index 2a815f5ca00..00000000000 --- a/mobile/src/files/mobile-file-mutation-ownership.test.ts +++ /dev/null @@ -1,92 +0,0 @@ -import { describe, expect, it, vi } from 'vitest' -import type { SshConnectionState } from '../../../src/shared/ssh-types' -import { FILE_MUTATION_OWNERSHIP_RUNTIME_CAPABILITY } from '../../../src/shared/file-access/files-runtime-capabilities' -import type { RpcClient } from '../transport/rpc-client' -import type { RpcResponse } from '../transport/types' -import { - buildMobileFileMutationOwnership, - captureMobileFileMutationOwnership -} from './mobile-file-mutation-ownership' - -function success(result: unknown): RpcResponse { - return { id: 'rpc-1', ok: true, result, _meta: { runtimeId: 'runtime-1' } } -} - -function clientWithResponses(responses: RpcResponse[]): { - client: Pick - sendRequest: ReturnType -} { - const sendRequest = vi.fn(async () => { - const response = responses.shift() - if (!response) { - throw new Error('Unexpected RPC request') - } - return response - }) - return { client: { sendRequest }, sendRequest } -} - -function sshState(targetId: string, connectionGeneration: number | undefined): SshConnectionState { - return { - targetId, - status: 'connected', - error: null, - reconnectAttempt: 0, - connectionGeneration - } -} - -describe('mobile file mutation ownership', () => { - it.each([undefined, 'local', 'runtime:environment-1'])( - 'binds %s worktrees to the runtime-local file host', - (hostId) => { - expect(buildMobileFileMutationOwnership(hostId)).toEqual({ - expectedExecutionHostId: 'local' - }) - } - ) - - it('binds SSH worktrees to the target and live connection generation', () => { - expect( - buildMobileFileMutationOwnership('ssh:target%20one', sshState('target one', 17)) - ).toEqual({ - expectedExecutionHostId: 'ssh:target%20one', - expectedSshTargetId: 'target one', - expectedSshConnectionGeneration: 17 - }) - }) - - it.each([ - ['a malformed owner', 'not-an-execution-host', null], - ['a missing SSH state', 'ssh:target-1', null], - ['a mismatched SSH target', 'ssh:target-1', sshState('target-2', 4)], - ['a missing SSH generation', 'ssh:target-1', sshState('target-1', undefined)] - ])('rejects %s', (_name, hostId, state) => { - expect(() => buildMobileFileMutationOwnership(hostId, state)).toThrow( - "Couldn't verify the SSH connection" - ) - }) - - // Absent hostId captures local; explicit null refuses an unplaceable host instead of sending its write locally. - it('captures local ownership for a workspace whose reply records no host', async () => { - const { client } = clientWithResponses([ - success({ capabilities: [FILE_MUTATION_OWNERSHIP_RUNTIME_CAPABILITY] }), - success({ worktree: {} }) - ]) - - await expect(captureMobileFileMutationOwnership(client, 'id:worktree-1')).resolves.toEqual({ - expectedExecutionHostId: 'local' - }) - }) - - it('refuses a workspace whose reply names an explicit null host', async () => { - const { client } = clientWithResponses([ - success({ capabilities: [FILE_MUTATION_OWNERSHIP_RUNTIME_CAPABILITY] }), - success({ worktree: { hostId: null } }) - ]) - - await expect(captureMobileFileMutationOwnership(client, 'id:worktree-1')).rejects.toThrow( - "Couldn't verify the SSH connection" - ) - }) -}) diff --git a/mobile/src/hooks/use-now.test.ts b/mobile/src/hooks/use-now.test.ts deleted file mode 100644 index 29dc8bd03d9..00000000000 --- a/mobile/src/hooks/use-now.test.ts +++ /dev/null @@ -1,131 +0,0 @@ -import { createElement } from 'react' -import { act, create, type ReactTestRenderer } from 'react-test-renderer' -import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' - -const appState = vi.hoisted(() => ({ - current: 'active', - listener: null as ((nextState: string) => void) | null, - remove: vi.fn() -})) - -vi.mock('react-native', () => ({ - AppState: { - get currentState(): string { - return appState.current - }, - addEventListener: (_event: string, listener: (nextState: string) => void) => { - appState.listener = listener - return { remove: appState.remove } - } - } -})) - -import { useNow, useSharedNow } from './use-now' - -describe('useSharedNow', () => { - let renderer: ReactTestRenderer | null = null - let latest = 0 - - function Harness(): null { - latest = useSharedNow(1_000) - return null - } - - afterEach(() => { - act(() => renderer?.unmount()) - renderer = null - vi.useRealTimers() - }) - - it('shows the current time when remounted soon after the last reader unmounted', () => { - vi.useFakeTimers() - vi.setSystemTime(1_000) - appState.current = 'active' - act(() => { - renderer = create(createElement(Harness)) - }) - act(() => renderer?.unmount()) - - vi.setSystemTime(1_500) - act(() => { - renderer = create(createElement(Harness)) - }) - expect(latest).toBe(1_500) - }) -}) - -describe('useNow', () => { - let renderer: ReactTestRenderer | null = null - let latest = 0 - - function Harness({ enabled = true }: { enabled?: boolean }): null { - latest = useNow(1_000, enabled) - return null - } - - function changeAppState(nextState: string): void { - act(() => { - appState.current = nextState - appState.listener?.(nextState) - }) - } - - beforeEach(() => { - vi.useFakeTimers() - vi.setSystemTime(1_000) - appState.current = 'active' - appState.listener = null - appState.remove.mockClear() - latest = 0 - act(() => { - renderer = create(createElement(Harness)) - }) - }) - - afterEach(() => { - act(() => renderer?.unmount()) - renderer = null - vi.useRealTimers() - }) - - it('ticks while active, pauses in the background, and refreshes immediately on resume', () => { - expect(latest).toBe(1_000) - - act(() => { - vi.advanceTimersByTime(1_000) - }) - expect(latest).toBe(2_000) - - changeAppState('background') - act(() => { - vi.advanceTimersByTime(5_000) - }) - expect(latest).toBe(2_000) - - changeAppState('active') - expect(latest).toBe(7_000) - - act(() => { - vi.advanceTimersByTime(1_000) - }) - expect(latest).toBe(8_000) - }) - - it('stops while disabled and refreshes immediately when re-enabled', () => { - act(() => renderer?.update(createElement(Harness, { enabled: false }))) - act(() => { - vi.advanceTimersByTime(5_000) - }) - expect(latest).toBe(1_000) - - act(() => renderer?.update(createElement(Harness, { enabled: true }))) - expect(latest).toBe(6_000) - }) - - it('removes the shared AppState listener after the last caller unmounts', () => { - act(() => renderer?.unmount()) - renderer = null - - expect(appState.remove).toHaveBeenCalledTimes(1) - }) -}) diff --git a/mobile/src/mobile-web-shell/page-open-target.test.tsx b/mobile/src/mobile-web-shell/page-open-target.test.tsx index 332b3865fb5..a2d825b8102 100644 --- a/mobile/src/mobile-web-shell/page-open-target.test.tsx +++ b/mobile/src/mobile-web-shell/page-open-target.test.tsx @@ -40,37 +40,6 @@ function shellArgs(overrides: Partial): Args { } } -it('pushes a session natively when the page the shell activates is older than open targets', async () => { - const openNatively = vi.fn() - const cleared = vi.fn() - await show( - shellArgs({ - route: { ...HOST, params: { open: SESSION } }, - pageVersion: 2, - openNatively, - onRouteParamClear: cleared - }) - ) - - expect(rendered.view?.pageRoute).toEqual({ pathname: '/h/host-1', params: {} }) - expect(cleared.mock.calls).toEqual([['open', SESSION]]) - expect(openNatively.mock.calls).toEqual([[SESSION]]) -}) - -it('drops that push when another screen opened on top while the page downloaded', async () => { - const openNatively = vi.fn() - await show( - shellArgs({ - route: { ...HOST, params: { open: SESSION } }, - pageVersion: 2, - isFocused: () => false, - openNatively - }) - ) - - expect(openNatively).not.toHaveBeenCalled() -}) - it('reopens the session a lost document showed in the document that replaces it', async () => { await show(shellArgs({})) await act(async () => rendered.view?.recordLocation(SESSION)) diff --git a/mobile/src/navigation/use-open-host-stack-route.test.tsx b/mobile/src/navigation/use-open-host-stack-route.test.tsx deleted file mode 100644 index e33b61d4ca6..00000000000 --- a/mobile/src/navigation/use-open-host-stack-route.test.tsx +++ /dev/null @@ -1,65 +0,0 @@ -import { createElement } from 'react' -import { act, create } from 'react-test-renderer' -import { expect, it, vi } from 'vitest' -import type { HostStackNavigationState } from './host-stack-navigation' - -const HOST_ROUTE: HostStackNavigationState = { - index: 0, - routes: [{ name: 'h' }] -} -// The same host route as the root container sees it, with the host list's stack already mounted. -const MOUNTED_HOST_ROUTE: HostStackNavigationState = { - index: 0, - routes: [ - { - name: 'h', - state: { - key: '/h', - index: 0, - routes: [{ key: 'host-index', name: '[hostId]/index', params: { hostId: 'host-1' } }] - } - } - ] -} - -const nav = vi.hoisted(() => ({ push: vi.fn(), dispatch: vi.fn() })) -vi.mock('expo-router', () => ({ - useRouter: () => ({ push: nav.push, replace: vi.fn() }), - // A screen's navigator keeps its last snapshot of a child stack until that stack next changes. - useNavigation: () => ({ - addListener: () => () => {}, - dispatch: nav.dispatch, - getState: () => HOST_ROUTE - }), - useNavigationContainerRef: () => ({ isReady: () => true, getRootState: () => MOUNTED_HOST_ROUTE }) -})) - -import { useOpenHostStackRoute } from './use-open-host-stack-route' - -it('hands a session to the host list that just mounted instead of pushing a second document', async () => { - const opener: { open?: ReturnType } = {} - function Opener() { - opener.open = useOpenHostStackRoute(true) - return null - } - await act(async () => { - create(createElement(Opener)) - }) - - opener.open?.('host-1', { - name: '[hostId]/session/[worktreeId]', - params: { hostId: 'host-1', worktreeId: 'wt-1' } - }) - - expect(nav.push).not.toHaveBeenCalled() - expect(nav.dispatch.mock.calls).toEqual([ - [ - { - type: 'SET_PARAMS', - target: '/h', - source: 'host-index', - payload: { params: { open: '%2Fh%2Fhost-1%2Fsession%2Fwt-1' } } - } - ] - ]) -}) diff --git a/mobile/src/query/host-query-persistence.test.ts b/mobile/src/query/host-query-persistence.test.ts index 955227af559..2095297f0c4 100644 --- a/mobile/src/query/host-query-persistence.test.ts +++ b/mobile/src/query/host-query-persistence.test.ts @@ -87,36 +87,6 @@ describe('createHostQueryPersistence', () => { vi.useRealTimers() }) - it('paints the stored host list with its saved time and no agent state before any host read', async () => { - const store = memoryStore('host-a') - await storeFromEarlierLaunch(store, [liveRow]) - - const relaunched = new QueryClient() - const restoring = createHostQueryPersistence(relaunched, store.backend).restore(firstPaint) - await vi.runAllTimersAsync() - await restoring - - expect(relaunched.getQueryData(key)).toEqual({ - snapshotId: null, - worktrees: [ - { - worktreeId: 'repo-1::/w/mobile-redesign', - repoId: 'repo-1', - repo: 'orca', - branch: 'kelvin/mobile-redesign', - displayName: 'Mobile redesign', - path: '/w/mobile-redesign', - liveTerminalCount: 2, - hasAttachedPty: true, - unread: true, - isPinned: false, - linkedPR: { number: 38983, state: 'open' } - } - ] - }) - expect(relaunched.getQueryState(key)?.dataUpdatedAt).toBe(SAVED_AT) - }) - it('never shows a stored record that fails its data check, and removes it', async () => { const store = memoryStore('host-a') const { branch: _branch, ...rowWithoutBranch } = liveRow diff --git a/mobile/src/tasks/use-mobile-tasks-jira-status-refresh.test.ts b/mobile/src/tasks/use-mobile-tasks-jira-status-refresh.test.ts index 53ae579792b..e1e07b45e4b 100644 --- a/mobile/src/tasks/use-mobile-tasks-jira-status-refresh.test.ts +++ b/mobile/src/tasks/use-mobile-tasks-jira-status-refresh.test.ts @@ -86,20 +86,6 @@ function pendingStatus() { } describe('mobile Jira status host ownership', () => { - it('applies an explicit status refresh for the current host', async () => { - const client = new FakeSession('connected') - client.sendRequest.mockResolvedValue({ - id: 'jira-test', - ok: true, - result: { connected: true } - }) - const fixture = mountRefresh(client, { current: client }) - await fixture.refresh() - expect(client.sendRequest).toHaveBeenCalledWith('jira.status') - expect(fixture.setJiraConnection).toHaveBeenCalledTimes(1) - expect(fixture.connection().connected).toBe(true) - }) - it('ignores an earlier explicit refresh that settles after a newer one', async () => { const client = new FakeSession('connected') const earlier = pendingStatus() diff --git a/src/cli/index-folder-workspace-selectors.test.ts b/src/cli/index-folder-workspace-selectors.test.ts deleted file mode 100644 index f85de56c45c..00000000000 --- a/src/cli/index-folder-workspace-selectors.test.ts +++ /dev/null @@ -1,178 +0,0 @@ -import { describe, expect, it, vi } from 'vitest' - -const { - callMock, - runtimeClientConstructorMock, - serveOrcaAppMock, - getDefaultUserDataPathMock, - addEnvironmentFromPairingCodeMock, - listEnvironmentsMock, - spawnMock -} = vi.hoisted(() => ({ - callMock: vi.fn(), - runtimeClientConstructorMock: vi.fn(), - serveOrcaAppMock: vi.fn(), - getDefaultUserDataPathMock: vi.fn(() => '/tmp/orca-user-data'), - addEnvironmentFromPairingCodeMock: vi.fn(), - listEnvironmentsMock: vi.fn(), - spawnMock: vi.fn() -})) - -vi.mock('./runtime-client', async () => { - const { createRuntimeClientModuleMock } = await import('./index-test-harness.js') - return createRuntimeClientModuleMock({ - callMock, - runtimeClientConstructorMock, - serveOrcaAppMock, - getDefaultUserDataPathMock - }) -}) - -vi.mock('./runtime/environments', () => ({ - addEnvironmentFromPairingCode: addEnvironmentFromPairingCodeMock, - removeEnvironment: vi.fn(), - resolveEnvironment: vi.fn() -})) - -vi.mock('../shared/remote-runtime/host-store/runtime-environment-store', () => ({ - listEnvironments: listEnvironmentsMock -})) - -vi.mock('child_process', async () => { - const { createChildProcessModuleMock } = await import('./index-test-harness.js') - return createChildProcessModuleMock(spawnMock) -}) - -import { main } from './index' -import { RuntimeRpcFailureError } from './runtime/types' -import { okFixture } from './test-fixtures' -import { useWorktreeAwarenessEnvironment } from './index-test-harness' - -const FOLDER_RECORD = { id: 'folder:hub-a', path: '/work/hub' } - -function notFound(): never { - throw new RuntimeRpcFailureError({ - id: 'req_missing', - ok: false, - error: { code: 'selector_not_found', message: 'selector_not_found' }, - _meta: { runtimeId: 'runtime-1' } - }) -} - -/** A runtime that matches folders by path only when the request opts in, or an older one that never does. */ -function answer(folderMatching: boolean) { - return async (method: string, params?: Record) => { - if (method === 'worktree.show') { - return folderMatching && params?.matchFolderWorkspaces === true - ? okFixture('req_show', { worktree: FOLDER_RECORD }) - : notFound() - } - if (method === 'status.get') { - return okFixture('req_status', { - capabilities: folderMatching ? ['workspace-selector.folder-match.v1'] : [] - }) - } - if (method === 'folderWorkspace.list') { - return okFixture('req_folders', { - folderWorkspaces: [{ id: 'hub-a', folderPath: '/work/hub' }] - }) - } - return okFixture('req_command', { - ports: [], - terminal: { handle: 'term_1', worktreeId: FOLDER_RECORD.id, title: null }, - opened: true, - kind: 'text', - relativePath: 'README.md' - }) - } -} - -describe('path: selectors on folder workspaces', () => { - useWorktreeAwarenessEnvironment({ - callMock, - serveOrcaAppMock, - getDefaultUserDataPathMock, - addEnvironmentFromPairingCodeMock, - listEnvironmentsMock, - spawnMock - }) - - it.each([ - [['terminal', 'create'], 'terminal.create'], - [['file', 'open', 'README.md'], 'files.open'] - ])('%s reaches the folder workspace', async (command, method) => { - vi.spyOn(console, 'log').mockImplementation(() => {}) - callMock.mockImplementation(answer(true)) - - await main([...command, '--worktree', 'path:/work/hub', '--json'], '/tmp') - - expect(callMock).toHaveBeenCalledWith( - method, - expect.objectContaining({ worktree: 'folder:hub-a' }) - ) - }) - - it.each([ - [true, 'folder:hub-a'], - [false, 'id:folder:hub-a'] - ])( - 'ports --all scans the folder workspace (runtime matches folders: %s)', - async (folderMatching, scanned) => { - vi.spyOn(console, 'log').mockImplementation(() => {}) - callMock.mockImplementation(answer(folderMatching)) - - await main(['ports', '--all', '--worktree', 'path:/work/hub', '--json'], '/tmp') - - expect(callMock).toHaveBeenCalledWith('workspacePorts.scanHost', { worktree: scanned }) - } - ) - - it('sends a path:/name: parent to the runtime parent resolver as typed, with its ambiguity code', async () => { - const log = vi.spyOn(console, 'log').mockImplementation(() => {}) - const choices = { - selector: 'name:Hub', - candidates: [ - { kind: 'folder workspace', hostId: 'local', path: '/work/hub', selector: 'folder:hub-a' }, - { kind: 'folder workspace', hostId: 'local', path: '/work/hub', selector: 'folder:hub-b' } - ], - nextSteps: ['Run `orca worktree ps --json` to compare the choices.'] - } - callMock.mockImplementation(async (method: string) => { - if (method === 'worktree.set') { - throw new RuntimeRpcFailureError({ - id: 'req_set', - ok: false, - error: { - code: 'LINEAGE_PARENT_AMBIGUOUS', - message: 'More than one workspace matches "name:Hub".', - data: choices - }, - _meta: { runtimeId: 'runtime-1' } - }) - } - return okFixture('req_other', {}) - }) - - await main( - ['worktree', 'set', '--worktree', 'id:repo::/c', '--parent-worktree', 'name:Hub', '--json'], - '/tmp' - ) - - expect(callMock).not.toHaveBeenCalledWith('worktree.show', expect.anything()) - expect(callMock).toHaveBeenCalledWith( - 'worktree.set', - expect.objectContaining({ parentWorktree: 'name:Hub', matchFolderWorkspaces: true }) - ) - const { error } = JSON.parse(String(log.mock.calls.at(-1)?.[0])) - expect(error).toMatchObject({ - code: 'LINEAGE_PARENT_AMBIGUOUS', - data: { - ...choices, - environment: 'local', - nextSteps: [ - 'Compare the choices with `orca worktree ps --environment local --json`, then pass one as --parent-worktree.' - ] - } - }) - }) -}) diff --git a/src/cli/index-folder-workspace-set-rm.test.ts b/src/cli/index-folder-workspace-set-rm.test.ts deleted file mode 100644 index 19342b705b1..00000000000 --- a/src/cli/index-folder-workspace-set-rm.test.ts +++ /dev/null @@ -1,178 +0,0 @@ -import { describe, expect, it, vi } from 'vitest' - -const { - callMock, - runtimeClientConstructorMock, - serveOrcaAppMock, - getDefaultUserDataPathMock, - addEnvironmentFromPairingCodeMock, - listEnvironmentsMock, - spawnMock -} = vi.hoisted(() => ({ - callMock: vi.fn(), - runtimeClientConstructorMock: vi.fn(), - serveOrcaAppMock: vi.fn(), - getDefaultUserDataPathMock: vi.fn(() => '/tmp/orca-user-data'), - addEnvironmentFromPairingCodeMock: vi.fn(), - listEnvironmentsMock: vi.fn(), - spawnMock: vi.fn() -})) - -vi.mock('./runtime-client', async () => { - const { createRuntimeClientModuleMock } = await import('./index-test-harness.js') - return createRuntimeClientModuleMock({ - callMock, - runtimeClientConstructorMock, - serveOrcaAppMock, - getDefaultUserDataPathMock - }) -}) - -vi.mock('./runtime/environments', () => ({ - addEnvironmentFromPairingCode: addEnvironmentFromPairingCodeMock, - removeEnvironment: vi.fn(), - resolveEnvironment: vi.fn() -})) - -vi.mock('../shared/remote-runtime/host-store/runtime-environment-store', () => ({ - listEnvironments: listEnvironmentsMock -})) - -vi.mock('child_process', async () => { - const { createChildProcessModuleMock } = await import('./index-test-harness.js') - return createChildProcessModuleMock(spawnMock) -}) - -import { main } from './index' -import { okFixture } from './test-fixtures' -import { useWorktreeAwarenessEnvironment } from './index-test-harness' - -const FOLDER_RECORD = { id: 'folder:hub-a', path: '/work/hub', displayName: 'Hub' } - -function answerFolder(overrides: Record unknown> = {}) { - return async (method: string) => { - if (overrides[method]) { - return overrides[method]() - } - if (method === 'worktree.show') { - return okFixture('req_show', { worktree: FOLDER_RECORD }) - } - if (method === 'status.get') { - return okFixture('req_status', { capabilities: ['folder-workspace.unique-id-mutation.v1'] }) - } - if (method === 'folderWorkspace.update') { - return okFixture('req_update', { folderWorkspace: { id: 'hub-a', name: 'Renamed' } }) - } - if (method === 'folderWorkspace.delete') { - return okFixture('req_delete', { deleted: true }) - } - throw new Error(`unexpected ${method}`) - } -} - -function lastJson(log: ReturnType): { - result?: unknown - error?: { code: string } -} { - return JSON.parse(String(log.mock.calls.at(-1)?.[0])) -} - -function folderMutations(): string[] { - return callMock.mock.calls - .map(([method]) => String(method)) - .filter((method) => method.startsWith('folderWorkspace.') || method === 'worktree.set') -} - -describe('worktree set and rm on folder workspaces', () => { - useWorktreeAwarenessEnvironment({ - callMock, - serveOrcaAppMock, - getDefaultUserDataPathMock, - addEnvironmentFromPairingCodeMock, - listEnvironmentsMock, - spawnMock - }) - - it('set renames and comments through the folder update and returns the set envelope', async () => { - const log = vi.spyOn(console, 'log').mockImplementation(() => {}) - callMock.mockImplementation(answerFolder()) - - await main( - [ - 'worktree', - 'set', - '--worktree', - 'folder:hub-a', - '--display-name', - 'Renamed', - '--comment', - 'triaging', - '--json' - ], - '/tmp' - ) - - expect(callMock).toHaveBeenCalledWith('folderWorkspace.update', { - folderWorkspaceId: 'hub-a', - requireUniqueId: true, - updates: { name: 'Renamed', comment: 'triaging' } - }) - expect(lastJson(log).result).toEqual({ worktree: FOLDER_RECORD }) - }) - - it('rm forgets the folder workspace and reports removed', async () => { - const log = vi.spyOn(console, 'log').mockImplementation(() => {}) - callMock.mockImplementation(answerFolder()) - - await main(['worktree', 'rm', '--worktree', 'folder:hub-a', '--json'], '/tmp') - - expect(callMock).toHaveBeenCalledWith('folderWorkspace.delete', { - folderWorkspaceId: 'hub-a', - requireUniqueId: true - }) - expect(callMock).not.toHaveBeenCalledWith('worktree.rm', expect.anything(), expect.anything()) - expect(lastJson(log).result).toEqual({ removed: true }) - }) - - it.each([[['set', '--display-name', 'x']], [['rm']]])( - 'an Orca without the unique-id guarantee is refused before any change (%s)', - async (command) => { - const log = vi.spyOn(console, 'log').mockImplementation(() => {}) - callMock.mockImplementation( - answerFolder({ 'status.get': () => okFixture('req_status', { capabilities: [] }) }) - ) - - const [verb, ...rest] = command - await main(['worktree', verb, '--worktree', 'folder:hub-a', ...rest, '--json'], '/tmp') - - expect(lastJson(log).error?.code).toBe('incompatible_runtime') - expect(folderMutations()).toEqual([]) - } - ) - - it.each([ - [['rm', '--delete-branch']], - [['rm', '--run-hooks']], - [['set', '--no-parent']], - [['set', '--pr', '5']] - ])('%s is refused on a folder workspace with no change', async (command) => { - const log = vi.spyOn(console, 'log').mockImplementation(() => {}) - callMock.mockImplementation(answerFolder()) - - const [verb, ...rest] = command - await main(['worktree', verb, '--worktree', 'folder:hub-a', ...rest, '--json'], '/tmp') - - expect(lastJson(log).error?.code).toBe('invalid_argument') - expect(folderMutations()).toEqual([]) - }) - - it('activate refuses a folder workspace without asking the runtime to open it', async () => { - const log = vi.spyOn(console, 'log').mockImplementation(() => {}) - callMock.mockImplementation(answerFolder()) - - await main(['worktree', 'activate', '--worktree', 'folder:hub-a', '--json'], '/tmp') - - expect(lastJson(log).error?.code).toBe('invalid_argument') - expect(callMock).not.toHaveBeenCalledWith('worktree.activate', expect.anything()) - }) -}) diff --git a/src/main/claude/theme-sync/claude-code-theme-sync.test.ts b/src/main/claude/theme-sync/claude-code-theme-sync.test.ts index 7163f01a89f..39e71097e1c 100644 --- a/src/main/claude/theme-sync/claude-code-theme-sync.test.ts +++ b/src/main/claude/theme-sync/claude-code-theme-sync.test.ts @@ -10,10 +10,10 @@ vi.mock('../claude-hook-event-versions', async (importOriginal) => ({ })) let claudeVersion: string | null = '2.1.319' import { syncClaudeCodeThemeConfigDirs } from './claude-code-theme-sync' -import { claudeCodeThemeFromTerminal } from './claude-code-theme' -import { TERMINAL_THEME_CATALOG } from '../../../shared/terminal-themes/terminal-theme-catalog' -import { contrastRatio } from '../../../shared/color/rgba-contrast' -import { parseTerminalColor } from '../../../shared/terminal-themes/terminal-title-contrast' +import './claude-code-theme' +import '../../../shared/terminal-themes/terminal-theme-catalog' +import '../../../shared/color/rgba-contrast' +import '../../../shared/terminal-themes/terminal-title-contrast' const THEME = { name: 'Orca', base: 'dark', overrides: { claude: '#7aa2f7' } } const on = { enabled: true, theme: THEME } as const @@ -59,13 +59,6 @@ describe('Claude Code theme sync', () => { }) }) - it('removes the theme key it added when there was none', async () => { - writeSettings({ model: 'opus' }) - await sync(on) - await sync(off) - expect(readSettings()).toEqual({ model: 'opus' }) - }) - it('stops writing once the user picks another theme, and leaves it on disable', async () => { writeSettings({ theme: 'dark' }) await sync(on) @@ -77,20 +70,6 @@ describe('Claude Code theme sync', () => { expect(readSettings()).toEqual({ theme: 'light-daltonized' }) }) - it('leaves a colorblind or custom theme the user picked alone', async () => { - writeSettings({ theme: 'dark-daltonized' }) - await sync(on) - expect(readSettings()).toEqual({ theme: 'dark-daltonized' }) - }) - - it('leaves settings alone for a Claude Code without custom themes', async () => { - claudeVersion = '2.1.118' - writeSettings({ theme: 'dark' }) - await sync(on) - claudeVersion = '2.1.319' - expect(readSettings()).toEqual({ theme: 'dark' }) - }) - it.skipIf(process.platform === 'win32')( 'takes over later when the settings write fails, instead of treating it as the user’s pick', async () => { @@ -113,30 +92,4 @@ describe('Claude Code theme sync', () => { await sync(on) expect(readFileSync(settingsPath, 'utf-8')).toBe('{ "theme": "dark", ') }) - - it('leaves Claude Code alone when its config dir does not exist', async () => { - rmSync(configDir, { recursive: true }) - await sync(on) - expect(() => readFileSync(settingsPath)).toThrow() - }) -}) - -describe('Claude Code theme from the terminal palette', () => { - it.each(['Solarized Light', 'Solarized Dark', 'Builtin Tango Light', 'Gruvbox Light'])( - 'keeps %s text and hues readable on its background', - (name) => { - const palette = TERMINAL_THEME_CATALOG[name] - const theme = claudeCodeThemeFromTerminal(palette) - const background = parseTerminalColor(palette.background) - const ratio = (key: string): number => { - const color = parseTerminalColor(theme?.overrides[key]) - return color && background ? contrastRatio(color, background) : 0 - } - expect(ratio('text')).toBeGreaterThanOrEqual(4.5) - expect(ratio('inactive')).toBeGreaterThanOrEqual(4.5) - for (const key of ['claude', 'success', 'error', 'warning', 'permission']) { - expect(ratio(key)).toBeGreaterThanOrEqual(3) - } - } - ) }) diff --git a/src/main/persistence/mobile-store-orphaned-temp-cleanup.test.ts b/src/main/persistence/mobile-store-orphaned-temp-cleanup.test.ts deleted file mode 100644 index d272a49d4ff..00000000000 --- a/src/main/persistence/mobile-store-orphaned-temp-cleanup.test.ts +++ /dev/null @@ -1,56 +0,0 @@ -import { existsSync, mkdtempSync, readFileSync, rmSync, utimesSync, writeFileSync } from 'node:fs' -import { tmpdir } from 'node:os' -import { join } from 'node:path' -import { afterEach, expect, it, vi } from 'vitest' -import { DeviceRegistry } from '../runtime/rpc/server/pairing/device-registry' -import { MobileNotificationDismissalStore } from '../runtime/push/notifications/mobile-notification-dismissal-store' -import { DEVICE_REGISTRY_FILENAME } from '../runtime/rpc/server/pairing/mobile-pairing-files' - -const dirs: string[] = [] -afterEach(() => { - vi.restoreAllMocks() - dirs.splice(0).forEach((dir) => rmSync(dir, { recursive: true, force: true })) -}) - -const stores = [ - [ - 'dismissal store', - 'mobile-notification-dismissals.json', - (dir: string) => new MobileNotificationDismissalStore(dir) - ], - ['device registry', DEVICE_REGISTRY_FILENAME, (dir: string) => new DeviceRegistry(dir)] -] as const - -it.each(stores)( - '%s reclaims orphaned temps while preserving active writes and unrelated files', - async (_, fileName, open) => { - const dir = mkdtempSync(join(tmpdir(), 'orca-orphaned-temp-')) - dirs.push(dir) - // Other-process PIDs: the sweep always spares this process's own temps. - const orphaned = join(dir, `${fileName}.${process.pid + 1}.1784108697605.b306bb91.tmp`) - const recent = join(dir, `${fileName}.${process.pid + 2}.1784108697605.cafef00d.tmp`) - const own = join(dir, `${fileName}.${process.pid}.1784108697605.aaaaaaaa.tmp`) - const otherStore = join( - dir, - `${fileName === DEVICE_REGISTRY_FILENAME ? 'mobile-notification-dismissals.json' : DEVICE_REGISTRY_FILENAME}.${process.pid + 1}.0.bbbbbbbb.tmp` - ) - const similarName = join(dir, `${fileName}-backup.${process.pid + 1}.0.cccccccc.tmp`) - const wrongSuffix = join(dir, `${fileName}.${process.pid + 1}.0.dddddddd.tmp.backup`) - const finalPath = join(dir, fileName) - writeFileSync(orphaned, '[]') - writeFileSync(recent, '[]') - writeFileSync(finalPath, '[]') - const twoDaysAgo = (Date.now() - 2 * 86400_000) / 1000 - for (const path of [orphaned, own, otherStore, similarName, wrongSuffix]) { - writeFileSync(path, '[]') - utimesSync(path, twoDaysAgo, twoDaysAgo) - } - - open(dir) - - await vi.waitFor(() => expect(existsSync(orphaned)).toBe(false)) - for (const path of [recent, own, otherStore, similarName, wrongSuffix, finalPath]) { - expect(readFileSync(path, 'utf8')).toBe('[]') - } - } -) diff --git a/src/main/runtime/agent-senders/runtime-agent-sender-associations.test.ts b/src/main/runtime/agent-senders/runtime-agent-sender-associations.test.ts index 59f63a163df..f403a9b54da 100644 --- a/src/main/runtime/agent-senders/runtime-agent-sender-associations.test.ts +++ b/src/main/runtime/agent-senders/runtime-agent-sender-associations.test.ts @@ -9,12 +9,8 @@ import 'node:path' import '../../../shared/remote-runtime/host-store/runtime-environment-store-file' import { createRuntimeAgentProjections } from '../construction/runtime-agent-projections' -import { - fingerprintOrchestrationPeer, - pairedOrchestrationPeers -} from '../orchestration/federation/environment-transport' - -import { devicePrincipalOf } from '../rpc/server/pairing/device-principal' +import { fingerprintOrchestrationPeer } from '../orchestration/federation/environment-transport' +import '../rpc/server/pairing/device-principal' import { OrchestrationDb } from '../orchestration/db/orchestration-db' @@ -39,11 +35,7 @@ import { agentSessionLeaseFixture, agentSessionRecordFixture } from '../../../shared/agent-session/lifecycle/agent-session-record.test-fixture' - -import { - mintStructuredWorkerHandle, - mintStructuredWorkerPaneKey -} from '../orchestration/terminal-authority/structured-sessions/structured-worker-identity' +import '../orchestration/terminal-authority/structured-sessions/structured-worker-identity' import { testOrcaSessionId } from '../../../shared/orca-session-address-test-fixture' @@ -186,64 +178,6 @@ function harness(n = 1, records: AgentSessionRecordReader | null = null) { } } -it('locates every sender by the handle or Dispatch its card saved after a warm restart, with no agent event', () => { - const h = harness(3) - const idleWorker = h.worker(0) - // Dispatched to tab-1's own terminal; Orca hasn't recorded that terminal for it yet. - const starting = createRootDispatch( - h.db, - h.db.createTask({ runId: h.run.id, spec: 'starting' }).id, - handle(1), - pane('tab-1') - ) - for (const check of [() => undefined, () => h.restart()]) { - check() - // Started-worker cards name the Dispatch; Message, Task and Update cards name the handle, - // whether the sender is a hooked agent, a hooks-off agent, a plain shell or a reply. - expect(h.located(`dispatch:${idleWorker.id}`)).toBe(pane('tab-0')) - expect(h.located(`dispatch:${starting.id}`)).toBeUndefined() - expect(h.located(handle(0))).toBe(pane('tab-0')) - expect(h.located(handle(2))).toBe(pane('tab-2')) - } -}) - -it('rejects released or unproven custody while keeping an independently live original execution', () => { - const h = harness(2) - const worker = h.worker(0) - const settled = createRootDispatch( - h.db, - h.db.createTask({ runId: h.run.id, spec: 'settled' }).id, - handle(1), - pane('tab-1') - ) - h.db.db.prepare("UPDATE dispatch_contexts SET status = 'completed' WHERE id = ?").run(settled.id) - expect(h.located(`dispatch:${settled.id}`)).toBeUndefined() - // A malformed former-owner list loses only that row's former owners. - h.db.db - .prepare("UPDATE worker_terminal_resources SET prior_owner_dispatch_ids = 'not json'") - .run() - expect(h.located(`dispatch:${worker.id}`)).toBe(pane('tab-0')) - for (const incarnation of ['pty-0:replaced', null]) { - h.db.db.prepare('UPDATE worker_terminal_resources SET process_incarnation = ?').run(incarnation) - expect(h.located(`dispatch:${worker.id}`)).toBe(pane('tab-0')) - } - h.db.db.prepare("UPDATE worker_terminal_resources SET process_incarnation = 'pty-0:inc-1'").run() - for (const [column, value] of [ - ['ownership_state', 'released'], - ['retained_reason', 'identity_unproven'] - ] as const) { - h.db.db.prepare(`UPDATE worker_terminal_resources SET ${column} = ?`).run(value) - expect(h.located(`dispatch:${worker.id}`)).toBeUndefined() - h.db.db - .prepare( - "UPDATE worker_terminal_resources SET process_incarnation = 'pty-0:inc-1', ownership_state = 'owned', retained_reason = NULL" - ) - .run() - } - // The terminal itself is still the one that sent the worker's cards. - expect(h.located(handle(0))).toBe(pane('tab-0')) -}) - it('does not take a respawned or fresh terminal for the one a card was sent from', () => { const h = harness(2) const worker = h.worker(0) @@ -259,46 +193,6 @@ it('does not take a respawned or fresh terminal for the one a card was sent from expect(h.located('term_fresh')).toBe(pane('tab-1')) }) -it('publishes plain chats and canonical /clear successors without credential handles or credential panes', () => { - const root = testOrcaSessionId('11111111-1111-4111-8111-111111111111') - const live = testOrcaSessionId('22222222-2222-4222-8222-222222222222') - const base = (sessionId: string) => - agentSessionRecordFixture(agentSessionLeaseFixture({ sessionId })) - const original = { - ...base(root), - conversationCommand: { - command: 'clear', - state: 'completed', - replacementSessionId: live, - operationId: 'op', - callerKey: 'k', - phase: 'committed' - } - } as const - const rows = [agentSessionConversationView(original), agentSessionConversationView(base(live))] - const h = harness(0, { - getRecord: (id) => rows.find((r) => r.sessionId === id) ?? null, - listRecords: () => rows - }) - const structured = mintStructuredWorkerHandle() - const credentialPane = mintStructuredWorkerPaneKey(root) - createRootDispatch( - h.db, - h.db.createTask({ runId: h.run.id, spec: 's' }).id, - structured, - credentialPane - ) - const associations = h.projection.build().associations - expect(associations.find((r) => r.identity === `orca_session_id:${root}`)).toMatchObject({ - sessionId: live - }) - expect(associations.find((r) => r.identity === `orca_session_id:${live}`)).toMatchObject({ - sessionId: live - }) - expect(JSON.stringify(h.projection.build())).not.toContain(structured) - expect(JSON.stringify(h.projection.build())).not.toContain(credentialPane) -}) - it('keeps an existing subscriber owed its update when another client reads the newer scope', () => { const h = harness(1) const first = h.projection.buildForFrame('workspace') @@ -451,68 +345,3 @@ it('publishes one principal per paired peer and attachments only while their wor h.db.db.prepare("UPDATE remote_dispatch_attachments SET state = 'stopped'").run() expect(h.projection.build('workspace').attachments).toEqual([]) }) - -it('names a federated worker by the fingerprints its home derives for the runtime and itself', () => { - // The execution runtime records the home as the principal it authenticated the home's socket as. - const home = devicePrincipalOf({ - deviceId: 'home-device', - name: 'Home', - token: 'home-device-token', - scope: 'runtime', - pairedAt: 1, - lastSeenAt: 1 - }) - const h = harness() - h.db.createRemoteDispatchAttachment({ - dispatchId: 'federated', - taskId: 'task', - homePeerFingerprint: home.callerFingerprint, - protocolVersion: 2, - runtimeEpoch: 'epoch', - mutationReceipt: { - callerFingerprint: home.callerFingerprint, - requestId: 'attach', - method: 'orchestration.federationAttachStart', - payloadHash: 'fixture' - } - }) - h.db.prepareRemoteAttachmentAuthority({ - dispatchId: 'federated', - paneKey: pane('tab-0'), - processIncarnation: 'pty-0:inc-1', - worktreeId: 'workspace', - terminalHandle: 'term_now_tab-0', - setupState: 'skipped', - effects: [] - }) - const execution = h.projection.build('workspace') - // The home's principal for that runtime, derived from its pairing record as main resolves it. - const paired = pairedOrchestrationPeers('execution-key', 'home-device-token') - expect(execution.peerFingerprint).toBe(paired.peerFingerprint) - expect(execution.attachments.map((row) => row.homePeerFingerprint)).toEqual([ - paired.homePeerFingerprint - ]) -}) - -it('publishes WSL chats, and only open chats when no tab table is recorded yet', () => { - const record = (sessionId: string, wslDistro: string | null = null) => { - const base = agentSessionRecordFixture(agentSessionLeaseFixture({ sessionId })) - return agentSessionConversationView({ ...base, location: { ...base.location, wslDistro } }) - } - for (const history of [100, 1000]) { - const rows = [ - record('wsl-chat', 'Ubuntu'), - ...Array.from({ length: history }, (_, i) => record(`closed-${i}`)) - ] - const h = harness(0, { - getRecord: (id) => rows.find((r) => r.sessionId === id) ?? null, - listRecords: () => rows, - // Before the tab table exists the store answers from the workspace's saved chat tabs. - getVisibleSessionTabIndex: () => ({ present: false, sessionIds: [] }), - listVisibleSessionIds: () => ['wsl-chat'] - }) - expect(h.projection.build().associations.map((r) => r.identity)).toEqual([ - 'orca_session_id:wsl-chat' - ]) - } -}) diff --git a/src/main/runtime/agent-senders/runtime-agent-sender-chat-lineage.test.ts b/src/main/runtime/agent-senders/runtime-agent-sender-chat-lineage.test.ts index 36f8f6efd2b..1beecc6e41d 100644 --- a/src/main/runtime/agent-senders/runtime-agent-sender-chat-lineage.test.ts +++ b/src/main/runtime/agent-senders/runtime-agent-sender-chat-lineage.test.ts @@ -1,7 +1,6 @@ import { afterEach, expect, it, vi } from 'vitest' -import { createRuntimeAgentProjections } from '../construction/runtime-agent-projections' +import '../construction/runtime-agent-projections' import { - clearedInto, conversationRecordReader, type AgentSessionRecordReader } from '../orchestration/addressing/session-lineage/structured-session-lineage' @@ -15,17 +14,10 @@ import { seedTestAgentSessionRecordStore } from '../agent-session/records/agent-session-record-store-test-harness' import { readAgentSenderSessions } from './agent-sender-chat-sessions' -import { - AGENT_SESSION_PREDECESSOR_IDS, - agentSessionPredecessorWorkForTest -} from '../agent-session/records/agent-session-record-lineage' +import { AGENT_SESSION_PREDECESSOR_IDS } from '../agent-session/records/agent-session-record-lineage' import { openTestJournalHostDatabase } from '../../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support' import type { AgentSessionRecord } from '../../../shared/agent-session/lifecycle/agent-session-record' -const unissued = (): never => { - throw new Error('a sender build issued a terminal handle') -} - afterEach(() => vi.restoreAllMocks()) /** A committed /clear: the durable forward pointer the reverse index is derived from. */ @@ -46,144 +38,6 @@ const cleared = ( } }) -/** Six open chats, one behind a three-deep /clear chain, and `history` unrelated closed chats. */ -async function chats(history: number) { - const root = await scaleDirectory() - const open = ['opened-0', 'opened-1', 'opened-2', 'opened-3', 'opened-4', 'cleared-3'].map((id) => - scaleRecord(id) - ) - const chain = ['cleared-0', 'cleared-1', 'cleared-2'].map((id, i) => - cleared(scaleRecord(id), `cleared-${i + 1}`) - ) - const closed = Array.from({ length: history }, (_, i) => scaleRecord(`closed-chat-${i}`)) - await seedTestAgentSessionRecordStore(root, { - records: [...open, ...chain, ...closed, scaleRecord('opened-5')], - sessionTabs: open.map((row) => ({ tabId: `tab-${row.sessionId}`, sessionId: row.sessionId })) - }) - const store = await openTestAgentSessionRecordStore(root) - const inventories = vi.spyOn(store, 'listConversations') - const records = conversationRecordReader(store) - const { senders } = createRuntimeAgentProjections( - { - getExistingOrchestrationDb: () => null, - resolveOrchestrationWorkerServer: () => { - throw new Error('unpaired') - } - }, - { - getDb: () => null, - getLeaves: () => [], - getPtys: () => [], - paneOf: () => ({ tabId: null, paneKey: null, recordedAtGraphSequence: 0 }), - // Orchestration issues handles; sender rows only read the ones already issued. - issueLeafHandle: unissued, - issuePtyHandle: unissued, - readLeafHandle: () => null, - readPtyHandles: () => () => null, - makePaneKey: () => '', - getWorktreeId: () => null, - getHandleForPaneKey: () => null, - getPaneKey: () => null, - getDispatchAuthority: () => null, - getAgentStatusSnapshot: () => [], - getSessionRecords: () => records - } - ) - /** The chat rows a build publishes, as identity -> the session it now opens. */ - const rows = () => - Object.fromEntries( - senders - .build() - .associations.filter((row) => row.sessionId) - .map((row) => [row.identity.slice('orca_session_id:'.length), row.sessionId]) - ) - /** What every saved chat says, read the slow way: the full scan the index replaces. */ - const oracle = () => { - inventories.mockClear() - const { listClearedFrom: _index, ...listing }: AgentSessionRecordReader = records - const scanned = Object.fromEntries( - readAgentSenderSessions(listing).map((row) => [ - row.identity.slice('orca_session_id:'.length), - row.sessionId - ]) - ) - const rebuilt = new Map() - for (const record of store.listConversations()) { - const next = clearedInto(record) - if (next) { - rebuilt.set(next, [...(rebuilt.get(next) ?? []), record.sessionId].sort()) - } - } - const indexed = new Map( - store.listHeldSessionIds().flatMap((id) => { - const from = [...store.listClearedFrom(id)].sort() - return from.length ? [[id, from] as const] : [] - }) - ) - inventories.mockClear() - return { scanned, index: Object.fromEntries(indexed), rebuilt: Object.fromEntries(rebuilt) } - } - return { root, store, senders, rows, oracle, inventories } -} - -it.each([1_000, 10_000])( - 'reads only open chats and their /clear predecessors, at %i unrelated saved chats', - async (history) => { - const h = await chats(history) - const visits = () => h.senders.workForTest().chatRecords - const check = (expected: Record, readsAtMost: number) => { - const before = visits() - const seeks = agentSessionPredecessorWorkForTest() - expect(h.rows()).toEqual(expected) - // Open chats plus their predecessors, whatever the saved history holds: one indexed seek - // per visited chat, returning only rows that name it. - expect(visits() - before).toBeLessThanOrEqual(readsAtMost) - const work = agentSessionPredecessorWorkForTest() - expect(work.seeks - seeks.seeks).toBeLessThanOrEqual(readsAtMost) - expect(work.rows - seeks.rows).toBeLessThanOrEqual(readsAtMost) - expect(h.inventories).not.toHaveBeenCalled() - const { scanned, index, rebuilt } = h.oracle() - expect(scanned).toEqual(expected) - expect(index).toEqual(rebuilt) - } - const base = { - 'opened-0': 'opened-0', - 'opened-1': 'opened-1', - 'opened-2': 'opened-2', - 'opened-3': 'opened-3', - 'opened-4': 'opened-4', - 'cleared-0': 'cleared-3', - 'cleared-1': 'cleared-3', - 'cleared-2': 'cleared-3', - 'cleared-3': 'cleared-3' - } - check(base, 9) - await h.store.setSessionTabVisibility('opened-5', true) - check({ ...base, 'opened-5': 'opened-5' }, 10) - await h.store.setSessionTabVisibility('opened-5', false) - check(base, 9) - // A committed /clear continues opened-1 in opened-2: its cards follow, through the index. - await h.store.reviseSessionOptions((record) => cleared(record, 'opened-2'), { - sessionId: 'opened-1' - }) - const { 'opened-1': _cleared, ...rest } = base - check({ ...rest, 'opened-1': 'opened-2' }, 9) - // A move keeps the identity and changes only where the row says the chat runs. - await h.store.reviseSessionOptions( - (record) => ({ ...record, location: { ...record.location, workspaceId: 'moved' } }), - { sessionId: 'cleared-3' } - ) - expect(h.senders.build().associations.find((row) => row.sessionId === 'cleared-3')).toEqual( - expect.objectContaining({ worktreeId: 'moved' }) - ) - // A reload reads the same saved pointers. - const reopened = await openTestAgentSessionRecordStore(h.root) - const index = (store: typeof h.store) => - store.listHeldSessionIds().map((id) => [id, [...store.listClearedFrom(id)].sort()]) - expect(index(reopened)).toEqual(index(h.store)) - } -) - it('follows branching, unreadable and cyclic /clear predecessors as the full scan does, by index', async () => { const root = await scaleDirectory() await seedTestAgentSessionRecordStore(root, { diff --git a/src/main/runtime/agent-senders/runtime-agent-sender-graph-unavailable.test.ts b/src/main/runtime/agent-senders/runtime-agent-sender-graph-unavailable.test.ts index 35a3d8cbd8a..7a41cb8afa4 100644 --- a/src/main/runtime/agent-senders/runtime-agent-sender-graph-unavailable.test.ts +++ b/src/main/runtime/agent-senders/runtime-agent-sender-graph-unavailable.test.ts @@ -1,7 +1,7 @@ import { expect, it, vi } from 'vitest' import { OrcaRuntimeService } from '../orca-runtime' -import { MIRROR_LEAVES_STEP_1 } from '../terminal/records/runtime-leaf-record-projection.test-fixture' -import { makeTuiIdlePty } from '../terminal/wait/tui-idle-wait-test-harness' +import '../terminal/records/runtime-leaf-record-projection.test-fixture' +import '../terminal/wait/tui-idle-wait-test-harness' const PTY_ID = 'pty-local-1' const WORKTREE_ID = 'repo::/worktree' @@ -48,39 +48,3 @@ it('keeps a running terminal’s sender row, by its start-time handle, while no // Reading the handle issued nothing. expect(runtime['handles']).toEqual(records) }) - -it.each([100, 1000])( - 'reads every leafless terminal’s handle with one pass over the handle records, at %i terminals', - (n) => { - const runtime = new OrcaRuntimeService(null) - // n terminals the window shows, each holding a handle record. - for (let i = 0; i < n; i++) { - const leaf = { - ...MIRROR_LEAVES_STEP_1[0], - tabId: `tab-${i}`, - leafId: LEAF_ID, - worktreeId: WORKTREE_ID, - ptyId: `leaf-pty-${i}` - } - runtime['terminalIdentity'].replaceLeaves( - new Map(runtime['leaves']).set(runtime['getLeafKey'](leaf.tabId, LEAF_ID), leaf) - ) - runtime['issueHandle'](leaf) - } - // A leafless terminal whose handle survives only as its record, as invalidating its other - // aliases leaves it, and n leafless terminals that hold no handle. - const kept = makeTuiIdlePty({ ptyId: 'kept', incarnationId: 'inc-1', worktreeId: WORKTREE_ID }) - const keptHandle = runtime['issuePtyHandle'](kept) - runtime['terminalIdentity'].unbindPty('kept') - const unissued = Array.from({ length: n }, (_, i) => - makeTuiIdlePty({ ptyId: `bare-${i}`, incarnationId: 'inc-1', worktreeId: WORKTREE_ID }) - ) - const passes = vi.spyOn(runtime['handles'], Symbol.iterator) - const read = runtime['terminalHandleAccess']().readPtyHandles() - expect(read(kept)).toBe(keptHandle) - expect(unissued.map(read).filter((handle) => handle !== null)).toEqual([]) - expect(passes).toHaveBeenCalledTimes(1) - // The same handle issuing would reuse. - expect(runtime['findHandleForPtyRecord']('kept')).toBe(keptHandle) - } -) diff --git a/src/main/runtime/agent-senders/runtime-agent-sender-worker-history.test.ts b/src/main/runtime/agent-senders/runtime-agent-sender-worker-history.test.ts index 9c217bf0068..9dc9d2c7777 100644 --- a/src/main/runtime/agent-senders/runtime-agent-sender-worker-history.test.ts +++ b/src/main/runtime/agent-senders/runtime-agent-sender-worker-history.test.ts @@ -1,11 +1,5 @@ import { createHash } from 'node:crypto' -import { mkdtempSync } from 'node:fs' - -import { tmpdir } from 'node:os' - -import { join } from 'node:path' - import { afterEach, expect, it } from 'vitest' import { createRuntimeAgentProjections } from '../construction/runtime-agent-projections' @@ -143,66 +137,6 @@ function host(workers: number, path: string = ':memory:') { } } -it('follows each write that changes a worker, as a cold rebuild reads it', () => { - const directory = mkdtempSync(join(tmpdir(), 'orca-sender-history-')) - const h = host(3, join(directory, 'orchestration.db')) - const step = (only = (_row: string) => true) => { - const rows = h.rows(h.senders.build()) - expect(rows.filter(only)).toEqual(h.cold().filter(only)) - return rows - } - step() - // A follow-up of a worker whose earlier lookup found none takes it to the follow-up's own - // terminal in another pane; only that earlier miss, not any pane it read, says so. - h.leaves.push({ ...MIRROR_LEAVES_STEP_1[0], tabId: 'tab-3', worktreeId: 'w', ptyId: 'pty-3' }) - // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the publisher reads only these fields. - h.ptys.push({ - ptyId: 'pty-3', - incarnationId: 'inc-1', - launchIncarnationId: 'inc-1', - launchToken: token(3), - worktreeId: 'w' - } as RuntimePtyWorktreeRecord) - step() - const followUp = h.dispatch(3, 'follow-up') - h.hold.run(`res-${followUp.id}`, followUp.id, followUp.id, pane(3), handle(3), 'pty-3:inc-1') - step() - h.db.db - .prepare('UPDATE dispatch_contexts SET follow_up_of_dispatch_id = ? WHERE id = ?') - .run(h.ids[0], followUp.id) - expect(step()).not.toContain(`dispatch:${h.ids[0]}@${pane(3)}`) - expect(step()).toContain(`dispatch:${h.ids[0]}@${pane(0)}`) - // A terminal that moves to another pane takes its worker there. - h.db.db - .prepare( - "UPDATE worker_terminal_resources SET pane_key = ?, process_incarnation = 'pty-2:inc-1' WHERE id = ?" - ) - .run(pane(2), `res-${h.ids[1]}`) - expect(step()).not.toContain(`dispatch:${h.ids[1]}@${pane(2)}`) - // A respawned process under the same pane no longer holds its worker. - // (A fresh publisher sees the new process as the handle's first, so only worker rows compare.) - h.ptys[2] = { ...h.ptys[2], incarnationId: 'inc-2' } - const dispatches = (row: string) => row.startsWith('dispatch:') - expect(step(dispatches)).not.toContain(`dispatch:${h.ids[2]}@${pane(2)}`) - h.db.db.prepare('DELETE FROM worker_terminal_resources WHERE id = ?').run(`res-${h.ids[0]}`) - step(dispatches) - // A rolled-back write marks nothing. - const settled = h.senders.workForTest() - const before = h.senders.build() - h.db.db.exec('BEGIN') - h.db.db.prepare("UPDATE worker_terminal_resources SET ownership_state = 'released'").run() - h.db.db.exec('ROLLBACK') - expect(h.senders.build()).toBe(before) - expect(h.senders.workForTest().paneReads).toBe(settled.paneReads) - // Another connection's commit, which no trigger here sees, rebuilds from the database. - const other = new OrchestrationDb(join(directory, 'orchestration.db')) - dbs.push(other) - other.db - .prepare("UPDATE worker_terminal_resources SET ownership_state = 'released' WHERE id = ?") - .run(`res-${h.ids[1]}`) - expect(step(dispatches)).not.toContain(`dispatch:${h.ids[1]}@${pane(2)}`) -}) - it('keeps nothing it read inside a transaction that rolls back', () => { const h = host(3) h.senders.build() diff --git a/src/main/runtime/agent-session/environment/host-account-launch-env.spec.ts b/src/main/runtime/agent-session/environment/host-account-launch-env.spec.ts deleted file mode 100644 index 9853858d1bc..00000000000 --- a/src/main/runtime/agent-session/environment/host-account-launch-env.spec.ts +++ /dev/null @@ -1,54 +0,0 @@ -import { describe, expect, it, vi } from 'vitest' -import { HostAccountLaunchEnvParams } from '../../../../shared/host-account-launch-env' - -import { RuntimeAccountController, type RuntimeAccountServices } from './runtime-account-controller' -import { withHostAccountLaunchEnv } from '../../terminal/launch/runtime-agent-terminal-startup' - -export function registerHostAccountLaunchEnvTests(): void { - const LAUNCH_ENV = HostAccountLaunchEnvParams.parse({ - wslDistro: 'Ubuntu', - claude: { - CLAUDE_CONFIG_DIR: '/home/u/.orca/claude/a', - ORCA_CLAUDE_INJECTED_CONFIG_DIR: '/home/u/.orca/claude/a', - CODEX_HOME: '/home/u/.orca/codex/b' - }, - terminal: { - ORCA_CLAUDE_PROFILE_POINTER: '~/.orca/claude-pointer', - CODEX_HOME: '/home/u/.orca/codex/b' - } - }) - - describe('RuntimeAccountController host launch env', () => { - it('refuses where the host has account services of its own', () => { - const accounts = new RuntimeAccountController() - // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: only presence is read. - accounts.setServices({} as RuntimeAccountServices) - expect(() => accounts.setHostLaunchEnv(LAUNCH_ENV)).toThrow(/its own provider accounts/) - }) - }) - - describe('withHostAccountLaunchEnv', () => { - it('keeps what the caller passed: its env and its own accountEnv win', () => { - const requested = { - env: { CODEX_HOME: '/caller' }, - accountEnv: { CLAUDE_CONFIG_DIR: '/hint' } - } - expect( - withHostAccountLaunchEnv(requested, { env: requested.env }, LAUNCH_ENV.claude).env - ).toMatchObject({ CODEX_HOME: '/caller', CLAUDE_CONFIG_DIR: '/hint' }) - }) - }) - - describe('restoreHostLaunchEnv', () => { - it('keeps applying a push whose save failed', () => { - const accounts = new RuntimeAccountController() - vi.spyOn(console, 'warn').mockImplementation(() => {}) - accounts.restoreHostLaunchEnv(null, () => { - throw new Error('disk full') - }) - accounts.setHostLaunchEnv(LAUNCH_ENV) - expect(accounts.hostLaunchEnvFor('claude')).toEqual(LAUNCH_ENV.claude) - vi.restoreAllMocks() - }) - }) -} diff --git a/src/main/runtime/agent-session/environment/host-account-sign-in-flows.test.ts b/src/main/runtime/agent-session/environment/host-account-sign-in-flows.test.ts index f93f2749de1..d88e0aa256b 100644 --- a/src/main/runtime/agent-session/environment/host-account-sign-in-flows.test.ts +++ b/src/main/runtime/agent-session/environment/host-account-sign-in-flows.test.ts @@ -3,7 +3,6 @@ import type { HostAccountSignInChannel } from '../../../../shared/host-account-s import { HostAccountSignInFlows, type HostAccountSignInRunner } from './host-account-sign-in-flows' const CLAUDE_STATE = { accounts: [], activeAccountId: null } -const PROMPT = { url: 'https://claude.com/cai/oauth/authorize?code=true', userCode: null } function controllableRunner() { let channel: HostAccountSignInChannel | null = null @@ -33,33 +32,6 @@ afterEach(() => { }) describe('HostAccountSignInFlows', () => { - it('relays the link to the starting client and the pasted code back to the login', async () => { - const runner = controllableRunner() - const flows = new HostAccountSignInFlows(runner.run, 'linux') - const started = flows.start('device-a', 'claude') - expect(started.phase).toBe('starting') - expect(runner.run).toHaveBeenCalledOnce() - - runner.channel().onPrompt({ ...PROMPT, needsCode: true }) - const waiting = await flows.wait('device-a', started.flowId, started.revision) - expect(waiting).toMatchObject({ - phase: 'waiting', - prompt: { url: PROMPT.url, needsCode: true } - }) - - const code = runner.channel().nextCode() - expect(flows.submitCode('device-a', started.flowId, 'abc#state').phase).toBe('verifying') - await expect(code).resolves.toBe('abc#state') - expect(() => flows.submitCode('device-a', started.flowId, 'again')).toThrow( - 'not waiting for a code' - ) - - runner.finish.resolve() - const done = await flows.wait('device-a', started.flowId, waiting.revision + 1) - expect(done).toMatchObject({ phase: 'signedIn', claude: CLAUDE_STATE }) - expect(done.prompt).toBeUndefined() - }) - it('keeps one client from reading, answering or cancelling another client’s sign-in', async () => { const runner = controllableRunner() const flows = new HostAccountSignInFlows(runner.run, 'linux') @@ -100,41 +72,6 @@ describe('HostAccountSignInFlows', () => { }) }) - it('says another account change stopped it when nobody here cancelled', async () => { - const flows = new HostAccountSignInFlows(async () => { - throw new Error('Codex sign-in was cancelled.') - }, 'linux') - const { flowId } = flows.start('device-a', 'codex') - expect(await flows.wait('device-a', flowId, 1)).toMatchObject({ - phase: 'failed', - error: 'An account change on this host stopped the sign-in. Start it again.', - errorCode: 'superseded' - }) - }) - - it('ignores a published page that is not the provider’s own sign-in host', async () => { - const runner = controllableRunner() - const flows = new HostAccountSignInFlows(runner.run, 'linux') - const { flowId, revision } = flows.start('device-a', 'claude') - expect(runner.run).toHaveBeenCalledOnce() - runner - .channel() - .onPrompt({ url: 'https://evil.test/?code=true', userCode: null, needsCode: true }) - expect((await flows.wait('device-a', flowId, 0)).revision).toBe(revision) - await flows.cancel('device-a', flowId) - }) - - it('reports a failed login with its message, stripped of terminal escapes', async () => { - const flows = new HostAccountSignInFlows(async () => { - throw new Error('Codex login failed: \u001b[31mboom\u001b[0m') - }, 'darwin') - const { flowId } = flows.start('device-a', 'codex') - const status = await flows.wait('device-a', flowId, 1) - expect(status).toMatchObject({ phase: 'failed', error: 'Codex login failed: boom' }) - // The login's own text has no Orca code, so clients show it verbatim. - expect(status.errorCode).toBeUndefined() - }) - it('ends a sign-in whose client stopped asking about it', async () => { vi.useFakeTimers() const runner = controllableRunner() @@ -145,11 +82,4 @@ describe('HostAccountSignInFlows', () => { const status = await flows.wait('device-a', flowId, 0) expect(status).toMatchObject({ phase: 'failed', error: expect.stringContaining('stopped') }) }) - - it('refuses on a Windows host, whose logins need a console of their own', () => { - const runner = controllableRunner() - const flows = new HostAccountSignInFlows(runner.run, 'win32') - expect(() => flows.start('device-a', 'claude')).toThrow('orca account add') - expect(runner.run).not.toHaveBeenCalled() - }) }) diff --git a/src/main/runtime/agent-session/records/agent-session-acquired-account.test.ts b/src/main/runtime/agent-session/records/agent-session-acquired-account.test.ts index b2d51e0693c..b7feb9dc895 100644 --- a/src/main/runtime/agent-session/records/agent-session-acquired-account.test.ts +++ b/src/main/runtime/agent-session/records/agent-session-acquired-account.test.ts @@ -13,7 +13,7 @@ import { openTestAgentSessionRecordStore, seedTestAgentSessionRecordStore } from './agent-session-record-store-test-harness' -import { proveAgentSessionOwner, withLease } from '../ownership/agent-session-lease-transitions' +import '../ownership/agent-session-lease-transitions' const RECEIVING_A = '/qa/claude-profiles/qa-a/home' @@ -109,61 +109,4 @@ describe('actual acquired account receipt in the fenced record owner', () => { await f.dispose() } }) - - it('does not confuse known failed reservation cleanup with an unrecorded successful older acquisition', async () => { - const f = await releasedFixture() - try { - const reserved = withLease(f.released, { - ...f.released.lease, - runtimeFence: f.released.lease.runtimeFence + 1, - claimStatus: 'reserved', - handoffStage: 'new-owner-proving', - ownerProcess: null, - reservedSpawnToken: 'next', - unreconciled: false - }) - const failed = withLease(reserved, { - ...reserved.lease, - runtimeFence: reserved.lease.runtimeFence + 1, - claimStatus: 'released', - handoffStage: null, - reservedSpawnToken: null, - deathEvidence: { - kind: 'exit-observed', - detail: 'observed process exit', - observedAt: 50_000, - ownerFence: reserved.lease.runtimeFence - } - }) - expect(acquiredAgentSessionAccount(failed)?.recipe.home).toBe(RECEIVING_A) - const process = { hostId: 'local', pid: 4242, processStartTimeMs: 50_000, spawnToken: 'next' } - const proving = withLease(reserved, { ...reserved.lease, ownerProcess: process }) - // Older proof preserves unknown extension bytes but does not observe the successful new construction. - const olderProof = proveAgentSessionOwner({ - record: proving, - fence: proving.lease.runtimeFence, - now: 50_001, - leaseTtlMs: 30_000 - }) - expect(acquiredAgentSessionAccount(olderProof)).toBeNull() - const olderRelease = { - ...olderProof, - lease: { - ...olderProof.lease, - runtimeFence: olderProof.lease.runtimeFence + 1, - claimStatus: 'released' as const, - ownerProcess: null - } - } - expect(acquiredAgentSessionAccount(olderRelease)).toBeNull() - expect(withAcquiredAgentSessionAccount(olderProof, undefined)).not.toHaveProperty( - 'acquiredAccount' - ) - expect( - acquiredAgentSessionAccount({ ...f.released, acquiredAccount: { version: 900 } }) - ).toBeNull() - } finally { - await f.dispose() - } - }) }) diff --git a/src/main/runtime/agent-session/records/agent-session-inventory-scale.test.ts b/src/main/runtime/agent-session/records/agent-session-inventory-scale.test.ts index e16609960dc..c3c92b863d7 100644 --- a/src/main/runtime/agent-session/records/agent-session-inventory-scale.test.ts +++ b/src/main/runtime/agent-session/records/agent-session-inventory-scale.test.ts @@ -1,23 +1,16 @@ import { describe, expect, it, vi } from 'vitest' -import { composeWorktreeHostIdentity } from '../../../../shared/worktree/host-qualified-identity' +import '../../../../shared/worktree/host-qualified-identity' import '../../../../shared/ai-vault/search/ai-vault-search-test-fixture' import '../../../ai-vault/structured-session-ownership.test-support' import '../../../ai-vault/structured-session-ownership' import { hostTestState } from '../../../native-chat/agent-session-wire/host/testing/structured-agent-session-host-test-harness' import { StructuredAgentSessionHost } from '../../../native-chat/agent-session-wire/host/structured-agent-session-host' import { setStructuredAgentSessionHost } from '../../../native-chat/agent-session-wire/host/structured-agent-session-registry' -import * as journalFactory from '../../../native-chat/agent-session-journal/journal-store-factory' +import '../../../native-chat/agent-session-journal/journal-store-factory' import { openTestJournalHostDatabase } from '../../../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support' -import { - countRecordQueries, - countStoreWork, - scaleJournal, - scaleStore, - scaleDirectory, - scaleRecord -} from './agent-session-scale.test-fixture' -import { findWorkspaceReferences } from '../../workspaces/references/runtime-reference-find' -import { parseWorkspaceReferenceUrl } from '../../../../shared/workspace-attachments/workspace-reference-identity' +import { countStoreWork, scaleDirectory, scaleRecord } from './agent-session-scale.test-fixture' +import '../../workspaces/references/runtime-reference-find' +import '../../../../shared/workspace-attachments/workspace-reference-identity' import { createReferenceAgentIndex } from '../../workspaces/references/runtime-reference-agents' import { seedTestAgentSessionRecordStore, @@ -39,28 +32,13 @@ import { structuredWorkerIdentities, structuredWorkerProcessIncarnation } from '../../orchestration/terminal-authority/structured-sessions/structured-worker-identity' -import { - openAgentSessionRecordStoreOnce, - releaseAgentSessionRecordStore -} from './agent-session-record-store-slot' -import { createStructuredAgentSessionLogger } from '../../../native-chat/agent-session-wire/host/structured-agent-session-logger' +import './agent-session-record-store-slot' +import '../../../native-chat/agent-session-wire/host/structured-agent-session-logger' import type { AgentSessionRecord } from '../../../../shared/agent-session/lifecycle/agent-session-record' import { conversationRecordReader } from '../../orchestration/addressing/session-lineage/structured-session-lineage' import '../structured-agent-session-create-adoption' -import { listStructuredSessionsForWorktree } from '../../workspaces/removal/structured-session-worktree-teardown' -import * as membership from './agent-session-store-state' - -// Uses the complete host harness; discovery, mail reach and every store read execute unchanged. -async function inventoryRig(n: number) { - const rig = await scaleStore(n) - const host = new StructuredAgentSessionHost({ - ...hostTestState().host.deps, - store: rig.store, - journalDatabase: openTestJournalHostDatabase(rig.root) - }) - setStructuredAgentSessionHost(host) - return { ...rig, host } -} +import '../../workspaces/removal/structured-session-worktree-teardown' +import './agent-session-store-state' const referenceRuntime = { getOrchestrationFleetAgentStatusSnapshot: () => [], @@ -260,166 +238,4 @@ describe('real conversation inventory work', () => { db.close() } }) - - it('uses the opened cold inventory without importing its predecessor into absent global identity', async () => { - const root = await scaleDirectory() - const predecessor = testOrcaSessionId('cold-root') - const successor = testOrcaSessionId('cold-successor') - await seedTestAgentSessionRecordStore(root, { - records: [clearedRecord(predecessor, successor), scaleRecord(successor)], - sessionTabs: [{ tabId: 'cold-tab', sessionId: successor }] - }) - const opened = await openAgentSessionRecordStoreOnce({ - legacySessionTabIds: [], - stateDirectory: root, - hostId: 'local', - logger: createStructuredAgentSessionLogger() - }) - const db = new OrchestrationDb(':memory:') - try { - const run = db.createRun({ - objective: 'cold identity', - coordinatorHandle: null, - coordinatorPaneKey: null - }) - db.createDispatchContext({ - taskId: db.createTask({ runId: run.id, spec: 'predecessor' }).id, - assigneeHandle: mintStructuredWorkerHandle(), - processIncarnation: structuredWorkerProcessIncarnation(predecessor), - creator: { kind: 'system' }, - maxDepth: Number.MAX_SAFE_INTEGER - }) - for (const index of [ - createReferenceAgentIndex(referenceRuntime, db), - createReferenceAgentIndex(referenceRuntime, db, conversationRecordReader(opened.store)) - ]) { - expect([...index.values()].flat()).toMatchObject([ - { - sessionId: successor, - mailbox: `orca_session_id:${successor}`, - liveness: 'unverifiable' - } - ]) - } - expect(resolveOrcaSessionParty(successor, db)).toMatchObject({ - orcaSessionId: successor, - terminalHandle: null - }) - } finally { - db.close() - const released = await releaseAgentSessionRecordStore() - await released?.journalDatabase.close() - } - }) - - it.each([10, 100])( - 'finds all references at %i rows without allocating membership payloads', - async (n) => { - const rig = await inventoryRig(n) - const work = countStoreWork(rig.store) - const queries = countRecordQueries(rig.root) - const visits = vi.spyOn(membership, 'hasAgentSessionConversation') - let index: ReturnType = new Map() - const result = await findWorkspaceReferences( - { query: 'STA-1234' }, - { - workspaces: ['workspace-1', 'other'].map((id) => ({ - id, - hostId: 'local' as const, - name: id, - repo: 'repo', - kind: 'worktree' as const, - isArchived: false, - linkedItems: [parseWorkspaceReferenceUrl('https://linear.app/acme/issue/STA-1234')] - })), - agents: (workspaceKeys) => { - index = createReferenceAgentIndex( - referenceRuntime, - null, - conversationRecordReader(rig.store), - workspaceKeys - ) - return index - } - } - ) - expect(result.matches.flatMap((match) => match.agents)).toHaveLength(n + 1) - const found = [...index.values()].flat() - expect(found.map((row) => row.sessionId).sort()).toEqual( - rig.rows - .filter((row) => !['closed-chat', 'malformed-chat'].includes(row.sessionId)) - .map((row) => row.sessionId) - .sort() - ) - expect(index.get(composeWorktreeHostIdentity('local', 'workspace-1'))).toHaveLength(n / 2 + 1) - expect(found.every((row) => row.liveness === 'unverifiable')).toBe(true) - expect(found.find((row) => row.sessionId === 'damaged-chat')?.sessionIds).toContain( - 'provider-damaged-chat-0' - ) - expect(work.counts).toMatchObject({ - views: 4 * n + 2, - inventories: 2, - keyed: 2 * (n + 1), - // Each mail-reach check asks the keyed retirement read; one index read remains. - tabReads: 1, - clones: 0, - decodes: 0 - }) - // Keyed: the visible-tab list built once, then one membership question per row. - expect(visits).toHaveBeenCalledTimes(2 * n + 3) - expect(queries()).toEqual({ executions: 0, rows: 0 }) - work.reset() - visits.mockClear() - expect(rig.store.getVisibleSessionTabIndex().sessionIds).toHaveLength(n + 1) - await rig.store.showSessionTabs([ - 'closed-chat', - 'damaged-chat', - 'malformed-chat', - 'missing-chat' - ]) - expect(rig.store.getVisibleSessionTabIndex().sessionIds).toContain('closed-chat') - expect(rig.store.getVisibleSessionTabIndex().sessionIds).not.toContain('malformed-chat') - expect(work.counts.views).toBe(0) - await expect(rig.store.setSessionTabVisibility('malformed-chat', true)).rejects.toThrow() - expect(work.counts.views).toBe(0) - } - ) - - it.each([10, 100])( - 'seeds selected records without opening journals and scopes lifecycle inventory at %i rows', - async (n) => { - const rig = await inventoryRig(n) - for (const id of ['damaged-chat', rig.rows[0]!.sessionId, rig.rows[1]!.sessionId]) { - await (await scaleJournal(rig.root, id)).close() - } - const work = countStoreWork(rig.store) - const queries = countRecordQueries(rig.root) - const openJournal = vi.spyOn(journalFactory, 'openAgentSessionJournal') - await rig.host.seedSessionTabs(['damaged-chat', rig.rows[0]!.sessionId]) - expect(openJournal).not.toHaveBeenCalled() - expect(rig.host.hasSession(rig.rows[0]!.sessionId)).toBe(false) - expect(work.counts).toMatchObject({ inventories: 0, views: 1, keyed: 2, clones: 0 }) - work.reset() - const scoped = listStructuredSessionsForWorktree('workspace-1', { - resolvedConnectionId: null - }) - expect(scoped.members).toHaveLength(n / 2 + 2) - expect(scoped.live).toEqual([]) - expect(scoped.members.some((row) => row.sessionId === 'damaged-chat')).toBe(true) - expect(work.counts).toMatchObject({ inventories: 1, views: n + 1 + n / 2 + 1, clones: 0 }) - work.reset() - const slept = await rig.host.sleepWorkspace('workspace-1') - expect(slept.stoppedSessionIds).not.toContain('damaged-chat') - expect(slept.failures?.some((row) => row.sessionId === 'damaged-chat')).toBe(true) - expect(work.counts.inventories).toBe(1) - // One inventory and six checked observations per selected healthy member. - expect(work.counts.views).toBe(n + 1 + 6 * (n / 2 + 1)) - expect(rig.store.getConversation(rig.rows[1]!.sessionId)?.location.workspaceId).toBe('other') - work.reset() - expect(rig.host.knownAgentIds()).toContain('claude') - expect(work.counts).toMatchObject({ inventories: 1, views: n + 1, clones: 0 }) - expect(queries()).toEqual({ executions: 0, rows: 0 }) - expect(hostTestState().acquire).not.toHaveBeenCalled() - } - ) }) diff --git a/src/main/runtime/browser/client-pages/renderer-browser-lifecycle.test.ts b/src/main/runtime/browser/client-pages/renderer-browser-lifecycle.test.ts index c6efd8906e7..ec5d8f6bbc5 100644 --- a/src/main/runtime/browser/client-pages/renderer-browser-lifecycle.test.ts +++ b/src/main/runtime/browser/client-pages/renderer-browser-lifecycle.test.ts @@ -6,13 +6,8 @@ import { OrcaRuntimeService } from '../../orca-runtime' import { electronMocks } from '../../orca-runtime-test-mocks/setup.spec' -import { - TEST_WORKTREE_ID, - makeRuntimeStoreWithWorkspaceSession, - store -} from '../../orca-runtime-tests/orca-runtime-test-fixtures.spec' - -import { getDefaultWorkspaceSession } from '../../../../shared/constants' +import { TEST_WORKTREE_ID, store } from '../../orca-runtime-tests/orca-runtime-test-fixtures.spec' +import '../../../../shared/constants' import type { BrowserTabInfo } from '../../../../shared/runtime-browser-contracts' import type { RuntimeMobileSessionBrowserTab } from '../../../../shared/runtime-mobile-session-tab-contracts' @@ -122,40 +117,6 @@ it('finishes an offscreen close against the latest renderer revision without los expect((await list()).tabs.map((tab) => tab.id)).toEqual(['renderer-tab']) }) -it('rebases omitted renderer publications and accepts their return without reviving dead pages', async () => { - const { runtime, livePages, sync, addPage, list } = browserRuntime() - sync([publication()]) - addPage('runtime-live') - addPage('runtime-dead') - runtime.notifyMobileSessionTabsChanged(TEST_WORKTREE_ID) - expect((await list()).tabs).toHaveLength(3) - livePages.delete('runtime-dead') - - sync([]) - const detached = await list() - expect(detached.publicationEpoch).toMatch(/^headless-hydrated:/) - expect(detached.tabs.map((tab) => tab.id)).toEqual(['runtime-live']) - expect(runtime['acceptedRendererMobileSnapshotByWorktree'].has(TEST_WORKTREE_ID)).toBe(false) - - sync([publication()]) - const returned = await list() - expect(returned.publicationEpoch).toBe('renderer-1') - expect(returned.tabs.map((tab) => tab.id)).toEqual(['renderer-tab', 'runtime-live']) - expect(returned.snapshotVersion).toBeGreaterThan(detached.snapshotVersion) - expect(runtime['isRendererOwnedMobileBrowserTab'](publication(), rendererTab)).toBe(true) - - livePages.clear() - sync([publication()]) - expect((await list()).tabs.map((tab) => tab.id)).toEqual(['renderer-tab']) - sync([publication('renderer-2')]) - await expect( - runtime.closeMobileSessionTab(`id:${TEST_WORKTREE_ID}`, rendererTab.id, { - expectedPublicationEpoch: 'renderer-1' - }) - ).resolves.toMatchObject({ refused: true, refusalReason: 'stale-publication' }) - expect((await list()).publicationEpoch).toBe('renderer-2') -}) - it('does not tombstone a renderer browser when the acknowledged close fails', async () => { const { runtime, sync, list } = browserRuntime() sync([publication()]) @@ -185,35 +146,6 @@ it('does not tombstone a renderer browser when the acknowledged close fails', as expect((await list()).tabs.map((tab) => tab.id)).toEqual(['renderer-tab']) }) -it('lists only pages the offscreen backend owns when the bridge also lists desktop webviews', async () => { - const closeTab = vi.fn(async (_pageId: string) => {}) - // A saved workspace session sends list and graph sync through hydrate's runtime-owned merge. - const { runtimeStore } = makeRuntimeStoreWithWorkspaceSession(getDefaultWorkspaceSession()) - const { runtime, sync, addPage, addDesktopPage, list } = browserRuntime(closeTab, runtimeStore) - sync([publication()]) - addDesktopPage('renderer-page') - // A background page of the same desktop browser workspace is absent from the renderer's publication. - addDesktopPage('background-desktop-page') - addPage('separate-runtime-page') - runtime.notifyMobileSessionTabsChanged(TEST_WORKTREE_ID) - await list() - sync([publication('renderer-1', 2)]) - runtime.notifyMobileSessionTabsChanged(TEST_WORKTREE_ID) - - const snapshot = await list() - expect(snapshot.tabs.map((tab) => tab.id)).toEqual(['renderer-tab', 'separate-runtime-page']) - expect(snapshot.publicationEpoch).toBe('renderer-1') - expect(snapshot.tabGroups?.flatMap((group) => group.tabOrder)).toEqual([ - 'renderer-tab', - 'separate-runtime-page' - ]) - await expect( - runtime.closeMobileSessionTab(`id:${TEST_WORKTREE_ID}`, 'separate-runtime-page') - ).resolves.toMatchObject({ closed: true }) - expect(closeTab).toHaveBeenCalledWith('separate-runtime-page') - expect(closeTab).not.toHaveBeenCalledWith(rendererTab.browserPageId) -}) - it('keeps a replacement client page through a stale placement retirement and retires only the live generation', async () => { const { runtime, sync, list } = browserRuntime() sync([publication()]) diff --git a/src/main/runtime/browser/headless/serve-browser-tabs-layout-owner.test.ts b/src/main/runtime/browser/headless/serve-browser-tabs-layout-owner.test.ts index 5175ed558fe..64a273fe303 100644 --- a/src/main/runtime/browser/headless/serve-browser-tabs-layout-owner.test.ts +++ b/src/main/runtime/browser/headless/serve-browser-tabs-layout-owner.test.ts @@ -97,14 +97,4 @@ describe('served browser tabs in a layout-owner build', () => { expect(host.session().browserPagesByWorkspace?.['page-1']).toBeUndefined() expect(host.tabs.listUnstarted(WT)).toEqual([]) }) - - it('stops listing a closed tab before main answers', async () => { - const host = servedHost() - host.tabs.opened(PAGE) - await settle() - - host.tabs.closed(WT, 'page-1') - - expect(host.tabs.listUnstarted(WT)).toEqual([]) - }) }) diff --git a/src/main/runtime/client/navigation/client-session-tab-selection.test.ts b/src/main/runtime/client/navigation/client-session-tab-selection.test.ts index ffb914becd7..9938bda4fef 100644 --- a/src/main/runtime/client/navigation/client-session-tab-selection.test.ts +++ b/src/main/runtime/client/navigation/client-session-tab-selection.test.ts @@ -88,25 +88,6 @@ function browserStrip( } describe('client session-tab selection', () => { - it('namespaces projections by device and discards revoked state', () => { - const store = new ClientSessionTabSelectionStore() - const initial = snapshot() - - store.project(initial, 'device-a') - store.project(initial, 'device-b') - const selectedA = store.activate(initial, 'device-a', 'browser-unified') - - expect(selectedA.activeTabId).toBe('browser-unified') - expect(selectedA.publicationEpoch).toBe('renderer:1:client-navigation') - expect(selectedA.snapshotVersion).toBe(2) - expect(store.project(initial, 'device-b').activeTabId).toBe('terminal-a::leaf-a') - const selectedAgain = store.activate(initial, 'device-a', 'terminal-a::leaf-b') - expect(selectedAgain.snapshotVersion).toBe(3) - expect(selectedAgain.activeTabId).toBe('terminal-a::leaf-b') - store.forgetClient('device-a') - expect(store.project(initial, 'device-a').activeTabId).toBe('terminal-a::leaf-a') - }) - it('does not restore a closed tab from a stale pre-close snapshot', () => { const store = new ClientSessionTabSelectionStore() const full = snapshot() diff --git a/src/main/runtime/client/navigation/created-mobile-session-tab-caller-selection.test.ts b/src/main/runtime/client/navigation/created-mobile-session-tab-caller-selection.test.ts index 3b4b265375e..8a9544e2b4d 100644 --- a/src/main/runtime/client/navigation/created-mobile-session-tab-caller-selection.test.ts +++ b/src/main/runtime/client/navigation/created-mobile-session-tab-caller-selection.test.ts @@ -67,24 +67,3 @@ it('selects a launched terminal for the caller only, without spawning again', as ) expect(spawn).toHaveBeenCalledTimes(2) }) - -it('selects a launched chat by its session for the caller only', async () => { - const { runtime, caller } = pairedRuntime() - const first = await createPane(runtime) - await runtime.publishStructuredAgentSessionTab({ - workspaceId: WT, - sessionId: 'sess-1', - agent: 'claude', - activate: false - }) - - expect( - runtime.selectCreatedMobileSessionTabForClient(WT, { sessionId: 'sess-1' }, 'device-caller') - ).toBe(true) - - expect(caller.at(-1)?.activeTabId).toBe('agent-session:sess-1') - expect((await runtime.listMobileSessionTabs(`id:${WT}`)).activeTabId).toBe(first.id) - expect((await runtime.listMobileSessionTabs(`id:${WT}`, 'device-bystander')).activeTabId).toBe( - first.id - ) -}) diff --git a/src/main/runtime/client/navigation/desktop-chat-creation-history.test.ts b/src/main/runtime/client/navigation/desktop-chat-creation-history.test.ts index 381ee755a61..3d29bae69b0 100644 --- a/src/main/runtime/client/navigation/desktop-chat-creation-history.test.ts +++ b/src/main/runtime/client/navigation/desktop-chat-creation-history.test.ts @@ -1,20 +1,8 @@ -import { mkdtempSync, mkdirSync, rmSync, writeFileSync, readFileSync } from 'node:fs' -import { writeFile, rename } from 'node:fs/promises' +import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from 'node:fs' import { tmpdir } from 'node:os' import { join } from 'node:path' import { afterEach, expect, it, vi } from 'vitest' import { DesktopChatCreationHistory } from './desktop-chat-creation-history' -import type * as NodeFs from 'node:fs' -import type * as NodeFsPromises from 'node:fs/promises' - -vi.mock('node:fs', async (original) => { - const fs = await original() - return { ...fs, readFileSync: vi.fn(fs.readFileSync) } -}) -vi.mock('node:fs/promises', async (original) => { - const fs = await original() - return { ...fs, writeFile: vi.fn(fs.writeFile), rename: vi.fn(fs.rename) } -}) const roots: string[] = [] afterEach(() => { @@ -58,65 +46,3 @@ it('treats unreadable history as unknown and never vetoes a create when its writ await history.recordCreate('ssh', 'agentSession.create', { envelope: { sessionId: 'chat' } }) expect(new DesktopChatCreationHistory(path).sessionIds()).toEqual(['chat']) }) - -it.each([1_000, 10_000])( - 'bounds drained creation work with %i retained memberships', - async (size) => { - const path = root() - const ids = Array.from({ length: size - 1 }, (_, i) => `saved-${i}`) - writeFileSync( - join(path, 'desktop-chat-creation-history.json'), - JSON.stringify({ ssh: ids, alias: ['saved-0'] }) - ) - vi.mocked(readFileSync).mockClear() - vi.mocked(writeFile).mockClear() - vi.mocked(rename).mockClear() - const history = new DesktopChatCreationHistory(path) - let serializedMemberships = 0 - const stringify = JSON.stringify - const serialization = vi - .spyOn(JSON, 'stringify') - .mockImplementation((value, replacer, space) => { - if ( - value && - typeof value === 'object' && - 'ssh' in value && - Array.isArray(value.ssh) && - Array.isArray(value.alias) - ) { - serializedMemberships += value.ssh.length + value.alias.length - } - return stringify(value, replacer, space) - }) - await history.recordCreate('ssh', 'agentSession.create', { envelope: { sessionId: 'saved-0' } }) - expect(writeFile).not.toHaveBeenCalled() - expect(rename).not.toHaveBeenCalled() - for (let i = 0; i < 10; i++) { - await history.recordCreate('ssh', 'agentSession.create', { - envelope: { sessionId: `new-${i}` } - }) - } - serialization.mockRestore() - expect(readFileSync).toHaveBeenCalledTimes(1) - expect(writeFile).toHaveBeenCalledTimes(10) - expect(rename).toHaveBeenCalledTimes(10) - // Distinct creates still serialize whole snapshots; unchanged bytes skip only disk replacement. - expect(serializedMemberships).toBeGreaterThan(0) - expect(serializedMemberships).toBeLessThanOrEqual(11 * size + 55) - let visits = 0 - const iterate = Set.prototype[Symbol.iterator] - const union = vi.spyOn(Set.prototype, Symbol.iterator).mockImplementation(function* ( - this: Set - ) { - for (const id of iterate.call(this)) { - visits++ - yield id - } - return undefined - }) - const all = history.sessionIds() - union.mockRestore() - expect(visits).toBeLessThanOrEqual(2 * (size + 10)) - expect(all).toEqual([...ids, ...Array.from({ length: 10 }, (_, i) => `new-${i}`)]) - } -) diff --git a/src/main/runtime/client/navigation/multi-client-navigation-isolation.integration.test.ts b/src/main/runtime/client/navigation/multi-client-navigation-isolation.integration.test.ts index bbfee36389c..f8852f70fd8 100644 --- a/src/main/runtime/client/navigation/multi-client-navigation-isolation.integration.test.ts +++ b/src/main/runtime/client/navigation/multi-client-navigation-isolation.integration.test.ts @@ -6,7 +6,6 @@ import { join } from 'node:path' import { afterEach, describe, expect, it, onTestFinished, vi } from 'vitest' import { parsePairingCode } from '../../../../shared/remote-runtime/pairing/pairing' import type { RuntimeMobileSessionTabsResult } from '../../../../shared/runtime-session-contracts' -import type { PersistedMobileClientTabSelections } from '../../../../shared/persisted-state-types' import { OrcaRuntimeService } from '../../orca-runtime' import { OrcaRuntimeRpcServer } from '../../rpc/server/runtime-rpc' import { @@ -578,43 +577,4 @@ describe('paired runtime navigation isolation', () => { ).toEqual(['follow', 'follow']) expect(harness.hostSelections.tabId).toBe('client-a-tab') }) - - it('restores a device tab selection after a runtime restart', async () => { - const persisted: { state: PersistedMobileClientTabSelections } = { state: {} } - const makeStoreWithSelections = () => ({ - ...makeStore(), - getMobileClientTabSelections: () => persisted.state, - setMobileClientTabSelections: (next: PersistedMobileClientTabSelections) => { - persisted.state = next - } - }) - - const first = new OrcaRuntimeService(makeStoreWithSelections() as never) - first.attachWindow(1) - first.markGraphReady(1) - seedSessionTabs(first) - await first.activateMobileSessionTab(`id:${SESSION_WORKTREE_ID}`, 'client-a-tab', undefined, { - notifyClients: false, - clientNavigationId: 'device-a', - navigation: 'caller' - }) - expect(persisted.state['device-a']?.[SESSION_WORKTREE_ID]?.activeTabId).toBe('client-a-tab') - - const restarted = new OrcaRuntimeService(makeStoreWithSelections() as never) - restarted.attachWindow(1) - restarted.markGraphReady(1) - seedSessionTabs(restarted) - const remembered = await restarted.listMobileSessionTabs( - `id:${SESSION_WORKTREE_ID}`, - 'device-a' - ) - expect(remembered.activeTabId).toBe('client-a-tab') - expect(remembered.tabs.find((tab) => tab.isActive)?.id).toBe('client-a-tab') - // Unknown devices inherit the host view, never another device's restored state. - const freshDevice = await restarted.listMobileSessionTabs( - `id:${SESSION_WORKTREE_ID}`, - 'device-b' - ) - expect(freshDevice.activeTabId).toBe('host-tab') - }) }) diff --git a/src/main/runtime/client/runtime-subscription-registry.test.ts b/src/main/runtime/client/runtime-subscription-registry.test.ts index 5fb0cdc6edb..2a2ea669fa0 100644 --- a/src/main/runtime/client/runtime-subscription-registry.test.ts +++ b/src/main/runtime/client/runtime-subscription-registry.test.ts @@ -32,21 +32,6 @@ describe('subscription registration versions', () => { } ) - it('still refuses a different connection with a current registration version', async () => { - const registry = new RuntimeSubscriptionRegistry() - const cleanup = vi.fn() - registry.register('terminal:owned', cleanup, 'conn-a') - expect( - registry.cleanupIfOwnedByConnection( - 'terminal:owned', - 'conn-b', - registry.getRegistrationVersion() - ) - ).toBe(false) - expect(cleanup).not.toHaveBeenCalled() - await registry.cleanupAndWait('terminal:owned') - }) - it('keeps registration ownership even when a cleanup callback is reused', async () => { const registry = new RuntimeSubscriptionRegistry() const cleanup = vi.fn() diff --git a/src/main/runtime/construction/orca-runtime-structured-status-sink-wiring.test.ts b/src/main/runtime/construction/orca-runtime-structured-status-sink-wiring.test.ts deleted file mode 100644 index 84aba075862..00000000000 --- a/src/main/runtime/construction/orca-runtime-structured-status-sink-wiring.test.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { describe, expect, it, vi } from 'vitest' -import type { StructuredAgentSessionLogger } from '../../native-chat/agent-session-wire/host/structured-agent-session-logger' - -const installed = vi.hoisted(() => { - const state: { - deps: Record | null - logger: StructuredAgentSessionLogger | null - } = { deps: null, logger: null } - return state -}) - -vi.mock('electron', () => ({ - BrowserWindow: { fromId: vi.fn(() => null) }, - webContents: { fromId: vi.fn(() => null) }, - ipcMain: { on: vi.fn(), removeListener: vi.fn() }, - app: { getPath: vi.fn(() => '/tmp') } -})) - -vi.mock('../agent-session/structured-agent-session-runtime', () => ({ - ensureStructuredAgentSessionHost: vi.fn( - async (deps: Record & { logger: StructuredAgentSessionLogger }) => { - installed.deps = deps - installed.logger = deps.logger - } - ) -})) - -import { OrcaRuntimeService } from '../orca-runtime' - -describe('checked runtime construction', () => { - it('installs subscription callbacks on the runtime with their real registry receiver', () => { - const runtime = new OrcaRuntimeService() - const cleanup = vi.fn() - runtime.registerSubscriptionCleanup('construction-subscription', cleanup) - runtime.cleanupSubscription('construction-subscription') - expect(cleanup).toHaveBeenCalledOnce() - runtime.cleanupSubscription('construction-subscription') - expect(cleanup).toHaveBeenCalledOnce() - }) -}) diff --git a/src/main/runtime/construction/runtime-edge-command-controller.test.ts b/src/main/runtime/construction/runtime-edge-command-controller.test.ts deleted file mode 100644 index b75c03df789..00000000000 --- a/src/main/runtime/construction/runtime-edge-command-controller.test.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { afterEach, expect, it } from 'vitest' -import { getRequestedPageUrl } from '../../browser/cdp/input/browser-expected-page-url' -import type { RuntimeBrowserCommands } from '../browser/commands/browser-commands' -import { setRuntimeBrowserCommandsFactory } from '../browser/installation/runtime-browser-commands-factory' -import { RuntimeEdgeCommandController } from './runtime-edge-command-controller' - -afterEach(() => setRuntimeBrowserCommandsFactory(null)) - -it('lets a browser command read the URL its RPC params expect, for that call only', async () => { - // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the test calls one method, and the proxy answers every name the controller binds. - const commands = new Proxy({} as RuntimeBrowserCommands, { - get: () => async () => getRequestedPageUrl() - }) - setRuntimeBrowserCommandsFactory(() => commands) - // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: these hosts are only used by commands the test never calls. - const hosts = { - browserHost: {}, - jiraHost: {}, - screencast: {}, - emulatorHost: {} - } as unknown as ConstructorParameters[0] - const { surface } = new RuntimeEdgeCommandController(hosts) - - await expect( - surface.browserClick({ element: '@e1', expectUrl: 'https://example.com/a' }) - ).resolves.toBe('https://example.com/a') - await expect(surface.browserClick({ element: '@e1' })).resolves.toBeUndefined() -}) diff --git a/src/main/runtime/construction/runtime-workspace-launch-contracts.test.ts b/src/main/runtime/construction/runtime-workspace-launch-contracts.test.ts index d4107653269..d962affe76f 100644 --- a/src/main/runtime/construction/runtime-workspace-launch-contracts.test.ts +++ b/src/main/runtime/construction/runtime-workspace-launch-contracts.test.ts @@ -1,7 +1,5 @@ import { registerOrcaRuntimeEmulatorFolderWorkspaceTests } from '../emulator/orca-runtime-emulator-folder-workspace.spec' import { registerOrcaRuntimeAutomationsTests } from '../integrations/automations/orca-runtime-automations.spec' -import { registerHostAccountLaunchEnvTests } from '../agent-session/environment/host-account-launch-env.spec' registerOrcaRuntimeEmulatorFolderWorkspaceTests() registerOrcaRuntimeAutomationsTests() -registerHostAccountLaunchEnvTests() diff --git a/src/main/runtime/construction/worktree-rows-change-events.test.ts b/src/main/runtime/construction/worktree-rows-change-events.test.ts deleted file mode 100644 index 04c80c2d1a8..00000000000 --- a/src/main/runtime/construction/worktree-rows-change-events.test.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { afterEach, describe, expect, it, vi } from 'vitest' -import type { RuntimeClientEvent } from '../../../shared/runtime-client-events' -import { AgentHookServer } from '../../agent-hooks/server' -import { OrcaRuntimeService } from '../orca-runtime' - -describe('worktreeRowsChanged', () => { - afterEach(() => { - vi.useRealTimers() - }) - - it('tells clients when an agent row changes', () => { - vi.useFakeTimers() - const runtime = new OrcaRuntimeService() - const server = new AgentHookServer() - const detach = runtime.attachAgentStatusStore(server) - const events: RuntimeClientEvent[] = [] - runtime.onClientEvent((event) => events.push(event)) - - server.ingestTerminalStatus({ - paneKey: 'tab-1:22222222-2222-4222-8222-222222222222', - tabId: 'tab-1', - worktreeId: 'repo-1::/tmp/worktree-a', - payload: { state: 'working', prompt: 'task', agentType: 'claude' } - }) - vi.advanceTimersByTime(500) - - expect(events).toContainEqual({ type: 'worktreeRowsChanged' }) - detach() - }) -}) diff --git a/src/main/runtime/files/orca-runtime-files-chunk-reads.test.ts b/src/main/runtime/files/orca-runtime-files-chunk-reads.test.ts deleted file mode 100644 index 7d19dfc2bf2..00000000000 --- a/src/main/runtime/files/orca-runtime-files-chunk-reads.test.ts +++ /dev/null @@ -1,51 +0,0 @@ -import { RuntimeFileReads } from './file-reads' -import { describe, expect, it, vi } from 'vitest' -import { openMock } from './runtime-file-command-mock-registry' -import { - createRuntimeFileHost, - useRuntimeFileHostLifecycle -} from './runtime-file-host.test-fixture' -import { SshHostNotServedError } from '../../providers/ssh-host-not-served-error' - -vi.mock('fs', async () => (await import('./runtime-file-command-mock-registry')).fsModuleMock()) -vi.mock('fs/promises', async () => - (await import('./runtime-file-command-mock-registry')).fsPromisesModuleMock() -) -vi.mock( - './watch/file-watcher-host', - async () => (await import('./runtime-file-command-mock-registry')).fileWatcherHostMock -) -vi.mock('../../ipc/filesystem/access/filesystem-auth', async () => - (await import('./runtime-file-command-mock-registry')).filesystemAuthModuleMock() -) -vi.mock('../../git/runner', async () => - (await import('./runtime-file-command-mock-registry')).gitRunnerModuleMock() -) -vi.mock( - '../../ipc/local-worktree-runtime-options', - async () => (await import('./runtime-file-command-mock-registry')).localWorktreeRuntimeOptionsMock -) - -describe('RuntimeFileReads.readFileExplorerChunk', () => { - useRuntimeFileHostLifecycle() - - it('refuses an SSH worktree without opening a local file', async () => { - const { commands } = createFileReads({ - path: '/remote/repo', - resolveRuntimeFileTarget: vi.fn(async () => ({ - worktree: { id: 'wt-1', repoId: 'repo-1', path: '/remote/repo' }, - executionHostId: 'ssh:ssh-1' - })) - }) - - await expect(commands.readFileExplorerChunk('id:wt-1', 'big.bin', 0, 1024)).rejects.toThrow( - SshHostNotServedError - ) - expect(openMock).not.toHaveBeenCalled() - }) -}) - -function createFileReads(options?: Parameters[0]) { - const { host, store } = createRuntimeFileHost(options) - return { commands: new RuntimeFileReads(host), store } -} diff --git a/src/main/runtime/files/orca-runtime-files-mobile-explorer-reads.test.ts b/src/main/runtime/files/orca-runtime-files-mobile-explorer-reads.test.ts deleted file mode 100644 index 3e6a6e99797..00000000000 --- a/src/main/runtime/files/orca-runtime-files-mobile-explorer-reads.test.ts +++ /dev/null @@ -1,63 +0,0 @@ -import { RuntimeFileAccess } from './file-access' -import { describe, expect, it, vi } from 'vitest' - -import type * as FileSystem from 'node:fs/promises' -import { resolveAuthorizedPathMock, statMock } from './runtime-file-command-mock-registry' -import { - createRuntimeFileHost, - useRuntimeFileHostLifecycle -} from './runtime-file-host.test-fixture' - -vi.mock('fs', async () => (await import('./runtime-file-command-mock-registry')).fsModuleMock()) -vi.mock('fs/promises', async () => - (await import('./runtime-file-command-mock-registry')).fsPromisesModuleMock() -) -vi.mock( - './watch/file-watcher-host', - async () => (await import('./runtime-file-command-mock-registry')).fileWatcherHostMock -) -vi.mock('../../ipc/filesystem/access/filesystem-auth', async () => - (await import('./runtime-file-command-mock-registry')).filesystemAuthModuleMock() -) -vi.mock('../../git/runner', async () => - (await import('./runtime-file-command-mock-registry')).gitRunnerModuleMock() -) -vi.mock( - '../../ipc/local-worktree-runtime-options', - async () => (await import('./runtime-file-command-mock-registry')).localWorktreeRuntimeOptionsMock -) - -describe('RuntimeFileAccess', () => { - useRuntimeFileHostLifecycle() - - it('detects a real same-size rewrite even after restoring the modification time', async () => { - vi.useRealTimers() - const files = await vi.importActual('node:fs/promises') - const { join } = await import('node:path') - const { tmpdir } = await import('node:os') - const root = await files.mkdtemp(join(tmpdir(), 'orca-media-revision-')) - const filePath = join(root, 'video.mp4') - try { - await files.writeFile(filePath, 'abc') - await files.utimes(filePath, 1, 2) - const { commands } = createFileAccess() - resolveAuthorizedPathMock.mockResolvedValue(filePath) - statMock.mockImplementation((path: string) => files.stat(path)) - const before = await commands.statRuntimeFile('id:wt-1', 'video.mp4') - await new Promise((resolve) => setTimeout(resolve, 20)) - await files.writeFile(filePath, 'xyz') - await files.utimes(filePath, 1, 2) - const after = await commands.statRuntimeFile('id:wt-1', 'video.mp4') - expect(after.size).toBe(before.size) - expect(after.mtime).toBe(before.mtime) - expect(after.ctime).not.toBe(before.ctime) - } finally { - await files.rm(root, { recursive: true, force: true }) - } - }) -}) - -function createFileAccess(options?: Parameters[0]) { - const { host, store } = createRuntimeFileHost(options) - return { commands: new RuntimeFileAccess(host), store } -} diff --git a/src/main/runtime/files/orca-runtime-files-preview-budget.test.ts b/src/main/runtime/files/orca-runtime-files-preview-budget.test.ts index e3d17d87259..ad8c4f9e7da 100644 --- a/src/main/runtime/files/orca-runtime-files-preview-budget.test.ts +++ b/src/main/runtime/files/orca-runtime-files-preview-budget.test.ts @@ -10,7 +10,6 @@ import { } from './runtime-file-host.test-fixture' import { RUNTIME_PREVIEWABLE_BINARY_MAX_BYTES } from './file-preview-budget' import { REMOTE_RPC_MAX_CONTENT_BYTES } from '../../../shared/remote-rpc-content-budget' -import { SshHostNotServedError } from '../../providers/ssh-host-not-served-error' vi.mock('fs', async () => (await import('./runtime-file-command-mock-registry')).fsModuleMock()) vi.mock('fs/promises', async () => @@ -102,20 +101,6 @@ describe('RuntimeFileReads', () => { commands.readChatImagePreview('id:wt-1', join(dir, 'notes.txt')) ).rejects.toThrow('not_an_image') }) - - it('refuses chat images for an SSH workspace, whose paths name the remote machine', async () => { - const dir = await previewFixture() - const { commands } = createFileReads({ - resolveRuntimeFileTarget: vi.fn(async () => ({ - worktree: { id: 'wt-1', repoId: 'repo-1', path: '/remote/repo' }, - executionHostId: 'ssh:ssh-1' - })) - }) - - await expect(commands.readChatImagePreview('id:wt-1', join(dir, 'logo.png'))).rejects.toThrow( - SshHostNotServedError - ) - }) }) })