@tencildev/testing 0.3.1 → 0.4.1

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,118 @@ 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
+ /** How long every answer takes, like a busy machine. */
228
+ answerDelayMs: number;
229
+ /** For `CodexProviderOptions.spawn`. */
230
+ spawn: () => FakeProcess;
231
+ release(): void;
232
+ /** Wait at a gate until `release()` or an interrupt. */
233
+ gate(): Promise<void>;
234
+ requestsOf(method: string): FakeCodexParams[];
235
+ /** Add a thread as if Codex made it earlier, outside Tencil. */
236
+ addThread(input: Partial<FakeCodexThread> & {
237
+ cwd: string;
238
+ }): FakeCodexThread;
239
+ }
240
+ //#endregion
127
241
  //#region src/fake-ensemble-server.d.ts
128
242
  type Status = Exclude<ThreadStatus, 'draft' | 'starting'>;
129
243
  type Message = Omit<ProviderMessage, 'createdAt'>;
@@ -349,4 +463,4 @@ export declare function waitFor<T>(check: () => T | undefined | null | false | P
349
463
  message?: string | undefined;
350
464
  }): Promise<T>;
351
465
  //#endregion
352
- export type { FakeAgentProviderOptions, FakeEnsembleEvent, FakeEnsembleOptions, FakeEnsembleProject, FakeEnsembleThread, FakeMessage, FakeProject, FakeProvider, FakeQueryRecord, FakeRegistryOptions, FakeSession, FakeT3Options, FakeThread };
466
+ 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,450 @@ 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
+ /** How long every answer takes, like a busy machine. */
370
+ answerDelayMs = 0;
371
+ #gates = /* @__PURE__ */ new Set();
372
+ #processes = /* @__PURE__ */ new Set();
373
+ /** For `CodexProviderOptions.spawn`. */
374
+ spawn = () => {
375
+ this.processes += 1;
376
+ const process = new FakeProcess();
377
+ if (this.failToStart !== void 0) {
378
+ const message = this.failToStart;
379
+ this.failToStart = void 0;
380
+ process.stderr.write(`${message}\n`);
381
+ setImmediate(() => process.end());
382
+ return process;
383
+ }
384
+ this.alive += 1;
385
+ this.#processes.add(process);
386
+ new Connection(this, process);
387
+ process.once("close", () => {
388
+ this.alive -= 1;
389
+ this.#processes.delete(process);
390
+ });
391
+ return process;
392
+ };
393
+ release() {
394
+ for (const open of this.#gates) open();
395
+ this.#gates.clear();
396
+ }
397
+ /** Wait at a gate until `release()` or an interrupt. */
398
+ gate() {
399
+ return new Promise((resolve) => this.#gates.add(resolve));
400
+ }
401
+ requestsOf(method) {
402
+ return this.requests.filter((r) => r.method === method).map((r) => r.params);
403
+ }
404
+ /** Add a thread as if Codex made it earlier, outside Tencil. */
405
+ addThread(input) {
406
+ const thread = {
407
+ id: randomUUID(),
408
+ name: null,
409
+ preview: "",
410
+ model: null,
411
+ updatedAt: Math.floor(Date.now() / 1e3),
412
+ settings: {},
413
+ items: [],
414
+ ...input
415
+ };
416
+ this.threads.set(thread.id, thread);
417
+ return thread;
418
+ }
419
+ };
420
+ var RpcFailure = class extends Error {
421
+ code;
422
+ constructor(code, message) {
423
+ super(message);
424
+ this.code = code;
425
+ }
426
+ };
427
+ /** One process's view: the threads it has loaded and their turns in progress. */
428
+ var Connection = class {
429
+ #server;
430
+ #process;
431
+ #loaded = /* @__PURE__ */ new Set();
432
+ #turns = /* @__PURE__ */ new Map();
433
+ #answering = /* @__PURE__ */ new Map();
434
+ #nextRequest = 1e3;
435
+ constructor(server, process) {
436
+ this.#server = server;
437
+ this.#process = process;
438
+ createInterface({ input: process.stdin }).on("line", (line) => this.#receive(line));
439
+ process.stdin.once("end", () => process.end());
440
+ }
441
+ #receive(line) {
442
+ const message = JSON.parse(line);
443
+ if (!message.method) {
444
+ if (message.id !== void 0) {
445
+ this.#answering.get(message.id)?.(message.result ?? { error: message.error });
446
+ this.#answering.delete(message.id);
447
+ }
448
+ return;
449
+ }
450
+ if (message.id === void 0) return;
451
+ const { id, method, params = {} } = message;
452
+ this.#server.requests.push({
453
+ method,
454
+ params
455
+ });
456
+ const answer = () => {
457
+ try {
458
+ this.#send({
459
+ id,
460
+ result: this.#handle(method, params)
461
+ });
462
+ } catch (error) {
463
+ const code = error instanceof RpcFailure ? error.code : -32603;
464
+ this.#send({
465
+ id,
466
+ error: {
467
+ code,
468
+ message: error.message
469
+ }
470
+ });
471
+ }
472
+ };
473
+ if (this.#server.answerDelayMs > 0) setTimeout(answer, this.#server.answerDelayMs);
474
+ else answer();
475
+ }
476
+ #handle(method, params) {
477
+ const server = this.#server;
478
+ switch (method) {
479
+ case "initialize": return {
480
+ userAgent: "fake-codex",
481
+ codexHome: "/fake",
482
+ platformOs: "macos"
483
+ };
484
+ case "account/read": return {
485
+ account: server.account,
486
+ requiresOpenaiAuth: true
487
+ };
488
+ case "config/read": return {
489
+ config: { model: server.configModel },
490
+ origins: {}
491
+ };
492
+ case "model/list": return {
493
+ data: server.models,
494
+ nextCursor: null
495
+ };
496
+ case "thread/list": return {
497
+ 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),
498
+ nextCursor: null
499
+ };
500
+ case "thread/start": {
501
+ const thread = server.addThread({
502
+ cwd: params.cwd ?? "",
503
+ model: params.model ?? null,
504
+ settings: params
505
+ });
506
+ this.#loaded.add(thread.id);
507
+ return {
508
+ thread: summary(thread),
509
+ model: thread.model,
510
+ cwd: thread.cwd
511
+ };
512
+ }
513
+ case "thread/resume": {
514
+ const thread = server.threads.get(params.threadId ?? "");
515
+ if (!thread) throw new RpcFailure(-32600, `no rollout found for thread id ${params.threadId}`);
516
+ thread.settings = params;
517
+ this.#loaded.add(thread.id);
518
+ return {
519
+ thread: summary(thread),
520
+ model: thread.model,
521
+ cwd: thread.cwd
522
+ };
523
+ }
524
+ case "thread/name/set":
525
+ this.#loadedThread(params.threadId).name = params.name ?? null;
526
+ return {};
527
+ case "thread/items/list": {
528
+ const thread = server.threads.get(params.threadId ?? "");
529
+ if (!thread || thread.legacy) throw new RpcFailure(-32601, "thread/items/list is not supported yet");
530
+ return {
531
+ data: thread.items,
532
+ nextCursor: null
533
+ };
534
+ }
535
+ case "thread/read": {
536
+ const thread = server.threads.get(params.threadId ?? "");
537
+ if (!thread) throw new RpcFailure(-32600, `thread not loaded: ${params.threadId}`);
538
+ const turns = [...new Set(thread.items.map((entry) => entry.turnId))].map((id) => ({
539
+ id,
540
+ status: "completed",
541
+ error: null,
542
+ startedAt: Math.floor((thread.items[0]?.startedAtMs ?? 0) / 1e3),
543
+ items: thread.items.filter((e) => e.turnId === id).map((e) => e.item)
544
+ }));
545
+ return { thread: {
546
+ ...summary(thread),
547
+ turns: params.includeTurns ? turns : []
548
+ } };
549
+ }
550
+ case "turn/start": return this.#startTurn(params);
551
+ case "turn/interrupt": {
552
+ const turn = this.#turns.get(params.threadId ?? "");
553
+ if (!turn || turn.id !== params.turnId) throw new RpcFailure(-32600, `no such turn: ${params.turnId}`);
554
+ turn.interrupted = true;
555
+ server.release();
556
+ return {};
557
+ }
558
+ default: throw new RpcFailure(-32601, `unknown method ${method}`);
559
+ }
560
+ }
561
+ #loadedThread(threadId = "") {
562
+ const thread = this.#server.threads.get(threadId);
563
+ if (!thread || !this.#loaded.has(threadId)) throw new RpcFailure(-32600, `thread not found: ${threadId}`);
564
+ return thread;
565
+ }
566
+ #startTurn(params) {
567
+ const thread = this.#loadedThread(params.threadId);
568
+ const running = this.#turns.get(thread.id);
569
+ const text = (params.input ?? []).flatMap((part) => part.text ? [part.text] : []).join("\n\n");
570
+ if (running) {
571
+ this.#saveUser(thread, running.id, params);
572
+ running.steered.push(text);
573
+ return { turn: {
574
+ id: running.id,
575
+ status: "inProgress",
576
+ error: null
577
+ } };
578
+ }
579
+ const id = randomUUID();
580
+ const turn = {
581
+ id,
582
+ interrupted: false,
583
+ steered: []
584
+ };
585
+ this.#turns.set(thread.id, turn);
586
+ thread.preview ||= text;
587
+ thread.updatedAt = Math.floor(Date.now() / 1e3);
588
+ this.#run(thread, turn, text, params);
589
+ return { turn: {
590
+ id,
591
+ status: "inProgress",
592
+ error: null
593
+ } };
594
+ }
595
+ #saveUser(thread, turnId, params) {
596
+ thread.items.push({
597
+ turnId,
598
+ startedAtMs: Date.now(),
599
+ item: {
600
+ type: "userMessage",
601
+ id: randomUUID(),
602
+ clientId: params.clientUserMessageId ?? null,
603
+ content: params.input
604
+ }
605
+ });
606
+ }
607
+ async #run(thread, turn, text, params) {
608
+ const server = this.#server;
609
+ const threadId = thread.id;
610
+ const notify = (method, body) => this.#send({
611
+ method,
612
+ params: {
613
+ threadId,
614
+ turnId: turn.id,
615
+ ...body
616
+ }
617
+ });
618
+ const finish = (status, error = null) => {
619
+ this.#turns.delete(threadId);
620
+ notify("turn/completed", { turn: {
621
+ id: turn.id,
622
+ status,
623
+ error
624
+ } });
625
+ };
626
+ await tick();
627
+ notify("turn/started", { turn: {
628
+ id: turn.id,
629
+ status: "inProgress",
630
+ error: null
631
+ } });
632
+ this.#saveUser(thread, turn.id, params);
633
+ for (const scripted of server.items) {
634
+ const item = {
635
+ ...scripted,
636
+ id: scripted.id ?? randomUUID()
637
+ };
638
+ notify("item/started", {
639
+ item: {
640
+ ...item,
641
+ status: "inProgress"
642
+ },
643
+ startedAtMs: Date.now()
644
+ });
645
+ await tick();
646
+ thread.items.push({
647
+ turnId: turn.id,
648
+ startedAtMs: Date.now(),
649
+ item
650
+ });
651
+ notify("item/completed", {
652
+ item,
653
+ completedAtMs: Date.now()
654
+ });
655
+ }
656
+ for (const asked of server.requestApproval) server.answers.push(await this.#ask(asked.method, {
657
+ threadId,
658
+ turnId: turn.id,
659
+ ...asked.params
660
+ }));
661
+ if (server.failNextTurn) {
662
+ const error = server.failNextTurn;
663
+ server.failNextTurn = void 0;
664
+ notify("error", {
665
+ error,
666
+ willRetry: false
667
+ });
668
+ return finish("failed", error);
669
+ }
670
+ const replyId = `msg_${randomUUID()}`;
671
+ const reply = server.reply(text);
672
+ const half = Math.ceil(reply.length / 2);
673
+ notify("item/started", {
674
+ item: {
675
+ type: "agentMessage",
676
+ id: replyId,
677
+ text: ""
678
+ },
679
+ startedAtMs: Date.now()
680
+ });
681
+ notify("item/agentMessage/delta", {
682
+ itemId: replyId,
683
+ delta: reply.slice(0, half)
684
+ });
685
+ if (server.crashNextTurn) {
686
+ server.crashNextTurn = false;
687
+ await tick();
688
+ this.#process.stderr.write("codex crashed\n");
689
+ return this.#process.end();
690
+ }
691
+ if (server.holdTurns && !turn.interrupted) await server.gate();
692
+ if (turn.interrupted) return finish("interrupted");
693
+ const full = reply + turn.steered.map((steered) => ` And: ${steered}`).join("");
694
+ notify("item/agentMessage/delta", {
695
+ itemId: replyId,
696
+ delta: full.slice(half)
697
+ });
698
+ const item = {
699
+ type: "agentMessage",
700
+ id: replyId,
701
+ text: full
702
+ };
703
+ thread.items.push({
704
+ turnId: turn.id,
705
+ startedAtMs: Date.now(),
706
+ item
707
+ });
708
+ notify("item/completed", {
709
+ item,
710
+ completedAtMs: Date.now()
711
+ });
712
+ finish("completed");
713
+ }
714
+ /** A request from Codex to the provider. Resolves with its answer. */
715
+ #ask(method, params) {
716
+ const id = this.#nextRequest++;
717
+ return new Promise((resolve) => {
718
+ this.#answering.set(id, resolve);
719
+ this.#send({
720
+ id,
721
+ method,
722
+ params
723
+ });
724
+ });
725
+ }
726
+ #send(message) {
727
+ if (this.#process.stdout.writable) this.#process.stdout.write(`${JSON.stringify(message)}\n`);
728
+ }
729
+ };
730
+ function summary(thread) {
731
+ return {
732
+ id: thread.id,
733
+ name: thread.name,
734
+ preview: thread.preview,
735
+ cwd: thread.cwd,
736
+ updatedAt: thread.updatedAt,
737
+ gitInfo: thread.branch ? { branch: thread.branch } : null,
738
+ status: { type: "notLoaded" },
739
+ turns: []
740
+ };
741
+ }
742
+ const tick = () => new Promise((resolve) => setImmediate(resolve));
743
+ //#endregion
297
744
  //#region src/fake-ensemble-server.ts
298
745
  var FakeEnsembleServer = class FakeEnsembleServer {
299
746
  options;
@@ -1292,4 +1739,4 @@ async function waitFor(check, { timeoutMs = 3e3, intervalMs = 10, message = "con
1292
1739
  throw new Error(`Timed out waiting for ${message}${last ? `: ${String(last)}` : ""}`);
1293
1740
  }
1294
1741
  //#endregion
1295
- export { CODEX_PROVIDER, FakeAgentProvider, FakeAgentSdk, FakeEnsembleServer, FakeRegistry, FakeT3Server, waitFor };
1742
+ 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.1",
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",
@@ -33,7 +33,7 @@
33
33
  },
34
34
  "dependencies": {
35
35
  "ws": "^8.22.0",
36
- "@tencildev/protocol": "^0.6.0"
36
+ "@tencildev/protocol": "^0.7.0"
37
37
  },
38
38
  "devDependencies": {
39
39
  "@types/ws": "^8.18.2",