@tencildev/testing 0.3.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -1,3 +1,5 @@
1
+ import { EventEmitter } from "node:events";
2
+ import { PassThrough } from "node:stream";
1
3
  import { IncomingMessage } from "node:http";
2
4
  import { CreateThreadInput, Provider, ProviderInfo, ProviderMessage, ProviderModels, ProviderProject, ProviderThreadSummary, SendMessageInput, ThreadSink, ThreadStatus } from "@tencildev/protocol";
3
5
  //#region src/fake-agent-provider.d.ts
@@ -124,6 +126,116 @@ export declare class FakeAgentSdk {
124
126
  getSessionMessages: (id: string) => Promise<Record<string, unknown>[]>;
125
127
  }
126
128
  //#endregion
129
+ //#region src/fake-codex-app-server.d.ts
130
+ interface FakeCodexThread {
131
+ id: string;
132
+ cwd: string;
133
+ name: string | null;
134
+ preview: string;
135
+ model: string | null;
136
+ updatedAt: number;
137
+ branch?: string;
138
+ /** Saved before Codex paginated history: `thread/items/list` can't read it. */
139
+ legacy?: boolean;
140
+ /** What it was last started or resumed with. */
141
+ settings: Record<string, unknown>;
142
+ /** The saved conversation, the way `thread/items/list` returns it. */
143
+ items: Array<{
144
+ turnId: string;
145
+ item: Record<string, unknown>;
146
+ startedAtMs: number;
147
+ }>;
148
+ }
149
+ /** An item Codex reports during a turn: a command, a file change, a tool call… */
150
+ type FakeCodexItem = {
151
+ type: string;
152
+ id?: string;
153
+ } & Record<string, unknown>;
154
+ /** What the provider sends: the fields the fake and tests read, by name. */
155
+ interface FakeCodexParams {
156
+ threadId?: string;
157
+ turnId?: string;
158
+ name?: string;
159
+ cwd?: string;
160
+ model?: string;
161
+ clientUserMessageId?: string;
162
+ input?: Array<{
163
+ type: string;
164
+ text?: string;
165
+ url?: string;
166
+ }>;
167
+ [key: string]: unknown;
168
+ }
169
+ interface FakeCodexRequest {
170
+ method: string;
171
+ params: FakeCodexParams;
172
+ }
173
+ /** The process `spawn()` returns: what `child_process.spawn` gives the provider. */
174
+ declare class FakeProcess extends EventEmitter {
175
+ #private;
176
+ readonly stdin: PassThrough;
177
+ readonly stdout: PassThrough;
178
+ readonly stderr: PassThrough;
179
+ kill(): void;
180
+ end(): void;
181
+ }
182
+ /**
183
+ * Stands in for `codex app-server`: JSON-RPC over stdio, threads kept in memory
184
+ * the way Codex keeps them on disk, and a scripted, streamed reply to each
185
+ * turn. A thread is only loaded on the process that started or resumed it, as
186
+ * with the real one, so a new process has to resume before it can continue.
187
+ */
188
+ export declare class FakeCodexAppServer {
189
+ #private;
190
+ readonly threads: Map<string, FakeCodexThread>;
191
+ /** Everything the provider asked, oldest first. */
192
+ readonly requests: FakeCodexRequest[];
193
+ /** Answers to the requests made with `requestApproval`, oldest first. */
194
+ readonly answers: unknown[];
195
+ processes: number;
196
+ alive: number;
197
+ /** `null`: signed out. */
198
+ account: {
199
+ type: string;
200
+ } | null;
201
+ configModel: string | null;
202
+ models: {
203
+ model: string;
204
+ displayName: string;
205
+ hidden: boolean;
206
+ isDefault: boolean;
207
+ }[];
208
+ reply: (text: string) => string;
209
+ /** Items each turn reports before its reply. */
210
+ items: FakeCodexItem[];
211
+ /** Requests Codex makes during each turn, before its reply. */
212
+ requestApproval: Array<{
213
+ method: string;
214
+ params: Record<string, unknown>;
215
+ }>;
216
+ /** Pause every turn after its first chunk until `release()` (or an interrupt). */
217
+ holdTurns: boolean;
218
+ /** The next turn ends like this instead of with a reply. */
219
+ failNextTurn: {
220
+ message: string;
221
+ codexErrorInfo?: unknown;
222
+ } | undefined;
223
+ /** The next turn's process dies mid-turn. */
224
+ crashNextTurn: boolean;
225
+ /** What the next process prints before dying, instead of starting. */
226
+ failToStart: string | undefined;
227
+ /** For `CodexProviderOptions.spawn`. */
228
+ spawn: () => FakeProcess;
229
+ release(): void;
230
+ /** Wait at a gate until `release()` or an interrupt. */
231
+ gate(): Promise<void>;
232
+ requestsOf(method: string): FakeCodexParams[];
233
+ /** Add a thread as if Codex made it earlier, outside Tencil. */
234
+ addThread(input: Partial<FakeCodexThread> & {
235
+ cwd: string;
236
+ }): FakeCodexThread;
237
+ }
238
+ //#endregion
127
239
  //#region src/fake-ensemble-server.d.ts
128
240
  type Status = Exclude<ThreadStatus, 'draft' | 'starting'>;
129
241
  type Message = Omit<ProviderMessage, 'createdAt'>;
@@ -349,4 +461,4 @@ export declare function waitFor<T>(check: () => T | undefined | null | false | P
349
461
  message?: string | undefined;
350
462
  }): Promise<T>;
351
463
  //#endregion
352
- export type { FakeAgentProviderOptions, FakeEnsembleEvent, FakeEnsembleOptions, FakeEnsembleProject, FakeEnsembleThread, FakeMessage, FakeProject, FakeProvider, FakeQueryRecord, FakeRegistryOptions, FakeSession, FakeT3Options, FakeThread };
464
+ export type { FakeAgentProviderOptions, FakeCodexItem, FakeCodexParams, FakeCodexRequest, FakeCodexThread, FakeEnsembleEvent, FakeEnsembleOptions, FakeEnsembleProject, FakeEnsembleThread, FakeMessage, FakeProject, FakeProvider, FakeQueryRecord, FakeRegistryOptions, FakeSession, FakeT3Options, FakeThread };
package/dist/index.mjs CHANGED
@@ -1,4 +1,7 @@
1
1
  import { randomUUID } from "node:crypto";
2
+ import { EventEmitter } from "node:events";
3
+ import { createInterface } from "node:readline";
4
+ import { PassThrough } from "node:stream";
2
5
  import { createServer } from "node:http";
3
6
  import { WebSocketServer } from "ws";
4
7
  //#region src/fake-agent-provider.ts
@@ -294,6 +297,444 @@ function textOf(content) {
294
297
  return content.filter((block) => block.type === "text").map((block) => block.text).join("\n\n");
295
298
  }
296
299
  //#endregion
300
+ //#region src/fake-codex-app-server.ts
301
+ /** The process `spawn()` returns: what `child_process.spawn` gives the provider. */
302
+ var FakeProcess = class extends EventEmitter {
303
+ stdin = new PassThrough();
304
+ stdout = new PassThrough();
305
+ stderr = new PassThrough();
306
+ #ended = false;
307
+ kill() {
308
+ this.end();
309
+ }
310
+ end() {
311
+ if (this.#ended) return;
312
+ this.#ended = true;
313
+ this.stdout.end();
314
+ this.stderr.end();
315
+ this.emit("exit");
316
+ this.emit("close");
317
+ }
318
+ };
319
+ /**
320
+ * Stands in for `codex app-server`: JSON-RPC over stdio, threads kept in memory
321
+ * the way Codex keeps them on disk, and a scripted, streamed reply to each
322
+ * turn. A thread is only loaded on the process that started or resumed it, as
323
+ * with the real one, so a new process has to resume before it can continue.
324
+ */
325
+ var FakeCodexAppServer = class {
326
+ threads = /* @__PURE__ */ new Map();
327
+ /** Everything the provider asked, oldest first. */
328
+ requests = [];
329
+ /** Answers to the requests made with `requestApproval`, oldest first. */
330
+ answers = [];
331
+ processes = 0;
332
+ alive = 0;
333
+ /** `null`: signed out. */
334
+ account = { type: "chatgpt" };
335
+ configModel = null;
336
+ models = [
337
+ {
338
+ model: "gpt-test",
339
+ displayName: "GPT Test",
340
+ hidden: false,
341
+ isDefault: true
342
+ },
343
+ {
344
+ model: "gpt-test-mini",
345
+ displayName: "GPT Test Mini",
346
+ hidden: false,
347
+ isDefault: false
348
+ },
349
+ {
350
+ model: "gpt-test-hidden",
351
+ displayName: "Hidden",
352
+ hidden: true,
353
+ isDefault: false
354
+ }
355
+ ];
356
+ reply = (text) => `Done: ${text}`;
357
+ /** Items each turn reports before its reply. */
358
+ items = [];
359
+ /** Requests Codex makes during each turn, before its reply. */
360
+ requestApproval = [];
361
+ /** Pause every turn after its first chunk until `release()` (or an interrupt). */
362
+ holdTurns = false;
363
+ /** The next turn ends like this instead of with a reply. */
364
+ failNextTurn;
365
+ /** The next turn's process dies mid-turn. */
366
+ crashNextTurn = false;
367
+ /** What the next process prints before dying, instead of starting. */
368
+ failToStart;
369
+ #gates = /* @__PURE__ */ new Set();
370
+ #processes = /* @__PURE__ */ new Set();
371
+ /** For `CodexProviderOptions.spawn`. */
372
+ spawn = () => {
373
+ this.processes += 1;
374
+ const process = new FakeProcess();
375
+ if (this.failToStart !== void 0) {
376
+ const message = this.failToStart;
377
+ this.failToStart = void 0;
378
+ process.stderr.write(`${message}\n`);
379
+ setImmediate(() => process.end());
380
+ return process;
381
+ }
382
+ this.alive += 1;
383
+ this.#processes.add(process);
384
+ new Connection(this, process);
385
+ process.once("close", () => {
386
+ this.alive -= 1;
387
+ this.#processes.delete(process);
388
+ });
389
+ return process;
390
+ };
391
+ release() {
392
+ for (const open of this.#gates) open();
393
+ this.#gates.clear();
394
+ }
395
+ /** Wait at a gate until `release()` or an interrupt. */
396
+ gate() {
397
+ return new Promise((resolve) => this.#gates.add(resolve));
398
+ }
399
+ requestsOf(method) {
400
+ return this.requests.filter((r) => r.method === method).map((r) => r.params);
401
+ }
402
+ /** Add a thread as if Codex made it earlier, outside Tencil. */
403
+ addThread(input) {
404
+ const thread = {
405
+ id: randomUUID(),
406
+ name: null,
407
+ preview: "",
408
+ model: null,
409
+ updatedAt: Math.floor(Date.now() / 1e3),
410
+ settings: {},
411
+ items: [],
412
+ ...input
413
+ };
414
+ this.threads.set(thread.id, thread);
415
+ return thread;
416
+ }
417
+ };
418
+ var RpcFailure = class extends Error {
419
+ code;
420
+ constructor(code, message) {
421
+ super(message);
422
+ this.code = code;
423
+ }
424
+ };
425
+ /** One process's view: the threads it has loaded and their turns in progress. */
426
+ var Connection = class {
427
+ #server;
428
+ #process;
429
+ #loaded = /* @__PURE__ */ new Set();
430
+ #turns = /* @__PURE__ */ new Map();
431
+ #answering = /* @__PURE__ */ new Map();
432
+ #nextRequest = 1e3;
433
+ constructor(server, process) {
434
+ this.#server = server;
435
+ this.#process = process;
436
+ createInterface({ input: process.stdin }).on("line", (line) => this.#receive(line));
437
+ process.stdin.once("end", () => process.end());
438
+ }
439
+ #receive(line) {
440
+ const message = JSON.parse(line);
441
+ if (!message.method) {
442
+ if (message.id !== void 0) {
443
+ this.#answering.get(message.id)?.(message.result ?? { error: message.error });
444
+ this.#answering.delete(message.id);
445
+ }
446
+ return;
447
+ }
448
+ if (message.id === void 0) return;
449
+ const { id, method, params = {} } = message;
450
+ this.#server.requests.push({
451
+ method,
452
+ params
453
+ });
454
+ try {
455
+ this.#send({
456
+ id,
457
+ result: this.#handle(method, params)
458
+ });
459
+ } catch (error) {
460
+ const code = error instanceof RpcFailure ? error.code : -32603;
461
+ this.#send({
462
+ id,
463
+ error: {
464
+ code,
465
+ message: error.message
466
+ }
467
+ });
468
+ }
469
+ }
470
+ #handle(method, params) {
471
+ const server = this.#server;
472
+ switch (method) {
473
+ case "initialize": return {
474
+ userAgent: "fake-codex",
475
+ codexHome: "/fake",
476
+ platformOs: "macos"
477
+ };
478
+ case "account/read": return {
479
+ account: server.account,
480
+ requiresOpenaiAuth: true
481
+ };
482
+ case "config/read": return {
483
+ config: { model: server.configModel },
484
+ origins: {}
485
+ };
486
+ case "model/list": return {
487
+ data: server.models,
488
+ nextCursor: null
489
+ };
490
+ case "thread/list": return {
491
+ data: [...server.threads.values()].filter((thread) => !params.cwd || thread.cwd === params.cwd).filter((thread) => thread.items.length > 0 || thread.name !== null).sort((a, b) => b.updatedAt - a.updatedAt).map(summary),
492
+ nextCursor: null
493
+ };
494
+ case "thread/start": {
495
+ const thread = server.addThread({
496
+ cwd: params.cwd ?? "",
497
+ model: params.model ?? null,
498
+ settings: params
499
+ });
500
+ this.#loaded.add(thread.id);
501
+ return {
502
+ thread: summary(thread),
503
+ model: thread.model,
504
+ cwd: thread.cwd
505
+ };
506
+ }
507
+ case "thread/resume": {
508
+ const thread = server.threads.get(params.threadId ?? "");
509
+ if (!thread) throw new RpcFailure(-32600, `no rollout found for thread id ${params.threadId}`);
510
+ thread.settings = params;
511
+ this.#loaded.add(thread.id);
512
+ return {
513
+ thread: summary(thread),
514
+ model: thread.model,
515
+ cwd: thread.cwd
516
+ };
517
+ }
518
+ case "thread/name/set":
519
+ this.#loadedThread(params.threadId).name = params.name ?? null;
520
+ return {};
521
+ case "thread/items/list": {
522
+ const thread = server.threads.get(params.threadId ?? "");
523
+ if (!thread || thread.legacy) throw new RpcFailure(-32601, "thread/items/list is not supported yet");
524
+ return {
525
+ data: thread.items,
526
+ nextCursor: null
527
+ };
528
+ }
529
+ case "thread/read": {
530
+ const thread = server.threads.get(params.threadId ?? "");
531
+ if (!thread) throw new RpcFailure(-32600, `thread not loaded: ${params.threadId}`);
532
+ const turns = [...new Set(thread.items.map((entry) => entry.turnId))].map((id) => ({
533
+ id,
534
+ status: "completed",
535
+ error: null,
536
+ startedAt: Math.floor((thread.items[0]?.startedAtMs ?? 0) / 1e3),
537
+ items: thread.items.filter((e) => e.turnId === id).map((e) => e.item)
538
+ }));
539
+ return { thread: {
540
+ ...summary(thread),
541
+ turns: params.includeTurns ? turns : []
542
+ } };
543
+ }
544
+ case "turn/start": return this.#startTurn(params);
545
+ case "turn/interrupt": {
546
+ const turn = this.#turns.get(params.threadId ?? "");
547
+ if (!turn || turn.id !== params.turnId) throw new RpcFailure(-32600, `no such turn: ${params.turnId}`);
548
+ turn.interrupted = true;
549
+ server.release();
550
+ return {};
551
+ }
552
+ default: throw new RpcFailure(-32601, `unknown method ${method}`);
553
+ }
554
+ }
555
+ #loadedThread(threadId = "") {
556
+ const thread = this.#server.threads.get(threadId);
557
+ if (!thread || !this.#loaded.has(threadId)) throw new RpcFailure(-32600, `thread not found: ${threadId}`);
558
+ return thread;
559
+ }
560
+ #startTurn(params) {
561
+ const thread = this.#loadedThread(params.threadId);
562
+ const running = this.#turns.get(thread.id);
563
+ const text = (params.input ?? []).flatMap((part) => part.text ? [part.text] : []).join("\n\n");
564
+ if (running) {
565
+ this.#saveUser(thread, running.id, params);
566
+ running.steered.push(text);
567
+ return { turn: {
568
+ id: running.id,
569
+ status: "inProgress",
570
+ error: null
571
+ } };
572
+ }
573
+ const id = randomUUID();
574
+ const turn = {
575
+ id,
576
+ interrupted: false,
577
+ steered: []
578
+ };
579
+ this.#turns.set(thread.id, turn);
580
+ thread.preview ||= text;
581
+ thread.updatedAt = Math.floor(Date.now() / 1e3);
582
+ this.#run(thread, turn, text, params);
583
+ return { turn: {
584
+ id,
585
+ status: "inProgress",
586
+ error: null
587
+ } };
588
+ }
589
+ #saveUser(thread, turnId, params) {
590
+ thread.items.push({
591
+ turnId,
592
+ startedAtMs: Date.now(),
593
+ item: {
594
+ type: "userMessage",
595
+ id: randomUUID(),
596
+ clientId: params.clientUserMessageId ?? null,
597
+ content: params.input
598
+ }
599
+ });
600
+ }
601
+ async #run(thread, turn, text, params) {
602
+ const server = this.#server;
603
+ const threadId = thread.id;
604
+ const notify = (method, body) => this.#send({
605
+ method,
606
+ params: {
607
+ threadId,
608
+ turnId: turn.id,
609
+ ...body
610
+ }
611
+ });
612
+ const finish = (status, error = null) => {
613
+ this.#turns.delete(threadId);
614
+ notify("turn/completed", { turn: {
615
+ id: turn.id,
616
+ status,
617
+ error
618
+ } });
619
+ };
620
+ await tick();
621
+ notify("turn/started", { turn: {
622
+ id: turn.id,
623
+ status: "inProgress",
624
+ error: null
625
+ } });
626
+ this.#saveUser(thread, turn.id, params);
627
+ for (const scripted of server.items) {
628
+ const item = {
629
+ ...scripted,
630
+ id: scripted.id ?? randomUUID()
631
+ };
632
+ notify("item/started", {
633
+ item: {
634
+ ...item,
635
+ status: "inProgress"
636
+ },
637
+ startedAtMs: Date.now()
638
+ });
639
+ await tick();
640
+ thread.items.push({
641
+ turnId: turn.id,
642
+ startedAtMs: Date.now(),
643
+ item
644
+ });
645
+ notify("item/completed", {
646
+ item,
647
+ completedAtMs: Date.now()
648
+ });
649
+ }
650
+ for (const asked of server.requestApproval) server.answers.push(await this.#ask(asked.method, {
651
+ threadId,
652
+ turnId: turn.id,
653
+ ...asked.params
654
+ }));
655
+ if (server.failNextTurn) {
656
+ const error = server.failNextTurn;
657
+ server.failNextTurn = void 0;
658
+ notify("error", {
659
+ error,
660
+ willRetry: false
661
+ });
662
+ return finish("failed", error);
663
+ }
664
+ const replyId = `msg_${randomUUID()}`;
665
+ const reply = server.reply(text);
666
+ const half = Math.ceil(reply.length / 2);
667
+ notify("item/started", {
668
+ item: {
669
+ type: "agentMessage",
670
+ id: replyId,
671
+ text: ""
672
+ },
673
+ startedAtMs: Date.now()
674
+ });
675
+ notify("item/agentMessage/delta", {
676
+ itemId: replyId,
677
+ delta: reply.slice(0, half)
678
+ });
679
+ if (server.crashNextTurn) {
680
+ server.crashNextTurn = false;
681
+ await tick();
682
+ this.#process.stderr.write("codex crashed\n");
683
+ return this.#process.end();
684
+ }
685
+ if (server.holdTurns && !turn.interrupted) await server.gate();
686
+ if (turn.interrupted) return finish("interrupted");
687
+ const full = reply + turn.steered.map((steered) => ` And: ${steered}`).join("");
688
+ notify("item/agentMessage/delta", {
689
+ itemId: replyId,
690
+ delta: full.slice(half)
691
+ });
692
+ const item = {
693
+ type: "agentMessage",
694
+ id: replyId,
695
+ text: full
696
+ };
697
+ thread.items.push({
698
+ turnId: turn.id,
699
+ startedAtMs: Date.now(),
700
+ item
701
+ });
702
+ notify("item/completed", {
703
+ item,
704
+ completedAtMs: Date.now()
705
+ });
706
+ finish("completed");
707
+ }
708
+ /** A request from Codex to the provider. Resolves with its answer. */
709
+ #ask(method, params) {
710
+ const id = this.#nextRequest++;
711
+ return new Promise((resolve) => {
712
+ this.#answering.set(id, resolve);
713
+ this.#send({
714
+ id,
715
+ method,
716
+ params
717
+ });
718
+ });
719
+ }
720
+ #send(message) {
721
+ if (this.#process.stdout.writable) this.#process.stdout.write(`${JSON.stringify(message)}\n`);
722
+ }
723
+ };
724
+ function summary(thread) {
725
+ return {
726
+ id: thread.id,
727
+ name: thread.name,
728
+ preview: thread.preview,
729
+ cwd: thread.cwd,
730
+ updatedAt: thread.updatedAt,
731
+ gitInfo: thread.branch ? { branch: thread.branch } : null,
732
+ status: { type: "notLoaded" },
733
+ turns: []
734
+ };
735
+ }
736
+ const tick = () => new Promise((resolve) => setImmediate(resolve));
737
+ //#endregion
297
738
  //#region src/fake-ensemble-server.ts
298
739
  var FakeEnsembleServer = class FakeEnsembleServer {
299
740
  options;
@@ -1292,4 +1733,4 @@ async function waitFor(check, { timeoutMs = 3e3, intervalMs = 10, message = "con
1292
1733
  throw new Error(`Timed out waiting for ${message}${last ? `: ${String(last)}` : ""}`);
1293
1734
  }
1294
1735
  //#endregion
1295
- export { CODEX_PROVIDER, FakeAgentProvider, FakeAgentSdk, FakeEnsembleServer, FakeRegistry, FakeT3Server, waitFor };
1736
+ export { CODEX_PROVIDER, FakeAgentProvider, FakeAgentSdk, FakeCodexAppServer, FakeEnsembleServer, FakeRegistry, FakeT3Server, waitFor };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tencildev/testing",
3
- "version": "0.3.1",
4
- "description": "Test doubles for Tencil: a fake provider, fake T3 Code and Ensemble servers and a fake Claude Agent SDK.",
3
+ "version": "0.4.0",
4
+ "description": "Test doubles for Tencil: a fake provider, fake T3 Code, Ensemble and Codex servers and a fake Claude Agent SDK.",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Pixel Perfect Agency",
7
7
  "homepage": "https://tencil.dev",