@tencildev/testing 0.1.2 → 0.3.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/README.md +1 -0
- package/dist/index.d.mts +118 -2
- package/dist/index.mjs +451 -2
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -11,6 +11,7 @@ npm install --save-dev @tencildev/testing
|
|
|
11
11
|
- **`FakeT3Server`**: a stand-in T3 Code server (auth endpoints and the RPC
|
|
12
12
|
WebSocket) that simulates streamed agent turns.
|
|
13
13
|
- **`FakeAgentSdk`**: a stand-in for `@anthropic-ai/claude-agent-sdk`.
|
|
14
|
+
- **`FakeRegistry`**: a stand-in npm registry that answers Tencil's `latest` version.
|
|
14
15
|
- **`waitFor`**: poll until a condition holds.
|
|
15
16
|
|
|
16
17
|
```ts
|
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { IncomingMessage } from "node:http";
|
|
2
|
+
import { CreateThreadInput, Provider, ProviderInfo, ProviderMessage, ProviderModels, ProviderProject, ProviderThreadSummary, SendMessageInput, ThreadSink, ThreadStatus } from "@tencildev/protocol";
|
|
2
3
|
//#region src/fake-agent-provider.d.ts
|
|
3
4
|
interface FakeAgentProviderOptions {
|
|
4
5
|
id?: string;
|
|
@@ -9,6 +10,8 @@ interface FakeAgentProviderOptions {
|
|
|
9
10
|
threads?: ProviderThreadSummary[];
|
|
10
11
|
/** Deep links into the provider; none by default. */
|
|
11
12
|
threadUrl?: (providerThreadId: string) => string | undefined;
|
|
13
|
+
/** Mark every thread it starts `archivable`, as a provider that starts one per pin. */
|
|
14
|
+
archivable?: boolean;
|
|
12
15
|
}
|
|
13
16
|
/**
|
|
14
17
|
* A `Provider` (the agent behind pins) that answers instantly and records
|
|
@@ -24,6 +27,8 @@ export declare class FakeAgentProvider implements Provider {
|
|
|
24
27
|
/** Every `sendMessage` input, in order. */
|
|
25
28
|
readonly sent: SendMessageInput[];
|
|
26
29
|
readonly interrupted: string[];
|
|
30
|
+
readonly archived: string[];
|
|
31
|
+
readonly reopened: string[];
|
|
27
32
|
/** The sink of each watched provider thread. */
|
|
28
33
|
readonly sinks: Map<string, ThreadSink>;
|
|
29
34
|
watchCount: number;
|
|
@@ -42,9 +47,12 @@ export declare class FakeAgentProvider implements Provider {
|
|
|
42
47
|
/** New provider threads are numbered per provider: `fake-1`, `fake-2`, … */
|
|
43
48
|
createThread(input: CreateThreadInput): Promise<{
|
|
44
49
|
providerThreadId: string;
|
|
50
|
+
archivable: boolean | undefined;
|
|
45
51
|
}>;
|
|
46
52
|
sendMessage(input: SendMessageInput): Promise<void>;
|
|
47
53
|
interrupt(providerThreadId: string): Promise<void>;
|
|
54
|
+
archive(providerThreadId: string): Promise<void>;
|
|
55
|
+
reopen(providerThreadId: string): Promise<void>;
|
|
48
56
|
watch(providerThreadId: string, sink: ThreadSink): () => void;
|
|
49
57
|
dispose(): Promise<void>;
|
|
50
58
|
}
|
|
@@ -116,6 +124,114 @@ export declare class FakeAgentSdk {
|
|
|
116
124
|
getSessionMessages: (id: string) => Promise<Record<string, unknown>[]>;
|
|
117
125
|
}
|
|
118
126
|
//#endregion
|
|
127
|
+
//#region src/fake-ensemble-server.d.ts
|
|
128
|
+
type Status = Exclude<ThreadStatus, 'draft' | 'starting'>;
|
|
129
|
+
type Message = Omit<ProviderMessage, 'createdAt'>;
|
|
130
|
+
interface Step {
|
|
131
|
+
id: string;
|
|
132
|
+
label: string;
|
|
133
|
+
detail?: string;
|
|
134
|
+
status: 'running' | 'done' | 'failed';
|
|
135
|
+
}
|
|
136
|
+
interface Chat {
|
|
137
|
+
id: string;
|
|
138
|
+
title: string;
|
|
139
|
+
status: Status;
|
|
140
|
+
messages: Message[];
|
|
141
|
+
steps: Step[];
|
|
142
|
+
}
|
|
143
|
+
interface FakeEnsembleProject {
|
|
144
|
+
id: string;
|
|
145
|
+
name: string;
|
|
146
|
+
source: {
|
|
147
|
+
kind: 'local';
|
|
148
|
+
path: string;
|
|
149
|
+
} | {
|
|
150
|
+
kind: 'git';
|
|
151
|
+
url: string;
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
interface FakeEnsembleThread {
|
|
155
|
+
id: string;
|
|
156
|
+
projectId: string;
|
|
157
|
+
title: string;
|
|
158
|
+
branch: string;
|
|
159
|
+
state: string;
|
|
160
|
+
status: Status;
|
|
161
|
+
agent: string;
|
|
162
|
+
machine: {
|
|
163
|
+
id: string;
|
|
164
|
+
name: string;
|
|
165
|
+
};
|
|
166
|
+
chats: Chat[];
|
|
167
|
+
}
|
|
168
|
+
interface FakeEnsembleEvent {
|
|
169
|
+
id: string;
|
|
170
|
+
type: string;
|
|
171
|
+
data: Record<string, unknown>;
|
|
172
|
+
}
|
|
173
|
+
interface FakeEnsembleOptions {
|
|
174
|
+
projects?: FakeEnsembleProject[];
|
|
175
|
+
autoAllow?: boolean;
|
|
176
|
+
turnDelayMs?: number;
|
|
177
|
+
reply?: (text: string) => string;
|
|
178
|
+
}
|
|
179
|
+
export declare class FakeEnsembleServer {
|
|
180
|
+
#private;
|
|
181
|
+
readonly options: FakeEnsembleOptions;
|
|
182
|
+
readonly projects: FakeEnsembleProject[];
|
|
183
|
+
readonly threads: Map<string, FakeEnsembleThread>;
|
|
184
|
+
readonly ports: Map<number, string>;
|
|
185
|
+
readonly events: FakeEnsembleEvent[];
|
|
186
|
+
readonly calls: Array<{
|
|
187
|
+
method: string;
|
|
188
|
+
path: string;
|
|
189
|
+
body: Record<string, unknown>;
|
|
190
|
+
headers: IncomingMessage['headers'];
|
|
191
|
+
query: string;
|
|
192
|
+
}>;
|
|
193
|
+
readonly tokens: Set<string>;
|
|
194
|
+
get port(): number;
|
|
195
|
+
get url(): string;
|
|
196
|
+
get feedCount(): number;
|
|
197
|
+
private constructor();
|
|
198
|
+
static start(options?: FakeEnsembleOptions): Promise<FakeEnsembleServer>;
|
|
199
|
+
addThread(input: Partial<FakeEnsembleThread> & {
|
|
200
|
+
projectId: string;
|
|
201
|
+
title: string;
|
|
202
|
+
}): FakeEnsembleThread;
|
|
203
|
+
allow(): void;
|
|
204
|
+
deny(status?: number): void;
|
|
205
|
+
revoke(): void;
|
|
206
|
+
dropFeeds(): void;
|
|
207
|
+
resetHistory(): void;
|
|
208
|
+
emit(type: string, data: Record<string, unknown>): FakeEnsembleEvent;
|
|
209
|
+
callsOf(method: string, path: string | RegExp): Record<string, unknown>[];
|
|
210
|
+
close(): Promise<void>;
|
|
211
|
+
}
|
|
212
|
+
//#endregion
|
|
213
|
+
//#region src/fake-registry.d.ts
|
|
214
|
+
interface FakeRegistryOptions {
|
|
215
|
+
/** What `GET /tencil/latest` answers; unset answers 404, like an unpublished package. */
|
|
216
|
+
latest?: string;
|
|
217
|
+
port?: number;
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* A stand-in for the npm registry that only knows Tencil's `latest` tag.
|
|
221
|
+
* Anything else, such as npm fetching the package to install it, gets a 404.
|
|
222
|
+
*/
|
|
223
|
+
export declare class FakeRegistry {
|
|
224
|
+
#private;
|
|
225
|
+
/** Change it to publish a new version. */
|
|
226
|
+
latest: string | undefined;
|
|
227
|
+
/** Paths asked for, oldest first. */
|
|
228
|
+
readonly requests: string[];
|
|
229
|
+
private constructor();
|
|
230
|
+
static start(options?: FakeRegistryOptions): Promise<FakeRegistry>;
|
|
231
|
+
get url(): string;
|
|
232
|
+
close(): Promise<void>;
|
|
233
|
+
}
|
|
234
|
+
//#endregion
|
|
119
235
|
//#region src/fake-t3-server.d.ts
|
|
120
236
|
/**
|
|
121
237
|
* A small stand-in for a T3 Code server: the HTTP auth endpoints, the shell
|
|
@@ -233,4 +349,4 @@ export declare function waitFor<T>(check: () => T | undefined | null | false | P
|
|
|
233
349
|
message?: string | undefined;
|
|
234
350
|
}): Promise<T>;
|
|
235
351
|
//#endregion
|
|
236
|
-
export type { FakeAgentProviderOptions, FakeMessage, FakeProject, FakeProvider, FakeQueryRecord, FakeSession, FakeT3Options, FakeThread };
|
|
352
|
+
export type { FakeAgentProviderOptions, FakeEnsembleEvent, FakeEnsembleOptions, FakeEnsembleProject, FakeEnsembleThread, FakeMessage, FakeProject, FakeProvider, FakeQueryRecord, FakeRegistryOptions, FakeSession, FakeT3Options, FakeThread };
|
package/dist/index.mjs
CHANGED
|
@@ -15,6 +15,8 @@ var FakeAgentProvider = class {
|
|
|
15
15
|
/** Every `sendMessage` input, in order. */
|
|
16
16
|
sent = [];
|
|
17
17
|
interrupted = [];
|
|
18
|
+
archived = [];
|
|
19
|
+
reopened = [];
|
|
18
20
|
/** The sink of each watched provider thread. */
|
|
19
21
|
sinks = /* @__PURE__ */ new Map();
|
|
20
22
|
watchCount = 0;
|
|
@@ -62,7 +64,10 @@ var FakeAgentProvider = class {
|
|
|
62
64
|
async createThread(input) {
|
|
63
65
|
if (this.failCreate) throw new Error("Agent offline");
|
|
64
66
|
this.created.push(input);
|
|
65
|
-
return {
|
|
67
|
+
return {
|
|
68
|
+
providerThreadId: `${this.id}-${this.created.length}`,
|
|
69
|
+
archivable: this.#options.archivable
|
|
70
|
+
};
|
|
66
71
|
}
|
|
67
72
|
async sendMessage(input) {
|
|
68
73
|
this.sent.push(input);
|
|
@@ -70,6 +75,12 @@ var FakeAgentProvider = class {
|
|
|
70
75
|
async interrupt(providerThreadId) {
|
|
71
76
|
this.interrupted.push(providerThreadId);
|
|
72
77
|
}
|
|
78
|
+
async archive(providerThreadId) {
|
|
79
|
+
this.archived.push(providerThreadId);
|
|
80
|
+
}
|
|
81
|
+
async reopen(providerThreadId) {
|
|
82
|
+
this.reopened.push(providerThreadId);
|
|
83
|
+
}
|
|
73
84
|
watch(providerThreadId, sink) {
|
|
74
85
|
this.watchCount++;
|
|
75
86
|
this.sinks.set(providerThreadId, sink);
|
|
@@ -283,6 +294,444 @@ function textOf(content) {
|
|
|
283
294
|
return content.filter((block) => block.type === "text").map((block) => block.text).join("\n\n");
|
|
284
295
|
}
|
|
285
296
|
//#endregion
|
|
297
|
+
//#region src/fake-ensemble-server.ts
|
|
298
|
+
var FakeEnsembleServer = class FakeEnsembleServer {
|
|
299
|
+
options;
|
|
300
|
+
projects;
|
|
301
|
+
threads = /* @__PURE__ */ new Map();
|
|
302
|
+
ports = /* @__PURE__ */ new Map();
|
|
303
|
+
events = [];
|
|
304
|
+
calls = [];
|
|
305
|
+
tokens = /* @__PURE__ */ new Set();
|
|
306
|
+
#http = createServer((req, res) => {
|
|
307
|
+
this.#route(req, res).catch(() => {
|
|
308
|
+
res.writeHead(500);
|
|
309
|
+
res.end();
|
|
310
|
+
});
|
|
311
|
+
});
|
|
312
|
+
#sockets = /* @__PURE__ */ new Set();
|
|
313
|
+
#feeds = /* @__PURE__ */ new Map();
|
|
314
|
+
#pending = /* @__PURE__ */ new Set();
|
|
315
|
+
#writes = /* @__PURE__ */ new Map();
|
|
316
|
+
#timers = /* @__PURE__ */ new Set();
|
|
317
|
+
#port = 0;
|
|
318
|
+
#sequence = 0;
|
|
319
|
+
#epoch = randomUUID();
|
|
320
|
+
get port() {
|
|
321
|
+
return this.#port;
|
|
322
|
+
}
|
|
323
|
+
get url() {
|
|
324
|
+
return `http://127.0.0.1:${this.#port}`;
|
|
325
|
+
}
|
|
326
|
+
get feedCount() {
|
|
327
|
+
return this.#feeds.size;
|
|
328
|
+
}
|
|
329
|
+
constructor(options) {
|
|
330
|
+
this.options = options;
|
|
331
|
+
this.projects = options.projects ?? [];
|
|
332
|
+
this.#http.on("connection", (socket) => {
|
|
333
|
+
this.#sockets.add(socket);
|
|
334
|
+
socket.on("close", () => this.#sockets.delete(socket));
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
static async start(options = {}) {
|
|
338
|
+
const server = new FakeEnsembleServer(options);
|
|
339
|
+
await new Promise((resolve) => server.#http.listen(0, "127.0.0.1", resolve));
|
|
340
|
+
const address = server.#http.address();
|
|
341
|
+
if (!address || typeof address === "string") throw new Error("No port");
|
|
342
|
+
server.#port = address.port;
|
|
343
|
+
return server;
|
|
344
|
+
}
|
|
345
|
+
addThread(input) {
|
|
346
|
+
const thread = {
|
|
347
|
+
id: randomUUID(),
|
|
348
|
+
branch: "work",
|
|
349
|
+
state: "idle",
|
|
350
|
+
status: "idle",
|
|
351
|
+
agent: "claude",
|
|
352
|
+
machine: {
|
|
353
|
+
id: "local",
|
|
354
|
+
name: "This Mac"
|
|
355
|
+
},
|
|
356
|
+
chats: [{
|
|
357
|
+
id: "main",
|
|
358
|
+
title: "Main",
|
|
359
|
+
status: "idle",
|
|
360
|
+
messages: [],
|
|
361
|
+
steps: []
|
|
362
|
+
}],
|
|
363
|
+
...input
|
|
364
|
+
};
|
|
365
|
+
this.threads.set(thread.id, thread);
|
|
366
|
+
return thread;
|
|
367
|
+
}
|
|
368
|
+
allow() {
|
|
369
|
+
for (const response of this.#pending) this.#allow(response);
|
|
370
|
+
this.#pending.clear();
|
|
371
|
+
}
|
|
372
|
+
deny(status = 403) {
|
|
373
|
+
for (const response of this.#pending) {
|
|
374
|
+
response.writeHead(status, { "content-type": "application/json" });
|
|
375
|
+
response.end(JSON.stringify({ error: "Connection not allowed" }));
|
|
376
|
+
}
|
|
377
|
+
this.#pending.clear();
|
|
378
|
+
}
|
|
379
|
+
revoke() {
|
|
380
|
+
this.tokens.clear();
|
|
381
|
+
this.dropFeeds();
|
|
382
|
+
}
|
|
383
|
+
dropFeeds() {
|
|
384
|
+
for (const response of this.#feeds.keys()) response.destroy();
|
|
385
|
+
}
|
|
386
|
+
resetHistory() {
|
|
387
|
+
this.events.length = 0;
|
|
388
|
+
this.#epoch = randomUUID();
|
|
389
|
+
}
|
|
390
|
+
emit(type, data) {
|
|
391
|
+
const thread = this.threads.get(String(data.threadId));
|
|
392
|
+
const chat = thread?.chats.find((c) => c.id === data.chatId);
|
|
393
|
+
if (chat) {
|
|
394
|
+
if (type === "message") {
|
|
395
|
+
const message = data.message;
|
|
396
|
+
const index = chat.messages.findIndex((m) => m.id === message.id);
|
|
397
|
+
if (index < 0) chat.messages.push(message);
|
|
398
|
+
else chat.messages[index] = message;
|
|
399
|
+
}
|
|
400
|
+
if (type === "steps") chat.steps = data.steps;
|
|
401
|
+
if (type === "status") chat.status = data.status;
|
|
402
|
+
}
|
|
403
|
+
if (type === "archived" && thread) thread.state = "archived";
|
|
404
|
+
const event = {
|
|
405
|
+
id: `${this.#epoch}:${++this.#sequence}`,
|
|
406
|
+
type,
|
|
407
|
+
data
|
|
408
|
+
};
|
|
409
|
+
this.events.push(event);
|
|
410
|
+
for (const [response, id] of this.#feeds) if (!id || id === data.threadId) this.#writeEvent(response, event);
|
|
411
|
+
return event;
|
|
412
|
+
}
|
|
413
|
+
callsOf(method, path) {
|
|
414
|
+
return this.calls.filter((c) => c.method === method && (typeof path === "string" ? c.path === path : path.test(c.path))).map((c) => c.body);
|
|
415
|
+
}
|
|
416
|
+
async close() {
|
|
417
|
+
for (const timer of this.#timers) clearTimeout(timer);
|
|
418
|
+
for (const socket of this.#sockets) socket.destroy();
|
|
419
|
+
await new Promise((resolve) => this.#http.close(() => resolve()));
|
|
420
|
+
}
|
|
421
|
+
#allow(response) {
|
|
422
|
+
const token = `ens_${randomUUID()}`;
|
|
423
|
+
this.tokens.add(token);
|
|
424
|
+
const value = {
|
|
425
|
+
token,
|
|
426
|
+
client: {
|
|
427
|
+
id: randomUUID(),
|
|
428
|
+
name: "Tencil",
|
|
429
|
+
access: "work"
|
|
430
|
+
}
|
|
431
|
+
};
|
|
432
|
+
response.writeHead(200, { "content-type": "application/json" });
|
|
433
|
+
response.end(JSON.stringify(value));
|
|
434
|
+
}
|
|
435
|
+
#writeEvent(response, event) {
|
|
436
|
+
response.write(`${event.id ? `id: ${event.id}\n` : ""}data: ${JSON.stringify({
|
|
437
|
+
type: event.type,
|
|
438
|
+
...event.data
|
|
439
|
+
})}\n\n`);
|
|
440
|
+
}
|
|
441
|
+
#run(threadId, chat, text) {
|
|
442
|
+
this.emit("message", {
|
|
443
|
+
threadId,
|
|
444
|
+
chatId: chat.id,
|
|
445
|
+
message: {
|
|
446
|
+
id: randomUUID(),
|
|
447
|
+
role: "user",
|
|
448
|
+
text,
|
|
449
|
+
streaming: false
|
|
450
|
+
}
|
|
451
|
+
});
|
|
452
|
+
this.emit("status", {
|
|
453
|
+
threadId,
|
|
454
|
+
chatId: chat.id,
|
|
455
|
+
status: "working"
|
|
456
|
+
});
|
|
457
|
+
const id = randomUUID();
|
|
458
|
+
const reply = this.options.reply?.(text) ?? `Done: ${text.split("\n")[0]}`;
|
|
459
|
+
this.emit("message", {
|
|
460
|
+
threadId,
|
|
461
|
+
chatId: chat.id,
|
|
462
|
+
message: {
|
|
463
|
+
id,
|
|
464
|
+
role: "assistant",
|
|
465
|
+
text: reply.slice(0, 3),
|
|
466
|
+
streaming: true
|
|
467
|
+
}
|
|
468
|
+
});
|
|
469
|
+
const timer = setTimeout(() => {
|
|
470
|
+
this.#timers.delete(timer);
|
|
471
|
+
if (chat.status !== "working") return;
|
|
472
|
+
this.emit("message", {
|
|
473
|
+
threadId,
|
|
474
|
+
chatId: chat.id,
|
|
475
|
+
message: {
|
|
476
|
+
id,
|
|
477
|
+
role: "assistant",
|
|
478
|
+
text: reply,
|
|
479
|
+
streaming: false
|
|
480
|
+
}
|
|
481
|
+
});
|
|
482
|
+
this.emit("status", {
|
|
483
|
+
threadId,
|
|
484
|
+
chatId: chat.id,
|
|
485
|
+
status: "idle"
|
|
486
|
+
});
|
|
487
|
+
}, this.options.turnDelayMs ?? 20);
|
|
488
|
+
this.#timers.add(timer);
|
|
489
|
+
}
|
|
490
|
+
async #route(request, response) {
|
|
491
|
+
const url = new URL(request.url ?? "/", this.url);
|
|
492
|
+
const path = url.pathname.replace(/^\/v1\/?/, "");
|
|
493
|
+
const method = request.method ?? "GET";
|
|
494
|
+
let raw = "";
|
|
495
|
+
for await (const chunk of request) raw += String(chunk);
|
|
496
|
+
const body = raw ? JSON.parse(raw) : {};
|
|
497
|
+
this.calls.push({
|
|
498
|
+
method,
|
|
499
|
+
path,
|
|
500
|
+
body,
|
|
501
|
+
headers: request.headers,
|
|
502
|
+
query: url.search
|
|
503
|
+
});
|
|
504
|
+
const key = request.headers["idempotency-key"];
|
|
505
|
+
const json = (status, value) => {
|
|
506
|
+
if (key && status < 400) this.#writes.set(key, {
|
|
507
|
+
status,
|
|
508
|
+
value
|
|
509
|
+
});
|
|
510
|
+
response.writeHead(status, { "content-type": "application/json" });
|
|
511
|
+
response.end(value === void 0 ? void 0 : JSON.stringify(value));
|
|
512
|
+
};
|
|
513
|
+
const local = [`127.0.0.1:${this.port}`, `localhost:${this.port}`];
|
|
514
|
+
if (request.headers.origin || !local.includes(request.headers.host ?? "")) return json(403, { error: "Forbidden" });
|
|
515
|
+
if (method === "GET" && !path) return json(200, {
|
|
516
|
+
name: "ensemble",
|
|
517
|
+
api: 1
|
|
518
|
+
});
|
|
519
|
+
if (method === "POST" && !key) return json(400, { error: "Missing Idempotency-Key" });
|
|
520
|
+
if (method === "POST" && path === "connect") return this.#connect(body, response, json);
|
|
521
|
+
const token = (request.headers.authorization ?? "").replace(/^Bearer /, "");
|
|
522
|
+
if (!this.tokens.has(token)) return json(401, { error: "Client removed" });
|
|
523
|
+
const repeated = key ? this.#writes.get(key) : void 0;
|
|
524
|
+
if (repeated) return json(repeated.status, repeated.value);
|
|
525
|
+
if (method === "GET" && path === "projects") return json(200, this.projects);
|
|
526
|
+
if (method === "GET" && path === "threads") return json(200, this.#openThreads(url));
|
|
527
|
+
if (method === "GET" && path === "feed") return this.#feed(request, url, response, json);
|
|
528
|
+
if (method === "POST" && path === "threads") return this.#createThread(body, json);
|
|
529
|
+
const lifecycle = path.match(/^threads\/([^/]+)\/(archive|reopen)$/);
|
|
530
|
+
if (method === "POST" && lifecycle) return this.#archive(lifecycle, json);
|
|
531
|
+
const match = path.match(/^threads\/([^/]+)(?:\/chats(?:\/([^/]+)(?:\/(messages|stop))?)?)?$/);
|
|
532
|
+
const thread = match?.[1] ? this.threads.get(decodeURIComponent(match[1])) : void 0;
|
|
533
|
+
if (!match || !thread) return json(404, { error: "Not found" });
|
|
534
|
+
if (method === "GET" && !path.includes("/chats")) return json(200, thread);
|
|
535
|
+
if (method === "POST" && path.endsWith("/chats")) return json(200, this.#createChat(thread, body));
|
|
536
|
+
const chat = thread.chats.find((c) => c.id === decodeURIComponent(match[2] ?? "main"));
|
|
537
|
+
if (!chat) return json(404, { error: "Not found" });
|
|
538
|
+
if (method === "GET") {
|
|
539
|
+
const { messages, steps, status } = chat;
|
|
540
|
+
return json(200, {
|
|
541
|
+
thread,
|
|
542
|
+
chat,
|
|
543
|
+
status,
|
|
544
|
+
messages,
|
|
545
|
+
steps
|
|
546
|
+
});
|
|
547
|
+
}
|
|
548
|
+
if (method === "POST" && match[3] === "messages") {
|
|
549
|
+
this.#run(thread.id, chat, String(body.text));
|
|
550
|
+
return json(204);
|
|
551
|
+
}
|
|
552
|
+
if (method === "POST" && match[3] === "stop") {
|
|
553
|
+
this.emit("status", {
|
|
554
|
+
threadId: thread.id,
|
|
555
|
+
chatId: chat.id,
|
|
556
|
+
status: "idle"
|
|
557
|
+
});
|
|
558
|
+
return json(204);
|
|
559
|
+
}
|
|
560
|
+
json(404, { error: "Not found" });
|
|
561
|
+
}
|
|
562
|
+
/** Hold the request open until the test calls `allow` or `deny`, unless `autoAllow`. */
|
|
563
|
+
#connect(body, response, json) {
|
|
564
|
+
if (body.name !== "Tencil" || body.access !== "work") json(422, { error: "Invalid client" });
|
|
565
|
+
else if (this.options.autoAllow !== false) this.#allow(response);
|
|
566
|
+
else {
|
|
567
|
+
this.#pending.add(response);
|
|
568
|
+
response.on("close", () => this.#pending.delete(response));
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
#openThreads(url) {
|
|
572
|
+
const port = url.searchParams.get("port");
|
|
573
|
+
const project = url.searchParams.get("project");
|
|
574
|
+
return [...this.threads.values()].filter((thread) => thread.state !== "archived" && (port === null || this.ports.get(Number(port)) === thread.id) && (project === null || project === thread.projectId));
|
|
575
|
+
}
|
|
576
|
+
#createThread(body, json) {
|
|
577
|
+
if (!this.projects.some((project) => project.id === body.projectId)) json(404, { error: "No project with that projectId: GET /v1/projects lists them." });
|
|
578
|
+
else if (!body.prompt) json(422, { error: "Send a prompt." });
|
|
579
|
+
else {
|
|
580
|
+
const title = String(body.title ?? "Thread");
|
|
581
|
+
const chat = {
|
|
582
|
+
id: "main",
|
|
583
|
+
title,
|
|
584
|
+
status: "idle",
|
|
585
|
+
messages: [],
|
|
586
|
+
steps: []
|
|
587
|
+
};
|
|
588
|
+
const thread = this.addThread({
|
|
589
|
+
projectId: String(body.projectId),
|
|
590
|
+
title,
|
|
591
|
+
branch: `ensemble/${randomUUID()}`,
|
|
592
|
+
chats: [chat]
|
|
593
|
+
});
|
|
594
|
+
this.emit("thread", {
|
|
595
|
+
threadId: thread.id,
|
|
596
|
+
thread
|
|
597
|
+
});
|
|
598
|
+
this.#run(thread.id, chat, String(body.prompt));
|
|
599
|
+
json(200, thread);
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
/** Archiving one archived already, or reopening an open one, changes nothing. */
|
|
603
|
+
#archive([, id = "", action], json) {
|
|
604
|
+
const thread = this.threads.get(decodeURIComponent(id));
|
|
605
|
+
if (!thread) json(404, { error: "Not found" });
|
|
606
|
+
else if (action === "archive") {
|
|
607
|
+
if (thread.state !== "archived") this.emit("archived", { threadId: thread.id });
|
|
608
|
+
json(204);
|
|
609
|
+
} else {
|
|
610
|
+
if (thread.state === "archived") {
|
|
611
|
+
thread.state = "idle";
|
|
612
|
+
this.emit("thread", {
|
|
613
|
+
threadId: thread.id,
|
|
614
|
+
thread
|
|
615
|
+
});
|
|
616
|
+
}
|
|
617
|
+
json(200, thread);
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
#createChat(thread, body) {
|
|
621
|
+
const chat = {
|
|
622
|
+
id: randomUUID(),
|
|
623
|
+
title: String(body.title ?? "Chat"),
|
|
624
|
+
status: "idle",
|
|
625
|
+
messages: [],
|
|
626
|
+
steps: []
|
|
627
|
+
};
|
|
628
|
+
thread.chats.push(chat);
|
|
629
|
+
this.emit("chat", {
|
|
630
|
+
threadId: thread.id,
|
|
631
|
+
chatId: chat.id,
|
|
632
|
+
...chat
|
|
633
|
+
});
|
|
634
|
+
if (body.prompt) this.#run(thread.id, chat, String(body.prompt));
|
|
635
|
+
return {
|
|
636
|
+
id: chat.id,
|
|
637
|
+
title: chat.title,
|
|
638
|
+
status: chat.status
|
|
639
|
+
};
|
|
640
|
+
}
|
|
641
|
+
/** Events after `Last-Event-ID` when it's known, else the whole state; then live. */
|
|
642
|
+
#feed(request, url, response, json) {
|
|
643
|
+
const threadId = url.searchParams.get("thread");
|
|
644
|
+
if (threadId && !this.threads.has(threadId)) {
|
|
645
|
+
json(404, { error: "Not found" });
|
|
646
|
+
return;
|
|
647
|
+
}
|
|
648
|
+
response.writeHead(200, { "content-type": "text/event-stream" });
|
|
649
|
+
this.#feeds.set(response, threadId);
|
|
650
|
+
const after = request.headers["last-event-id"] ?? url.searchParams.get("after");
|
|
651
|
+
const index = this.events.findIndex((event) => event.id === after);
|
|
652
|
+
if (index >= 0) {
|
|
653
|
+
for (const event of this.events.slice(index + 1)) if (!threadId || event.data.threadId === threadId) this.#writeEvent(response, event);
|
|
654
|
+
} else for (const thread of this.threads.values()) if (!threadId || thread.id === threadId) this.#writeState(response, thread);
|
|
655
|
+
this.#writeEvent(response, {
|
|
656
|
+
type: "ready",
|
|
657
|
+
data: {}
|
|
658
|
+
});
|
|
659
|
+
const ping = setInterval(() => response.write(": ping\n\n"), 15e3);
|
|
660
|
+
response.on("close", () => {
|
|
661
|
+
clearInterval(ping);
|
|
662
|
+
this.#feeds.delete(response);
|
|
663
|
+
});
|
|
664
|
+
}
|
|
665
|
+
#writeState(response, thread) {
|
|
666
|
+
if (thread.state === "archived") {
|
|
667
|
+
this.#writeEvent(response, {
|
|
668
|
+
type: "archived",
|
|
669
|
+
data: { threadId: thread.id }
|
|
670
|
+
});
|
|
671
|
+
return;
|
|
672
|
+
}
|
|
673
|
+
this.#writeEvent(response, {
|
|
674
|
+
type: "thread",
|
|
675
|
+
data: {
|
|
676
|
+
threadId: thread.id,
|
|
677
|
+
thread
|
|
678
|
+
}
|
|
679
|
+
});
|
|
680
|
+
for (const chat of thread.chats) this.#writeEvent(response, {
|
|
681
|
+
type: "chat",
|
|
682
|
+
data: {
|
|
683
|
+
threadId: thread.id,
|
|
684
|
+
chatId: chat.id,
|
|
685
|
+
...chat
|
|
686
|
+
}
|
|
687
|
+
});
|
|
688
|
+
}
|
|
689
|
+
};
|
|
690
|
+
//#endregion
|
|
691
|
+
//#region src/fake-registry.ts
|
|
692
|
+
/**
|
|
693
|
+
* A stand-in for the npm registry that only knows Tencil's `latest` tag.
|
|
694
|
+
* Anything else, such as npm fetching the package to install it, gets a 404.
|
|
695
|
+
*/
|
|
696
|
+
var FakeRegistry = class FakeRegistry {
|
|
697
|
+
/** Change it to publish a new version. */
|
|
698
|
+
latest;
|
|
699
|
+
/** Paths asked for, oldest first. */
|
|
700
|
+
requests = [];
|
|
701
|
+
#http;
|
|
702
|
+
#port = 0;
|
|
703
|
+
constructor(options) {
|
|
704
|
+
this.latest = options.latest;
|
|
705
|
+
this.#http = createServer((request, response) => {
|
|
706
|
+
const path = new URL(request.url ?? "/", "http://localhost").pathname;
|
|
707
|
+
this.requests.push(path);
|
|
708
|
+
if (path === "/tencil/latest" && this.latest) {
|
|
709
|
+
response.writeHead(200, { "content-type": "application/json" });
|
|
710
|
+
response.end(JSON.stringify({
|
|
711
|
+
name: "tencil",
|
|
712
|
+
version: this.latest
|
|
713
|
+
}));
|
|
714
|
+
return;
|
|
715
|
+
}
|
|
716
|
+
response.writeHead(404, { "content-type": "application/json" });
|
|
717
|
+
response.end(JSON.stringify({ error: "Not found" }));
|
|
718
|
+
});
|
|
719
|
+
}
|
|
720
|
+
static async start(options = {}) {
|
|
721
|
+
const registry = new FakeRegistry(options);
|
|
722
|
+
await new Promise((resolve) => registry.#http.listen(options.port ?? 0, "127.0.0.1", resolve));
|
|
723
|
+
const address = registry.#http.address();
|
|
724
|
+
registry.#port = typeof address === "object" && address ? address.port : 0;
|
|
725
|
+
return registry;
|
|
726
|
+
}
|
|
727
|
+
get url() {
|
|
728
|
+
return `http://127.0.0.1:${this.#port}`;
|
|
729
|
+
}
|
|
730
|
+
close() {
|
|
731
|
+
return new Promise((resolve) => this.#http.close(() => resolve()));
|
|
732
|
+
}
|
|
733
|
+
};
|
|
734
|
+
//#endregion
|
|
286
735
|
//#region src/fake-t3-server.ts
|
|
287
736
|
const CODEX_PROVIDER = {
|
|
288
737
|
instanceId: "codex",
|
|
@@ -843,4 +1292,4 @@ async function waitFor(check, { timeoutMs = 3e3, intervalMs = 10, message = "con
|
|
|
843
1292
|
throw new Error(`Timed out waiting for ${message}${last ? `: ${String(last)}` : ""}`);
|
|
844
1293
|
}
|
|
845
1294
|
//#endregion
|
|
846
|
-
export { CODEX_PROVIDER, FakeAgentProvider, FakeAgentSdk, FakeT3Server, waitFor };
|
|
1295
|
+
export { CODEX_PROVIDER, FakeAgentProvider, FakeAgentSdk, FakeEnsembleServer, FakeRegistry, FakeT3Server, waitFor };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tencildev/testing",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Test doubles for Tencil: a fake provider,
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Test doubles for Tencil: a fake provider, fake T3 Code and Ensemble 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.
|
|
36
|
+
"@tencildev/protocol": "^0.5.0"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
39
|
"@types/ws": "^8.18.2",
|