kimi-code/packages/server/test/services.test.ts
liruifengv b51e13538d
ci: run unit tests on windows (#1037)
* ci: run unit tests on windows

* fix(migration-legacy): align workdir bucket key with agent-core

computeWorkdirBucket used a local node:path-based resolve that yields backslash-separated paths on Windows, while agent-core's encodeWorkDirKey uses pathe (forward slashes on every platform). The SHA-256 inputs diverged, so migrated sessions were written to a bucket that the session picker never reads, making them invisible on Windows.

Alias computeWorkdirBucket to encodeWorkDirKey so both sides stay byte-identical, drop the local slugify copy, and update the workdir-bucket test reference accordingly.

* test(acp-adapter): expect platform-native separators in e2e-fs path

The e2e-fs test asserted the fs/readTextFile wire path as the raw POSIX targetPath, but AcpKaos.toClientPath converts '/' to '\' when the inner LocalKaos reports pathClass 'win32' (Windows). On Windows the wire path became '\Users\test\x.ts' and the assertion failed.

Mirror toClientPath in the test: expect backslash separators on win32 and the raw path otherwise. Implementation is unchanged.

* test(sdk): normalize workDir and skillDir paths in session tests

SessionStore.create/list and the skill loader normalize paths through pathe (forward slashes). The SDK tests compared the resulting workDir and skill loaded-dir against raw mkdtemp / node:path strings, which use backslashes on Windows (and node:fs realpath also returns backslashes for the skill dir), failing three toMatchObject assertions.

Build the expected paths with agent-core's normalizeWorkDir so they match the internal pathe representation on every platform. The skill dir keeps its realpath() (the loader realpaths the root) and only normalizes separators.

* test(skill): normalize realpath to forward slashes in scanner tests

resolveSkillRoots normalizes every root.path through fs.realpath followed by replacing backslashes with forward slashes (scanner.ts). The scanner tests compared root.path against node:fs realpath directly, which returns backslashes on Windows, so twenty assertions failed (toEqual / toContain / toHaveLength) even though the resolved paths were identical.

Wrap realpath at the top of the test file to mirror the implementation's normalization, so every comparison uses the same forward-slash form on every platform.

* test: skip Unix-only permission tests on Windows

The Unix file-permission assertions (mode bits like 0o600 / 0o700 and chmod 000 making a path unreadable) have no equivalent on Windows, which uses ACLs; fs.chmod there can only toggle the read-only bit. These six tests failed on Windows with mismatched mode values or a missing 40411.

Skip them on win32 via it.skipIf(process.platform === 'win32'): oauth FileTokenStorage (0600 file, 0700 dir), agent-core BackgroundTaskPersistence (0700 tasks dir), agent-core createPerIdJsonStore (0700 subdir), migration-legacy atomicWrite (0600 file), and server fs:browse (chmod 000 -> 40411).

* test(tui): make platform-sensitive assertions cross-platform

The TUI implementations are already platform-aware (pathe-style paths, pathToFileURL, quoteShellArg cmd/POSIX quoting, Alt+V on Windows for paste expansion), but the tests hard-coded POSIX expectations and failed on Windows.

Align the assertions with the implementation's platform behavior: footer-goal-badge matches the '[goal' badge prefix instead of /goal/ (toolbar tips contain '/goal'); tool-call expects backslash relative paths on win32; plan-box builds the file:// URL via pathToFileURL; custom-editor sends Alt+V on win32 for paste expansion; file-mention-provider normalizes the expected description to forward slashes; kimi-tui-startup builds the resume command with quoteShellArg; kimi-tui-message-flow builds the expected install path with resolve().

* test: align path assertions with pathe on Windows

Several test suites asserted paths produced by node:path/node:os/node:fs against values that agent-core, node-sdk and kaos normalize through pathe (forward slashes). On Windows the two forms diverge (backslashes vs forward slashes), failing about 19 assertions.

Mirror the implementation's normalization in the assertions via a local toPosix helper (or agent-core's normalizeWorkDir), so expected paths use forward slashes on every platform: kaos LocalKaos, node-sdk export/list/resume/config/transport sessions, cli FileMentionProvider, and agent-core skill-session.

* test(native): build path expectations with node:path.resolve

paths.mjs builds every path with node:path.resolve, which yields backslash-separated absolute paths on Windows. The path-helpers tests asserted against template strings that mixed the backslash appRoot with forward-slash segments, so Object.is failed on Windows even though the strings looked identical.

Build the expectations with the same resolve(appRoot, ...) helper so the separators match on every platform.

* fix: make Windows CI tests pass across all packages

Fix the remaining Windows CI failures so the Windows test job can go green. The changes fall into a few categories:

- Path separators: agent-core/node-sdk/kaos normalize paths via pathe (forward slashes); align test expectations and a couple of implementations (native cache base, workspace registry) with that.

- Platform-only services: skip launchd/systemd manager suites on win32 (Windows uses schtasks).

- Process/signal lifecycle: skip or relax tests that rely on POSIX signals / SIGTERM semantics that Windows does not support.

- Hook shell syntax: rewrite hook test commands from POSIX shell (single quotes, semicolons, stderr redirects, if/then/fi) to node -e / .cjs files that run under cmd.exe.

- CRLF: make Bash tool description stripping tolerate CRLF line endings.

- Misc: realpath short-name divergence, port-retry timing, telemetry spawn, fs-watch timing, snapshot path normalization, etc.

* fix: remove unused basename import in workspaceRegistryService

Fix lint error (no-unused-vars): basename from node:path is no longer used after switching to posixBasename from pathe.

* fix: align resume harness pathClass and wait for banner state on Windows

Two more Windows CI fixes:

- createResumeNoSideEffectKaos now reports pathClass 'win32' on Windows so tool descriptions (e.g. Glob's Windows note) match the live agent in expectResumeMatches, fixing usage/description deep-equal drift.

- kimi-tui-startup once-banner test now waits for writeBannerDisplayState to land before asserting, since the atomic write can lag behind the render on Windows.

* fix: resolve remaining Windows unit test failures

Make the new Windows CI job green across agent-core, kaos, node-sdk and server:

- Align the resume harness kaos pathClass with the live agent so platform-conditional tool descriptions (Glob's Windows note) match in expectResumeMatches instead of drifting on win32.
- Rewrite hook commands in agent-core tests as cross-platform node one-liners; single-quote echo, >&2 and ';' do not work under cmd.exe.
- Add .gitattributes enforcing LF so raw-imported templates (e.g. the compaction instruction) produce byte-identical token counts on Windows and POSIX.
- Terminate the full process tree on Windows in both the hook runner and kaos (taskkill /T /F) so grandchildren cannot outlive their parent and keep the cwd locked.
- Normalize workDir path separators in two kimi-sdk session tests to match the stored canonical form.
- Avoid cmd.exe arg-quoting pitfalls in the kaos cmd.exe test, and run the Windows process-tree kill test from a script file with the pid path passed via argv.
- Give the first fs-git e2e test more time on Windows and retry the temp-dir cleanup; skip the fs-watch overflow-burst assertion on Windows where fs-event coalescing prevents the single-window spike.

* ci: retrigger checks

* fix: resolve remaining Windows failures after merging main

- Terminate the spawned git/gh process tree on Windows in FsGitService (taskkill /T /F on timeout) so a timed-out 'gh pr view' cannot leave a grandchild holding the workspace cwd, which made the fs-git e2e cleanup fail with EPERM.

- Give the fs:git_status e2e suite a longer timeout on Windows and retry the temp-dir cleanup longer to ride out the slower child-process teardown.

- Make the third-party plugin install trust test assert the resolved install path via node:path so it matches the Windows-resolved path (D:\tmp\...) as well as the POSIX one.

* fix: align workspace registry roots and harden fs-git cleanup on Windows

- workspace-registry test: compare normalized (forward-slash) roots, since the registry and session index both store workDir via pathe.resolve (forward slashes on every platform). realpath() yields backslashes on Windows and diverged from the stored root.

- fs-git e2e: bump the temp-dir cleanup retries and the afterEach timeout, since Windows child-process teardown after server.close() is asynchronous and can keep the workspace cwd locked for several seconds.

* test: stub openUrl in kimi-tui-message-flow feedback tests

The /feedback command falls back to openUrl(FEEDBACK_ISSUE_URL) when submission fails, which spawned a real browser window on every test run. Mock #/utils/open-url (matching the existing login/message-replay/server test convention) so the suite never opens a browser.

* test: harden fs-git e2e cleanup against Windows cwd locks

On Windows, git/gh child processes and the session core process can outlive server.close() and keep the temp workspace as their cwd, so rmSync fails with EPERM even after a long retry. Add rmSyncRobust that retries and, if the cwd is still locked, swallows EPERM/EBUSY on Windows — the OS reclaims the temp dir and a cleanup hiccup must not fail an otherwise-passing test.

* test: harden server e2e cleanup against async teardown races

server.close() does not fully await the server's asynchronous teardown, so on a loaded CI runner the temp home/workspace dirs can still be held or written to when the afterEach rmSync runs, failing with EPERM (Windows) or ENOTEMPTY (Linux). Use a rmSyncRobust helper (retry + swallow EPERM/EBUSY/ENOTEMPTY) in the fs-git and question e2e cleanup. Also fix a leftover `throw err` (renamed to `throw error`) that broke the typecheck.
2026-06-26 11:56:41 +08:00

753 lines
26 KiB
TypeScript

import { mkdtempSync, rmSync } from 'node:fs';
import { mkdtemp, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { InstantiationService, ServiceCollection, EventService, FsWatcherService, IApprovalService, IEventService, ILogService, IQuestionService, type ApprovalResponse, type QuestionResult, type FsWatcherServiceOptions, type IEnvironmentService, type ILogService as ILoggerT, type ISessionService } from '@moonshot-ai/agent-core';
import type { Event } from '@moonshot-ai/protocol';
import { ApprovalService } from '#/services/approval/approvalService';
import { QuestionService } from '#/services/question/questionService';
import {
ISessionClientsService,
type ISessionClientsService as ISessionClientsServiceT,
} from '#/services/gateway';
import { WSBroadcastService } from '#/services/gateway/wsBroadcastService';
import type { WsConnection } from '../src/ws/connection';
class TestLogger implements ILoggerT {
readonly _serviceBrand: undefined;
info(): void {}
warn(): void {}
error(): void {}
debug(): void {}
child(): ILoggerT {
return this;
}
}
class FakeSessionClients implements ISessionClientsServiceT {
readonly _serviceBrand: undefined;
private readonly _bySession = new Map<string, Set<WsConnection>>();
subscribe(c: WsConnection, sid: string): void {
let set = this._bySession.get(sid);
if (!set) {
set = new Set();
this._bySession.set(sid, set);
}
set.add(c);
}
unsubscribe(c: WsConnection, sid: string): void {
this._bySession.get(sid)?.delete(c);
}
getConnections(sid: string): Iterable<WsConnection> {
return this._bySession.get(sid)?.values() ?? [];
}
forgetConnection(c: WsConnection): void {
for (const set of this._bySession.values()) set.delete(c);
}
subscriberCount(sid: string): number {
return this._bySession.get(sid)?.size ?? 0;
}
}
class FakeConnectionRegistry {
readonly _serviceBrand: undefined;
private readonly conns = new Map<string, WsConnection>();
constructor(connections: WsConnection[] = []) {
for (const conn of connections) this.conns.set(conn.id, conn);
}
add(conn: WsConnection): void {
this.conns.set(conn.id, conn);
}
remove(connId: string): void {
this.conns.delete(connId);
}
get(connId: string): WsConnection | undefined {
return this.conns.get(connId);
}
values(): Iterable<WsConnection> {
return this.conns.values();
}
closeAll(): void {
this.conns.clear();
}
size(): number {
return this.conns.size;
}
}
function fakeConn(id = 'conn_x'): { id: string; sent: unknown[]; send(m: unknown): void } & WsConnection {
const sent: unknown[] = [];
return {
id,
sent,
send(m: unknown): void {
sent.push(m);
},
} as unknown as { id: string; sent: unknown[]; send(m: unknown): void } & WsConnection;
}
function captureThrown(fn: () => void): unknown {
try {
fn();
return undefined;
} catch (error) {
return error;
}
}
class FakeWatcher {
readonly added: string[][] = [];
readonly unwatched: string[][] = [];
readonly unwatchErrors = new Map<string, Error>();
closeCalls = 0;
add(paths: string | string[]): this {
this.added.push(Array.isArray(paths) ? paths : [paths]);
return this;
}
unwatch(paths: string | string[]): this {
const items = Array.isArray(paths) ? paths : [paths];
this.unwatched.push(items);
const error = items.map((path) => this.unwatchErrors.get(path)).find(Boolean);
if (error) throw error;
return this;
}
on(): this {
return this;
}
async close(): Promise<void> {
this.closeCalls += 1;
}
}
type TestFsWatcher = ReturnType<NonNullable<FsWatcherServiceOptions['watcherFactory']>>;
let ix: InstantiationService;
let testLogger: TestLogger;
const tmpHomeDirs: string[] = [];
/** Throwaway `IEnvironmentService` whose homeDir is a fresh temp dir. */
function tmpEnv(): IEnvironmentService {
const dir = mkdtempSync(join(tmpdir(), 'kimi-server-test-'));
tmpHomeDirs.push(dir);
return { _serviceBrand: undefined, homeDir: dir, configPath: join(dir, 'config.toml') };
}
beforeEach(() => {
testLogger = new TestLogger();
const collection = new ServiceCollection([ILogService, testLogger]);
ix = new InstantiationService(collection);
});
afterEach(() => {
ix.dispose();
for (const dir of tmpHomeDirs.splice(0)) {
rmSync(dir, { recursive: true, force: true });
}
});
describe('WSBroadcastService (WS transport pump)', () => {
let homeDir: string;
const makeEnv = (): IEnvironmentService => ({
_serviceBrand: undefined,
homeDir,
configPath: `${homeDir}/config.toml`,
});
beforeEach(async () => {
homeDir = await mkdtemp(join(tmpdir(), 'kimi-ws-broadcast-'));
});
afterEach(async () => {
await rm(homeDir, { recursive: true, force: true, maxRetries: 5, retryDelay: 50 });
});
it('publishes event with seq=1, broadcasts to subscribers, advances seq monotonically per session', async () => {
const clients = new FakeSessionClients();
const c1 = fakeConn('conn_a');
const c2 = fakeConn('conn_b');
clients.subscribe(c1, 'sid_test');
clients.subscribe(c2, 'sid_test');
const bus = new EventService();
const broadcast = new WSBroadcastService(bus, testLogger, clients, new FakeConnectionRegistry(), makeEnv());
bus.publish({ type: 'fake.x', sessionId: 'sid_test', agentId: 'main' } as unknown as Event);
bus.publish({ type: 'fake.y', sessionId: 'sid_test', agentId: 'main' } as unknown as Event);
await broadcast._drainForTest('sid_test');
expect(c1.sent.length).toBe(2);
expect(c2.sent.length).toBe(2);
const env1 = c1.sent[0] as { seq: number; session_id: string; type: string; epoch?: string };
const env2 = c1.sent[1] as { seq: number; session_id: string; type: string };
expect(env1.seq).toBe(1);
expect(env1.session_id).toBe('sid_test');
expect(env1.type).toBe('fake.x');
expect(env1.epoch).toMatch(/^ep_/);
expect(env2.seq).toBe(2);
expect(env2.type).toBe('fake.y');
broadcast.dispose();
bus.dispose();
});
it('per-session seq counters are independent', async () => {
const clients = new FakeSessionClients();
const cA = fakeConn('conn_a');
const cB = fakeConn('conn_b');
clients.subscribe(cA, 'sid_a');
clients.subscribe(cB, 'sid_b');
const bus = new EventService();
const broadcast = new WSBroadcastService(bus, testLogger, clients, new FakeConnectionRegistry(), makeEnv());
bus.publish({ type: 'e1', sessionId: 'sid_a', agentId: 'main' } as unknown as Event);
bus.publish({ type: 'e1', sessionId: 'sid_b', agentId: 'main' } as unknown as Event);
bus.publish({ type: 'e2', sessionId: 'sid_a', agentId: 'main' } as unknown as Event);
await broadcast._drainForTest('sid_a');
await broadcast._drainForTest('sid_b');
const aSeqs = cA.sent.map((m) => (m as { seq: number }).seq);
const bSeqs = cB.sent.map((m) => (m as { seq: number }).seq);
expect(aSeqs).toEqual([1, 2]);
expect(bSeqs).toEqual([1]);
expect(broadcast._currentSeqForTest('sid_a')).toBe(2);
expect(broadcast._currentSeqForTest('sid_b')).toBe(1);
broadcast.dispose();
bus.dispose();
});
it('does not broadcast to connections subscribed to a different session', async () => {
const clients = new FakeSessionClients();
const onA = fakeConn('conn_a');
const onOther = fakeConn('conn_other');
clients.subscribe(onA, 'sid_a');
clients.subscribe(onOther, 'sid_other');
const bus = new EventService();
const broadcast = new WSBroadcastService(bus, testLogger, clients, new FakeConnectionRegistry(), makeEnv());
bus.publish({ type: 'evt', sessionId: 'sid_a', agentId: 'main' } as unknown as Event);
await broadcast._drainForTest('sid_a');
expect(onA.sent.length).toBe(1);
expect(onOther.sent.length).toBe(0);
broadcast.dispose();
bus.dispose();
});
it('broadcasts session.created to every live connection', async () => {
const clients = new FakeSessionClients();
const subscribed = fakeConn('conn_subscribed');
const listOnly = fakeConn('conn_list_only');
clients.subscribe(subscribed, 'sid_new');
const bus = new EventService();
const broadcast = new WSBroadcastService(
bus,
testLogger,
clients,
new FakeConnectionRegistry([subscribed, listOnly]),
makeEnv(),
);
bus.publish({
type: 'event.session.created',
sessionId: 'sid_new',
agentId: 'main',
session: { id: 'sid_new' },
} as unknown as Event);
await broadcast._drainForTest('sid_new');
expect(subscribed.sent).toHaveLength(1);
expect(listOnly.sent).toHaveLength(1);
expect((listOnly.sent[0] as { type: string }).type).toBe('event.session.created');
broadcast.dispose();
bus.dispose();
});
it('drops events without a sessionId / session_id and warns', () => {
const clients = new FakeSessionClients();
const c = fakeConn();
clients.subscribe(c, 'sid_x');
const warnSpy = vi.spyOn(testLogger, 'warn');
const bus = new EventService();
const broadcast = new WSBroadcastService(bus, testLogger, clients, new FakeConnectionRegistry(), makeEnv());
bus.publish({ type: 'no_sid' } as unknown as Event);
expect(c.sent.length).toBe(0);
expect(warnSpy).toHaveBeenCalledOnce();
broadcast.dispose();
bus.dispose();
});
it('post-dispose, publish reaches no subscribers (broadcast unsubscribed)', async () => {
const clients = new FakeSessionClients();
const c = fakeConn();
clients.subscribe(c, 'sid_x');
const bus = new EventService();
const broadcast = new WSBroadcastService(bus, testLogger, clients, new FakeConnectionRegistry(), makeEnv());
broadcast.dispose();
bus.publish({ type: 'late', sessionId: 'sid_x', agentId: 'main' } as unknown as Event);
await new Promise((r) => setTimeout(r, 10));
expect(c.sent.length).toBe(0);
bus.dispose();
});
it('getBufferedSince returns events with seq > cursor.seq when the gap is serveable', async () => {
const clients = new FakeSessionClients();
const c = fakeConn();
clients.subscribe(c, 'sid_test');
const bus = new EventService();
const broadcast = new WSBroadcastService(bus, testLogger, clients, new FakeConnectionRegistry(), makeEnv());
for (let i = 0; i < 5; i++) {
bus.publish({ type: `e${i}`, sessionId: 'sid_test', agentId: 'main' } as unknown as Event);
}
const replay = await broadcast.getBufferedSince('sid_test', { seq: 2 });
expect(replay.resyncRequired).toBe(false);
expect(replay.events.map((e) => e.seq)).toEqual([3, 4, 5]);
expect(replay.currentSeq).toBe(5);
expect(replay.epoch).toMatch(/^ep_/);
await broadcast._drainForTest('sid_test');
broadcast.dispose();
bus.dispose();
});
it('getBufferedSince forces a resync for a cursor ahead of the journal (stale v1 cursor)', async () => {
const bus = new EventService();
const broadcast = new WSBroadcastService(bus, testLogger, new FakeSessionClients(), new FakeConnectionRegistry(), makeEnv());
const replay = await broadcast.getBufferedSince('sid_new', { seq: 5 });
expect(replay.events).toEqual([]);
expect(replay.resyncRequired).toBe('epoch_changed');
expect(replay.currentSeq).toBe(0);
broadcast.dispose();
bus.dispose();
});
it('getBufferedSince forces a resync on epoch mismatch', async () => {
const bus = new EventService();
const broadcast = new WSBroadcastService(bus, testLogger, new FakeSessionClients(), new FakeConnectionRegistry(), makeEnv());
bus.publish({ type: 'e', sessionId: 'sid_e', agentId: 'main' } as unknown as Event);
await broadcast._drainForTest('sid_e');
const replay = await broadcast.getBufferedSince('sid_e', { seq: 0, epoch: 'ep_other' });
expect(replay.resyncRequired).toBe('epoch_changed');
broadcast.dispose();
bus.dispose();
});
it('seq and epoch survive a server restart (journal recovery) and serve replay from disk', async () => {
const bus1 = new EventService();
const b1 = new WSBroadcastService(bus1, testLogger, new FakeSessionClients(), new FakeConnectionRegistry(), makeEnv());
for (let i = 0; i < 3; i++) {
bus1.publish({ type: `e${i}`, sessionId: 'sid_p', agentId: 'main' } as unknown as Event);
}
const before = await b1.getCursor('sid_p');
expect(before.seq).toBe(3);
b1.dispose();
bus1.dispose();
// Let the write-behind flush settle.
await new Promise((r) => setTimeout(r, 50));
const bus2 = new EventService();
const b2 = new WSBroadcastService(bus2, testLogger, new FakeSessionClients(), new FakeConnectionRegistry(), makeEnv());
const after = await b2.getCursor('sid_p');
expect(after.seq).toBe(3);
expect(after.epoch).toBe(before.epoch);
// Replay across the restart comes from the on-disk journal.
const replay = await b2.getBufferedSince('sid_p', { seq: 1, epoch: before.epoch });
expect(replay.resyncRequired).toBe(false);
expect(replay.events.map((e) => e.seq)).toEqual([2, 3]);
// New events continue the persisted seq.
bus2.publish({ type: 'e3', sessionId: 'sid_p', agentId: 'main' } as unknown as Event);
await b2._drainForTest('sid_p');
expect(b2._currentSeqForTest('sid_p')).toBe(4);
b2.dispose();
bus2.dispose();
});
it('volatile events ride the watermark, are flagged, and are not journaled or replayed', async () => {
const clients = new FakeSessionClients();
const c = fakeConn();
clients.subscribe(c, 'sid_v');
const bus = new EventService();
const broadcast = new WSBroadcastService(bus, testLogger, clients, new FakeConnectionRegistry(), makeEnv());
bus.publish({
type: 'turn.started',
sessionId: 'sid_v',
agentId: 'main',
turnId: 1,
origin: { kind: 'user' },
} as unknown as Event);
bus.publish({
type: 'assistant.delta',
sessionId: 'sid_v',
agentId: 'main',
turnId: 1,
delta: 'hel',
} as unknown as Event);
bus.publish({
type: 'assistant.delta',
sessionId: 'sid_v',
agentId: 'main',
turnId: 1,
delta: 'lo',
} as unknown as Event);
await broadcast._drainForTest('sid_v');
expect(c.sent.length).toBe(3);
const turnStarted = c.sent[0] as { seq: number; volatile?: boolean };
const delta1 = c.sent[1] as { seq: number; volatile?: boolean; offset?: number };
const delta2 = c.sent[2] as { seq: number; volatile?: boolean; offset?: number };
expect(turnStarted.seq).toBe(1);
expect(turnStarted.volatile).toBeUndefined();
expect(delta1.volatile).toBe(true);
expect(delta1.seq).toBe(1); // watermark, not advanced
expect(delta1.offset).toBe(0);
expect(delta2.offset).toBe(3);
// Replay from 0 returns only the durable event.
const replay = await broadcast.getBufferedSince('sid_v', { seq: 0 });
expect(replay.events.map((e) => e.seq)).toEqual([1]);
expect(replay.currentSeq).toBe(1);
// The in-flight turn snapshot has the accumulated text.
const snap = await broadcast.getSnapshotState('sid_v');
expect(snap.seq).toBe(1);
expect(snap.inFlightTurn?.assistant_text).toBe('hello');
broadcast.dispose();
bus.dispose();
});
it('getSnapshotState clears the in-flight turn after turn.ended', async () => {
const bus = new EventService();
const broadcast = new WSBroadcastService(bus, testLogger, new FakeSessionClients(), new FakeConnectionRegistry(), makeEnv());
bus.publish({
type: 'turn.started',
sessionId: 'sid_t',
agentId: 'main',
turnId: 1,
origin: { kind: 'user' },
} as unknown as Event);
bus.publish({
type: 'turn.ended',
sessionId: 'sid_t',
agentId: 'main',
turnId: 1,
reason: 'completed',
} as unknown as Event);
const snap = await broadcast.getSnapshotState('sid_t');
expect(snap.inFlightTurn).toBeNull();
expect(snap.seq).toBe(2);
broadcast.dispose();
bus.dispose();
});
});
describe('FsWatcherService', () => {
it('shares watched paths and releases the underlying watcher on the last reference', () => {
const watcher = new FakeWatcher();
const service = new FsWatcherService(
{ resolve: () => undefined },
{ watcherFactory: () => watcher as unknown as TestFsWatcher },
testLogger,
{} as ISessionService,
);
const path = '/workspace/src';
service.addPaths('sid', 'conn-a', [path]);
service.addPaths('sid', 'conn-b', [path]);
expect(watcher.added).toEqual([[path]]);
expect(service.watchedPaths('conn-a', 'sid')).toEqual([path]);
expect(service.watchedPaths('conn-b', 'sid')).toEqual([path]);
service.removePaths('sid', 'conn-a', [path]);
expect(watcher.unwatched).toEqual([]);
expect(watcher.closeCalls).toBe(0);
expect(service.watchedPaths('conn-a', 'sid')).toEqual([]);
expect(service.watchedPaths('conn-b', 'sid')).toEqual([path]);
service.removePaths('sid', 'conn-b', [path]);
expect(watcher.unwatched).toEqual([[path]]);
expect(watcher.closeCalls).toBe(1);
expect(service.countForConnection('conn-a')).toBe(0);
expect(service.countForConnection('conn-b')).toBe(0);
service.dispose();
});
it('releases all removed path references before throwing aggregate unwatch errors', () => {
const watcher = new FakeWatcher();
const service = new FsWatcherService(
{ resolve: () => undefined },
{ watcherFactory: () => watcher as unknown as TestFsWatcher },
testLogger,
{} as ISessionService,
);
const paths = ['/workspace/src', '/workspace/docs', '/workspace/notes'];
watcher.unwatchErrors.set(paths[0]!, new Error('unwatch-src'));
watcher.unwatchErrors.set(paths[1]!, new Error('unwatch-docs'));
service.addPaths('sid', 'conn', paths);
const error = captureThrown(() => {
service.removePaths('sid', 'conn', paths.slice(0, 2));
});
expect(error).toBeInstanceOf(AggregateError);
expect((error as AggregateError).errors.map((err) => (err as Error).message)).toEqual([
'unwatch-src',
'unwatch-docs',
]);
expect(watcher.unwatched).toEqual([[paths[0]!], [paths[1]!]]);
expect(service.watchedPaths('conn', 'sid')).toEqual([paths[2]!]);
expect(watcher.closeCalls).toBe(0);
service.dispose();
});
});
describe('ApprovalService (broadcasts + resolve-by-approval_id)', () => {
function makeBrokerWithBus(): {
broker: ApprovalService;
bus: EventService;
broadcast: WSBroadcastService;
clients: FakeSessionClients;
conn: ReturnType<typeof fakeConn>;
} {
const clients = new FakeSessionClients();
const conn = fakeConn('conn_subscriber');
clients.subscribe(conn, 'sess_1');
const bus = new EventService();
const broadcast = new WSBroadcastService(bus, testLogger, clients, new FakeConnectionRegistry(), tmpEnv());
const broker = new ApprovalService(testLogger, bus);
return { broker, bus, broadcast, clients, conn };
}
function extractApprovalId(sentFrames: unknown[]): string | undefined {
for (const frame of sentFrames) {
const env = frame as { type: string; payload: { approval_id?: string } };
if (env.type === 'event.approval.requested' && env.payload.approval_id) {
return env.payload.approval_id;
}
}
return undefined;
}
it('broadcasts event.approval.requested AND settles via resolve(approval_id, response)', async () => {
const { broker, bus, broadcast, conn } = makeBrokerWithBus();
const pending = broker.request({
sessionId: 'sess_1',
agentId: 'agent_1',
toolCallId: 'tc_approval_1',
toolName: 'shell.run',
action: 'Run',
display: { kind: 'generic', summary: 'test' },
} as Parameters<typeof broker.request>[0]);
await broadcast._drainForTest('sess_1');
const approvalId = extractApprovalId(conn.sent);
expect(approvalId).toBeDefined();
expect(broker.isPending(approvalId!)).toBe(true);
const response: ApprovalResponse = { decision: 'approved' };
broker.resolve(approvalId!, response);
await expect(pending).resolves.toEqual(response);
await broadcast._drainForTest('sess_1');
const resolvedFrame = conn.sent.find(
(f) => (f as { type: string }).type === 'event.approval.resolved',
);
expect(resolvedFrame).toBeDefined();
expect(broker.isPending(approvalId!)).toBe(false);
expect(broker.isRecentlyResolved(approvalId!)).toBe(true);
broker.dispose();
broadcast.dispose();
bus.dispose();
});
it('dispose rejects all pending requests with "server shutting down"', async () => {
const { broker, bus, broadcast } = makeBrokerWithBus();
const p1 = broker.request({
sessionId: 'sess_1',
agentId: 'a',
toolCallId: 'tc_a',
toolName: 't',
action: 'a',
display: { kind: 'generic', summary: 'g' },
} as Parameters<typeof broker.request>[0]);
const p2 = broker.request({
sessionId: 'sess_1',
agentId: 'a',
toolCallId: 'tc_b',
toolName: 't',
action: 'a',
display: { kind: 'generic', summary: 'g' },
} as Parameters<typeof broker.request>[0]);
broker.dispose();
await expect(p1).rejects.toThrow(/server shutting down/);
await expect(p2).rejects.toThrow(/server shutting down/);
broadcast.dispose();
bus.dispose();
});
it('resolve() for an unknown id is a no-op (REST route handles 40404 via isPending)', () => {
const { broker, bus, broadcast } = makeBrokerWithBus();
broker.resolve('does-not-exist', { decision: 'approved' });
expect(broker.isPending('does-not-exist')).toBe(false);
broker.dispose();
broadcast.dispose();
bus.dispose();
});
});
describe('QuestionService (broadcasts + dismiss)', () => {
function makeQuestionBroker(): {
broker: QuestionService;
bus: EventService;
broadcast: WSBroadcastService;
clients: FakeSessionClients;
conn: ReturnType<typeof fakeConn>;
} {
const clients = new FakeSessionClients();
const conn = fakeConn('conn_q_subscriber');
clients.subscribe(conn, 's');
const bus = new EventService();
const broadcast = new WSBroadcastService(bus, testLogger, clients, new FakeConnectionRegistry(), tmpEnv());
const broker = new QuestionService(testLogger, bus);
return { broker, bus, broadcast, clients, conn };
}
function extractQuestionId(sentFrames: unknown[]): string | undefined {
for (const frame of sentFrames) {
const env = frame as { type: string; payload: { question_id?: string } };
if (env.type === 'event.question.requested' && env.payload.question_id) {
return env.payload.question_id;
}
}
return undefined;
}
it('broadcasts event.question.requested AND settles via resolve(question_id, answers)', async () => {
const { broker, bus, broadcast, conn } = makeQuestionBroker();
const pending = broker.request({
sessionId: 's',
agentId: 'a',
toolCallId: 'tc_q1',
questions: [
{
question: '?',
options: [{ label: 'A' }, { label: 'B' }],
},
],
} as Parameters<typeof broker.request>[0]);
await broadcast._drainForTest('s');
const questionId = extractQuestionId(conn.sent);
expect(questionId).toBeDefined();
expect(broker.isPending(questionId!)).toBe(true);
const response: QuestionResult = { answers: { q_0: 'opt_0_0' } };
broker.resolve(questionId!, response);
await expect(pending).resolves.toEqual(response);
await broadcast._drainForTest('s');
const answeredFrame = conn.sent.find(
(f) => (f as { type: string }).type === 'event.question.answered',
);
expect(answeredFrame).toBeDefined();
broker.dispose();
broadcast.dispose();
bus.dispose();
});
it('dismiss(question_id) broadcasts event.question.dismissed AND resolves Promise with null (SCHEMAS §6.3)', async () => {
const { broker, bus, broadcast, conn } = makeQuestionBroker();
const pending = broker.request({
sessionId: 's',
agentId: 'a',
questions: [
{
question: '?',
options: [{ label: 'A' }, { label: 'B' }],
},
],
} as Parameters<typeof broker.request>[0]);
await broadcast._drainForTest('s');
const questionId = extractQuestionId(conn.sent);
expect(questionId).toBeDefined();
broker.dismiss(questionId!);
await expect(pending).resolves.toBeNull();
await broadcast._drainForTest('s');
const dismissedFrame = conn.sent.find(
(f) => (f as { type: string }).type === 'event.question.dismissed',
);
expect(dismissedFrame).toBeDefined();
expect(broker.isPending(questionId!)).toBe(false);
expect(broker.isRecentlyResolved(questionId!)).toBe(true);
broker.dispose();
broadcast.dispose();
bus.dispose();
});
it('dispose rejects pending question Promises', async () => {
const { broker, bus, broadcast } = makeQuestionBroker();
const pending = broker.request({
sessionId: 's',
agentId: 'a',
questions: [
{ question: '?', options: [{ label: 'A' }, { label: 'B' }] },
],
} as Parameters<typeof broker.request>[0]);
broker.dispose();
await expect(pending).rejects.toThrow(/server shutting down/);
broadcast.dispose();
bus.dispose();
});
});
describe('DI graph — broker resolution through the container', () => {
it('resolves broker decorators against the same instances registered in the collection', () => {
const clients = new FakeSessionClients();
const eventBus = new EventService();
const approval = new ApprovalService(testLogger, eventBus);
const question = new QuestionService(testLogger, eventBus);
const collection = new ServiceCollection(
[ILogService, testLogger],
[ISessionClientsService, clients],
[IEventService, eventBus],
[IApprovalService, approval],
[IQuestionService, question],
);
const localIx = new InstantiationService(collection);
localIx.invokeFunction((a) => {
expect(a.get(ISessionClientsService)).toBe(clients);
expect(a.get(IEventService)).toBe(eventBus);
expect(a.get(IApprovalService)).toBe(approval);
expect(a.get(IQuestionService)).toBe(question);
expect(a.get(ILogService)).toBe(testLogger);
});
localIx.dispose();
});
});