|
|
|
@@ -18,6 +18,16 @@ function mutApp(thtRunner: any) {
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function deferred<T = void>() {
|
|
|
|
|
let resolve!: (value: T | PromiseLike<T>) => void;
|
|
|
|
|
let reject!: (reason?: unknown) => void;
|
|
|
|
|
const promise = new Promise<T>((onResolve, onReject) => {
|
|
|
|
|
resolve = onResolve;
|
|
|
|
|
reject = onReject;
|
|
|
|
|
});
|
|
|
|
|
return { promise, resolve, reject };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
test("POST /sessions usa i settings (workspace/provider/model/thinking) e crea+avvia", async () => {
|
|
|
|
|
const modelKey = path.join(os.tmpdir(), `thoth-model-key-${process.pid}`);
|
|
|
|
|
writeFileSync(modelKey, "test-model-key", { mode: 0o600 });
|
|
|
|
@@ -52,11 +62,17 @@ test("POST /sessions configura Pi con il thinking globale selezionato", async ()
|
|
|
|
|
let configured: any;
|
|
|
|
|
const bridge = { onClientEvent: () => {}, emitClientEvent: () => {} };
|
|
|
|
|
const runtime = { bridge } as any;
|
|
|
|
|
let current: any;
|
|
|
|
|
const mgr = {
|
|
|
|
|
createFor: () => runtime,
|
|
|
|
|
get: () => current,
|
|
|
|
|
createFor: () => { current = runtime; return runtime; },
|
|
|
|
|
configure: async (_rt: any, options: any) => { configured = options; },
|
|
|
|
|
start: () => {},
|
|
|
|
|
teardown: () => {},
|
|
|
|
|
teardownIfCurrent: (_id: string, expected: any) => {
|
|
|
|
|
if (current !== expected) return false;
|
|
|
|
|
current = undefined;
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
} as any;
|
|
|
|
|
const app = buildApp(loadConfig({ THT_HARNESS_DIR: "../harness" }), {
|
|
|
|
|
mgr,
|
|
|
|
@@ -78,12 +94,17 @@ test("POST /sessions/:id/resume configura Pi con il thinking persistito", async
|
|
|
|
|
let configured: any;
|
|
|
|
|
const bridge = { onClientEvent: () => {}, emitClientEvent: () => {} };
|
|
|
|
|
const runtime = { bridge } as any;
|
|
|
|
|
let current: any;
|
|
|
|
|
const mgr = {
|
|
|
|
|
get: () => undefined,
|
|
|
|
|
createFor: () => runtime,
|
|
|
|
|
get: () => current,
|
|
|
|
|
createFor: () => { current = runtime; return runtime; },
|
|
|
|
|
configure: async (_rt: any, options: any) => { configured = options; },
|
|
|
|
|
start: () => {},
|
|
|
|
|
teardown: () => {},
|
|
|
|
|
teardownIfCurrent: (_id: string, expected: any) => {
|
|
|
|
|
if (current !== expected) return false;
|
|
|
|
|
current = undefined;
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
} as any;
|
|
|
|
|
const app = buildApp(loadConfig({ THT_HARNESS_DIR: "../harness" }), {
|
|
|
|
|
mgr,
|
|
|
|
@@ -111,12 +132,17 @@ test("POST /sessions/:id/resume usa il thinking globale se manca nel manifest",
|
|
|
|
|
let configured: any;
|
|
|
|
|
const bridge = { onClientEvent: () => {}, emitClientEvent: () => {} };
|
|
|
|
|
const runtime = { bridge } as any;
|
|
|
|
|
let current: any;
|
|
|
|
|
const mgr = {
|
|
|
|
|
get: () => undefined,
|
|
|
|
|
createFor: () => runtime,
|
|
|
|
|
get: () => current,
|
|
|
|
|
createFor: () => { current = runtime; return runtime; },
|
|
|
|
|
configure: async (_rt: any, options: any) => { configured = options; },
|
|
|
|
|
start: () => {},
|
|
|
|
|
teardown: () => {},
|
|
|
|
|
teardownIfCurrent: (_id: string, expected: any) => {
|
|
|
|
|
if (current !== expected) return false;
|
|
|
|
|
current = undefined;
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
} as any;
|
|
|
|
|
const app = buildApp(loadConfig({ THT_HARNESS_DIR: "../harness" }), {
|
|
|
|
|
mgr,
|
|
|
|
@@ -162,11 +188,17 @@ test.each(["idle", "failed"])(
|
|
|
|
|
const order: string[] = [];
|
|
|
|
|
const oldRuntime = { bridge: { turnState: () => state } } as any;
|
|
|
|
|
const newRuntime = { bridge: { onClientEvent: () => {}, emitClientEvent: () => {} } } as any;
|
|
|
|
|
let current: any = oldRuntime;
|
|
|
|
|
const app = buildApp(loadConfig({ THT_HARNESS_DIR: "../harness" }), {
|
|
|
|
|
mgr: {
|
|
|
|
|
get: () => oldRuntime,
|
|
|
|
|
teardown: (id: string) => order.push(`teardown:${id}`),
|
|
|
|
|
createFor: () => { order.push("create"); return newRuntime; },
|
|
|
|
|
get: () => current,
|
|
|
|
|
teardownIfCurrent: (id: string, expected: any) => {
|
|
|
|
|
if (current !== expected) return false;
|
|
|
|
|
order.push(`teardown:${id}`);
|
|
|
|
|
current = undefined;
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
createFor: () => { order.push("create"); current = newRuntime; return newRuntime; },
|
|
|
|
|
configure: async () => {},
|
|
|
|
|
start: () => order.push("start"),
|
|
|
|
|
} as any,
|
|
|
|
@@ -195,12 +227,14 @@ test("POST resume without a runtime clears stale SSE state before cold start", a
|
|
|
|
|
const order: string[] = [];
|
|
|
|
|
let createOptions: any;
|
|
|
|
|
const newRuntime = { bridge: { onClientEvent: () => {}, emitClientEvent: () => {} } } as any;
|
|
|
|
|
let current: any;
|
|
|
|
|
const app = buildApp(loadConfig({ THT_HARNESS_DIR: "../harness" }), {
|
|
|
|
|
mgr: {
|
|
|
|
|
get: () => undefined,
|
|
|
|
|
get: () => current,
|
|
|
|
|
createFor: (_id: string, options: any) => {
|
|
|
|
|
createOptions = options;
|
|
|
|
|
order.push("create");
|
|
|
|
|
current = newRuntime;
|
|
|
|
|
return newRuntime;
|
|
|
|
|
},
|
|
|
|
|
configure: async () => {},
|
|
|
|
@@ -327,10 +361,15 @@ test("POST resume sanitizes runtime creation failure and preserves the old hub a
|
|
|
|
|
} as any;
|
|
|
|
|
hub.subscribe("s1", (_event: string, data: any) => delivered.push(data.text));
|
|
|
|
|
hub.publish("s1", "info", { text: "before" });
|
|
|
|
|
let current: any = { bridge: { turnState: () => "failed" } };
|
|
|
|
|
const app = buildApp(loadConfig({ THT_HARNESS_DIR: "../harness" }), {
|
|
|
|
|
mgr: {
|
|
|
|
|
get: () => ({ bridge: { turnState: () => "failed" } }),
|
|
|
|
|
teardown: () => {},
|
|
|
|
|
get: () => current,
|
|
|
|
|
teardownIfCurrent: (_id: string, expected: any) => {
|
|
|
|
|
if (current !== expected) return false;
|
|
|
|
|
current = undefined;
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
createFor: () => { createCalls += 1; throw new Error("CREATE_SENTINEL"); },
|
|
|
|
|
} as any,
|
|
|
|
|
hub,
|
|
|
|
@@ -424,6 +463,490 @@ test("concurrent cold Resume requests serialize and create one runtime", async (
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test.each([
|
|
|
|
|
["close", "resolve"],
|
|
|
|
|
["close", "reject"],
|
|
|
|
|
["delete", "resolve"],
|
|
|
|
|
["delete", "reject"],
|
|
|
|
|
] as const)(
|
|
|
|
|
"a bootstrap that %s left behind cannot %s against a replacement runtime",
|
|
|
|
|
async (lifecycle, outcome) => {
|
|
|
|
|
const oldConfigure = deferred<void>();
|
|
|
|
|
const published: any[] = [];
|
|
|
|
|
const starts: string[] = [];
|
|
|
|
|
let failed = 0;
|
|
|
|
|
let configureCalls = 0;
|
|
|
|
|
let current: any;
|
|
|
|
|
const runtime = (name: string) => {
|
|
|
|
|
let listener: ((event: any) => void) | undefined;
|
|
|
|
|
return {
|
|
|
|
|
name,
|
|
|
|
|
bridge: {
|
|
|
|
|
turnState: () => "running",
|
|
|
|
|
onClientEvent: (next: (event: any) => void) => { listener = next; },
|
|
|
|
|
emitClientEvent: (event: any) => listener?.(event),
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
const oldRuntime = runtime("old");
|
|
|
|
|
const replacement = runtime("replacement");
|
|
|
|
|
const runtimes = [oldRuntime, replacement];
|
|
|
|
|
const mgr = {
|
|
|
|
|
get: () => current,
|
|
|
|
|
createFor: () => {
|
|
|
|
|
current = runtimes.shift();
|
|
|
|
|
return current;
|
|
|
|
|
},
|
|
|
|
|
configure: async () => {
|
|
|
|
|
configureCalls += 1;
|
|
|
|
|
if (configureCalls === 1) await oldConfigure.promise;
|
|
|
|
|
},
|
|
|
|
|
start: (_id: string, expected: any) => {
|
|
|
|
|
if (current !== expected) throw new Error("stale start");
|
|
|
|
|
starts.push(expected.name);
|
|
|
|
|
},
|
|
|
|
|
teardown: () => { current = undefined; },
|
|
|
|
|
teardownIfCurrent: (_id: string, expected: any) => {
|
|
|
|
|
if (current !== expected) return false;
|
|
|
|
|
current = undefined;
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
const app = buildApp(loadConfig({ THT_HARNESS_DIR: "../harness" }), {
|
|
|
|
|
mgr: mgr as any,
|
|
|
|
|
hub: {
|
|
|
|
|
publish: (_id: string, event: string, data: object) => {
|
|
|
|
|
published.push({ event, data });
|
|
|
|
|
return published.length;
|
|
|
|
|
},
|
|
|
|
|
clear: () => {},
|
|
|
|
|
forget: () => {},
|
|
|
|
|
} as any,
|
|
|
|
|
thtRunner: {
|
|
|
|
|
sessionNew: async () => ({ id: "s1" }),
|
|
|
|
|
searchPack: async () => {},
|
|
|
|
|
sessionShow: async () => ({ status: "open", archived: false }),
|
|
|
|
|
reopenSession: async () => {},
|
|
|
|
|
closeSession: async () => {},
|
|
|
|
|
deleteSession: async () => {},
|
|
|
|
|
failSession: async () => { failed += 1; },
|
|
|
|
|
} as any,
|
|
|
|
|
readiness: { ensure: async () => ({ ok: true }) } as any,
|
|
|
|
|
getSettings: () => ({ workspace: "local" }) as any,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await app.inject({ method: "POST", url: "/sessions", payload: { question: "old" } });
|
|
|
|
|
if (lifecycle === "close") {
|
|
|
|
|
await app.inject({ method: "POST", url: "/sessions/s1/close" });
|
|
|
|
|
await app.inject({ method: "POST", url: "/sessions/s1/resume" });
|
|
|
|
|
} else {
|
|
|
|
|
await app.inject({ method: "DELETE", url: "/sessions/s1" });
|
|
|
|
|
await app.inject({ method: "POST", url: "/sessions", payload: { question: "replacement" } });
|
|
|
|
|
}
|
|
|
|
|
await new Promise((resolve) => setImmediate(resolve));
|
|
|
|
|
expect(current).toBe(replacement);
|
|
|
|
|
published.length = 0;
|
|
|
|
|
starts.length = 0;
|
|
|
|
|
failed = 0;
|
|
|
|
|
|
|
|
|
|
if (outcome === "resolve") oldConfigure.resolve();
|
|
|
|
|
else oldConfigure.reject(new Error("old bootstrap failed"));
|
|
|
|
|
await new Promise((resolve) => setImmediate(resolve));
|
|
|
|
|
|
|
|
|
|
expect(current).toBe(replacement);
|
|
|
|
|
expect(starts).toEqual([]);
|
|
|
|
|
expect(failed).toBe(0);
|
|
|
|
|
expect(published).toEqual([]);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
test.each(["resolve", "reject"] as const)(
|
|
|
|
|
"a deleted session stays forgotten when its stale bootstrap later %s",
|
|
|
|
|
async (outcome) => {
|
|
|
|
|
const oldConfigure = deferred<void>();
|
|
|
|
|
const published: any[] = [];
|
|
|
|
|
let failed = 0;
|
|
|
|
|
let current: any;
|
|
|
|
|
let forgotten = false;
|
|
|
|
|
let listener: ((event: any) => void) | undefined;
|
|
|
|
|
const runtime = {
|
|
|
|
|
bridge: {
|
|
|
|
|
onClientEvent: (next: (event: any) => void) => { listener = next; },
|
|
|
|
|
emitClientEvent: (event: any) => listener?.(event),
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
const app = buildApp(loadConfig({ THT_HARNESS_DIR: "../harness" }), {
|
|
|
|
|
mgr: {
|
|
|
|
|
get: () => current,
|
|
|
|
|
createFor: () => { current = runtime; return runtime; },
|
|
|
|
|
configure: async () => oldConfigure.promise,
|
|
|
|
|
start: () => { throw new Error("deleted runtime must not start"); },
|
|
|
|
|
teardown: () => { current = undefined; },
|
|
|
|
|
teardownIfCurrent: (_id: string, expected: any) => {
|
|
|
|
|
if (current !== expected) return false;
|
|
|
|
|
current = undefined;
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
} as any,
|
|
|
|
|
hub: {
|
|
|
|
|
publish: (_id: string, event: string, data: object) => {
|
|
|
|
|
published.push({ event, data });
|
|
|
|
|
return published.length;
|
|
|
|
|
},
|
|
|
|
|
forget: () => { forgotten = true; },
|
|
|
|
|
} as any,
|
|
|
|
|
thtRunner: {
|
|
|
|
|
sessionNew: async () => ({ id: "s1" }),
|
|
|
|
|
searchPack: async () => {},
|
|
|
|
|
deleteSession: async () => {},
|
|
|
|
|
failSession: async () => { failed += 1; },
|
|
|
|
|
} as any,
|
|
|
|
|
readiness: { ensure: async () => ({ ok: true }) } as any,
|
|
|
|
|
getSettings: () => ({ workspace: "local" }) as any,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await app.inject({ method: "POST", url: "/sessions", payload: { question: "old" } });
|
|
|
|
|
await app.inject({ method: "DELETE", url: "/sessions/s1" });
|
|
|
|
|
published.length = 0;
|
|
|
|
|
|
|
|
|
|
if (outcome === "resolve") oldConfigure.resolve();
|
|
|
|
|
else oldConfigure.reject(new Error("old bootstrap failed"));
|
|
|
|
|
await new Promise((resolve) => setImmediate(resolve));
|
|
|
|
|
|
|
|
|
|
expect(current).toBeUndefined();
|
|
|
|
|
expect(forgotten).toBe(true);
|
|
|
|
|
expect(failed).toBe(0);
|
|
|
|
|
expect(published).toEqual([]);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
test.each(["close", "delete"] as const)(
|
|
|
|
|
"Resume followed by %s leaves no runtime or post-removal bootstrap events",
|
|
|
|
|
async (lifecycle) => {
|
|
|
|
|
const reopen = deferred<void>();
|
|
|
|
|
const configure = deferred<void>();
|
|
|
|
|
let markReopenStarted!: () => void;
|
|
|
|
|
const reopenStarted = new Promise<void>((resolve) => { markReopenStarted = resolve; });
|
|
|
|
|
let current: any;
|
|
|
|
|
let removalSettled = false;
|
|
|
|
|
const published: any[] = [];
|
|
|
|
|
const removalEvents: string[] = [];
|
|
|
|
|
const replacement = {
|
|
|
|
|
bridge: {
|
|
|
|
|
turnState: () => "running",
|
|
|
|
|
onClientEvent: () => {},
|
|
|
|
|
emitClientEvent: () => {},
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
const teardown = (expected?: any) => {
|
|
|
|
|
if (expected && current !== expected) return false;
|
|
|
|
|
current = undefined;
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
const app = buildApp(loadConfig({ THT_HARNESS_DIR: "../harness" }), {
|
|
|
|
|
mgr: {
|
|
|
|
|
get: () => current,
|
|
|
|
|
createFor: () => { current = replacement; return replacement; },
|
|
|
|
|
configure: async () => configure.promise,
|
|
|
|
|
start: () => { throw new Error("removed runtime must not start"); },
|
|
|
|
|
teardown: () => { teardown(); },
|
|
|
|
|
teardownIfCurrent: (_id: string, expected: any) => teardown(expected),
|
|
|
|
|
} as any,
|
|
|
|
|
hub: {
|
|
|
|
|
publish: (_id: string, event: string, data: object) => {
|
|
|
|
|
published.push({ event, data });
|
|
|
|
|
return published.length;
|
|
|
|
|
},
|
|
|
|
|
clear: () => { removalEvents.push("clear"); },
|
|
|
|
|
forget: () => { removalEvents.push("forget"); },
|
|
|
|
|
} as any,
|
|
|
|
|
thtRunner: {
|
|
|
|
|
sessionShow: async () => ({ status: "open", archived: false }),
|
|
|
|
|
reopenSession: async () => {
|
|
|
|
|
markReopenStarted();
|
|
|
|
|
await reopen.promise;
|
|
|
|
|
},
|
|
|
|
|
closeSession: async () => { removalEvents.push("close"); },
|
|
|
|
|
deleteSession: async () => { removalEvents.push("delete"); },
|
|
|
|
|
failSession: async () => {},
|
|
|
|
|
} as any,
|
|
|
|
|
readiness: { ensure: async () => ({ ok: true }) } as any,
|
|
|
|
|
getSettings: () => ({ workspace: "local" }) as any,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const resumeResponse = app.inject({ method: "POST", url: "/sessions/s1/resume" });
|
|
|
|
|
await reopenStarted;
|
|
|
|
|
const removalResponse = app.inject({
|
|
|
|
|
method: lifecycle === "close" ? "POST" : "DELETE",
|
|
|
|
|
url: `/sessions/s1/${lifecycle === "close" ? "close" : ""}`.replace(/\/$/, ""),
|
|
|
|
|
}).then((response) => {
|
|
|
|
|
removalSettled = true;
|
|
|
|
|
return response;
|
|
|
|
|
});
|
|
|
|
|
await new Promise((resolve) => setImmediate(resolve));
|
|
|
|
|
expect(removalSettled).toBe(false);
|
|
|
|
|
|
|
|
|
|
reopen.resolve();
|
|
|
|
|
expect((await resumeResponse).json()).toEqual({ id: "s1", alreadyActive: false });
|
|
|
|
|
await removalResponse;
|
|
|
|
|
published.length = 0;
|
|
|
|
|
configure.resolve();
|
|
|
|
|
await new Promise((resolve) => setImmediate(resolve));
|
|
|
|
|
|
|
|
|
|
expect(current).toBeUndefined();
|
|
|
|
|
expect(published).toEqual([]);
|
|
|
|
|
expect(removalEvents.at(-1)).toBe(lifecycle === "close" ? "clear" : "forget");
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
test("Close followed by Resume installs a fresh runtime only after Close finishes", async () => {
|
|
|
|
|
const close = deferred<void>();
|
|
|
|
|
let markCloseStarted!: () => void;
|
|
|
|
|
const closeStarted = new Promise<void>((resolve) => { markCloseStarted = resolve; });
|
|
|
|
|
const oldRuntime = { bridge: { turnState: () => "running" } };
|
|
|
|
|
const replacement = {
|
|
|
|
|
bridge: {
|
|
|
|
|
turnState: () => "running",
|
|
|
|
|
onClientEvent: () => {},
|
|
|
|
|
emitClientEvent: () => {},
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
let current: any = oldRuntime;
|
|
|
|
|
let resumeSettled = false;
|
|
|
|
|
const teardown = (expected?: any) => {
|
|
|
|
|
if (expected && current !== expected) return false;
|
|
|
|
|
current = undefined;
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
const app = buildApp(loadConfig({ THT_HARNESS_DIR: "../harness" }), {
|
|
|
|
|
mgr: {
|
|
|
|
|
get: () => current,
|
|
|
|
|
createFor: () => { current = replacement; return replacement; },
|
|
|
|
|
configure: async () => {},
|
|
|
|
|
start: () => {},
|
|
|
|
|
teardown: () => { teardown(); },
|
|
|
|
|
teardownIfCurrent: (_id: string, expected: any) => teardown(expected),
|
|
|
|
|
} as any,
|
|
|
|
|
hub: { publish: () => 1, clear: () => {} } as any,
|
|
|
|
|
thtRunner: {
|
|
|
|
|
closeSession: async () => { markCloseStarted(); await close.promise; },
|
|
|
|
|
sessionShow: async () => ({ status: "open", archived: false }),
|
|
|
|
|
reopenSession: async () => {},
|
|
|
|
|
} as any,
|
|
|
|
|
readiness: { ensure: async () => ({ ok: true }) } as any,
|
|
|
|
|
getSettings: () => ({ workspace: "local" }) as any,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const closeResponse = app.inject({ method: "POST", url: "/sessions/s1/close" });
|
|
|
|
|
await closeStarted;
|
|
|
|
|
const resumeResponse = app.inject({ method: "POST", url: "/sessions/s1/resume" })
|
|
|
|
|
.then((response) => { resumeSettled = true; return response; });
|
|
|
|
|
await new Promise((resolve) => setImmediate(resolve));
|
|
|
|
|
expect(resumeSettled).toBe(false);
|
|
|
|
|
|
|
|
|
|
close.resolve();
|
|
|
|
|
await closeResponse;
|
|
|
|
|
const resumed = await resumeResponse;
|
|
|
|
|
await new Promise((resolve) => setImmediate(resolve));
|
|
|
|
|
|
|
|
|
|
expect(resumed.json()).toEqual({ id: "s1", alreadyActive: false });
|
|
|
|
|
expect(current).toBe(replacement);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("Close suppresses a bootstrap that settles while close persistence is pending", async () => {
|
|
|
|
|
const configure = deferred<void>();
|
|
|
|
|
const close = deferred<void>();
|
|
|
|
|
let markCloseStarted!: () => void;
|
|
|
|
|
const closeStarted = new Promise<void>((resolve) => { markCloseStarted = resolve; });
|
|
|
|
|
const published: any[] = [];
|
|
|
|
|
const starts: any[] = [];
|
|
|
|
|
let current: any;
|
|
|
|
|
let listener: ((event: any) => void) | undefined;
|
|
|
|
|
const runtime = {
|
|
|
|
|
bridge: {
|
|
|
|
|
onClientEvent: (next: (event: any) => void) => { listener = next; },
|
|
|
|
|
emitClientEvent: (event: any) => listener?.(event),
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
const app = buildApp(loadConfig({ THT_HARNESS_DIR: "../harness" }), {
|
|
|
|
|
mgr: {
|
|
|
|
|
get: () => current,
|
|
|
|
|
createFor: () => { current = runtime; return runtime; },
|
|
|
|
|
configure: async () => configure.promise,
|
|
|
|
|
start: (_id: string, expected: any) => { starts.push(expected); },
|
|
|
|
|
teardownIfCurrent: (_id: string, expected: any) => {
|
|
|
|
|
if (current !== expected) return false;
|
|
|
|
|
current = undefined;
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
} as any,
|
|
|
|
|
hub: {
|
|
|
|
|
publish: (_id: string, event: string, data: object) => {
|
|
|
|
|
published.push({ event, data });
|
|
|
|
|
return published.length;
|
|
|
|
|
},
|
|
|
|
|
clear: () => {},
|
|
|
|
|
} as any,
|
|
|
|
|
thtRunner: {
|
|
|
|
|
sessionNew: async () => ({ id: "s1" }),
|
|
|
|
|
searchPack: async () => {},
|
|
|
|
|
closeSession: async () => { markCloseStarted(); await close.promise; },
|
|
|
|
|
failSession: async () => {},
|
|
|
|
|
} as any,
|
|
|
|
|
readiness: { ensure: async () => ({ ok: true }) } as any,
|
|
|
|
|
getSettings: () => ({ workspace: "local" }) as any,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await app.inject({ method: "POST", url: "/sessions", payload: { question: "q" } });
|
|
|
|
|
const closeResponse = app.inject({ method: "POST", url: "/sessions/s1/close" });
|
|
|
|
|
await closeStarted;
|
|
|
|
|
published.length = 0;
|
|
|
|
|
configure.resolve();
|
|
|
|
|
await new Promise((resolve) => setImmediate(resolve));
|
|
|
|
|
|
|
|
|
|
expect(starts).toEqual([]);
|
|
|
|
|
expect(published).toEqual([]);
|
|
|
|
|
|
|
|
|
|
close.resolve();
|
|
|
|
|
await closeResponse;
|
|
|
|
|
expect(current).toBeUndefined();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("bootstrap failure persists once and keeps Resume serialized behind that persistence", async () => {
|
|
|
|
|
const oldConfigure = deferred<void>();
|
|
|
|
|
const failurePersistence = deferred<void>();
|
|
|
|
|
let markFailureStarted!: () => void;
|
|
|
|
|
const failureStarted = new Promise<void>((resolve) => { markFailureStarted = resolve; });
|
|
|
|
|
const oldRuntime = {
|
|
|
|
|
bridge: {
|
|
|
|
|
onClientEvent: undefined as ((next: (event: any) => void) => void) | undefined,
|
|
|
|
|
emitClientEvent: undefined as ((event: any) => void) | undefined,
|
|
|
|
|
},
|
|
|
|
|
} as any;
|
|
|
|
|
const replacement = {
|
|
|
|
|
bridge: {
|
|
|
|
|
turnState: () => "running",
|
|
|
|
|
onClientEvent: () => {},
|
|
|
|
|
emitClientEvent: () => {},
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
let oldListener: ((event: any) => void) | undefined;
|
|
|
|
|
oldRuntime.bridge.onClientEvent = (next: (event: any) => void) => { oldListener = next; };
|
|
|
|
|
oldRuntime.bridge.emitClientEvent = (event: any) => oldListener?.(event);
|
|
|
|
|
const runtimes = [oldRuntime, replacement];
|
|
|
|
|
let current: any;
|
|
|
|
|
let configureCalls = 0;
|
|
|
|
|
let failCalls = 0;
|
|
|
|
|
let resumeSettled = false;
|
|
|
|
|
const app = buildApp(loadConfig({ THT_HARNESS_DIR: "../harness" }), {
|
|
|
|
|
mgr: {
|
|
|
|
|
get: () => current,
|
|
|
|
|
createFor: () => { current = runtimes.shift(); return current; },
|
|
|
|
|
configure: async () => {
|
|
|
|
|
configureCalls += 1;
|
|
|
|
|
if (configureCalls === 1) await oldConfigure.promise;
|
|
|
|
|
},
|
|
|
|
|
start: () => {},
|
|
|
|
|
teardownIfCurrent: (_id: string, expected: any) => {
|
|
|
|
|
if (current !== expected) return false;
|
|
|
|
|
current = undefined;
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
} as any,
|
|
|
|
|
hub: { publish: () => 1, clear: () => {} } as any,
|
|
|
|
|
thtRunner: {
|
|
|
|
|
sessionNew: async () => ({ id: "s1" }),
|
|
|
|
|
searchPack: async () => {},
|
|
|
|
|
sessionShow: async () => ({ status: "open", archived: false }),
|
|
|
|
|
reopenSession: async () => {},
|
|
|
|
|
failSession: async () => {
|
|
|
|
|
failCalls += 1;
|
|
|
|
|
if (failCalls === 1) {
|
|
|
|
|
markFailureStarted();
|
|
|
|
|
await failurePersistence.promise;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
} as any,
|
|
|
|
|
readiness: { ensure: async () => ({ ok: true }) } as any,
|
|
|
|
|
getSettings: () => ({ workspace: "local" }) as any,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await app.inject({ method: "POST", url: "/sessions", payload: { question: "q" } });
|
|
|
|
|
oldConfigure.reject(new Error("configure failed"));
|
|
|
|
|
await failureStarted;
|
|
|
|
|
const resumeResponse = app.inject({ method: "POST", url: "/sessions/s1/resume" })
|
|
|
|
|
.then((response) => { resumeSettled = true; return response; });
|
|
|
|
|
await new Promise((resolve) => setImmediate(resolve));
|
|
|
|
|
expect(resumeSettled).toBe(false);
|
|
|
|
|
|
|
|
|
|
failurePersistence.resolve();
|
|
|
|
|
expect((await resumeResponse).json()).toEqual({ id: "s1", alreadyActive: false });
|
|
|
|
|
await new Promise((resolve) => setImmediate(resolve));
|
|
|
|
|
|
|
|
|
|
expect(failCalls).toBe(1);
|
|
|
|
|
expect(current).toBe(replacement);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("Delete followed by Resume cannot resurrect the deleted session", async () => {
|
|
|
|
|
const deletion = deferred<void>();
|
|
|
|
|
let markDeleteStarted!: () => void;
|
|
|
|
|
const deleteStarted = new Promise<void>((resolve) => { markDeleteStarted = resolve; });
|
|
|
|
|
const oldRuntime = { bridge: { turnState: () => "running" } };
|
|
|
|
|
let current: any = oldRuntime;
|
|
|
|
|
let deleted = false;
|
|
|
|
|
let resumeSettled = false;
|
|
|
|
|
let createCalls = 0;
|
|
|
|
|
const teardown = (expected?: any) => {
|
|
|
|
|
if (expected && current !== expected) return false;
|
|
|
|
|
current = undefined;
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
const app = buildApp(loadConfig({ THT_HARNESS_DIR: "../harness" }), {
|
|
|
|
|
mgr: {
|
|
|
|
|
get: () => current,
|
|
|
|
|
createFor: () => {
|
|
|
|
|
createCalls += 1;
|
|
|
|
|
current = { bridge: { onClientEvent: () => {}, emitClientEvent: () => {} } };
|
|
|
|
|
return current;
|
|
|
|
|
},
|
|
|
|
|
configure: async () => {},
|
|
|
|
|
start: () => {},
|
|
|
|
|
teardown: () => { teardown(); },
|
|
|
|
|
teardownIfCurrent: (_id: string, expected: any) => teardown(expected),
|
|
|
|
|
} as any,
|
|
|
|
|
hub: { publish: () => 1, clear: () => {}, forget: () => {} } as any,
|
|
|
|
|
thtRunner: {
|
|
|
|
|
deleteSession: async () => {
|
|
|
|
|
markDeleteStarted();
|
|
|
|
|
await deletion.promise;
|
|
|
|
|
deleted = true;
|
|
|
|
|
},
|
|
|
|
|
sessionShow: async () => {
|
|
|
|
|
if (deleted) throw new Error("session deleted");
|
|
|
|
|
return { status: "open", archived: false };
|
|
|
|
|
},
|
|
|
|
|
reopenSession: async () => {},
|
|
|
|
|
} as any,
|
|
|
|
|
readiness: { ensure: async () => ({ ok: true }) } as any,
|
|
|
|
|
getSettings: () => ({ workspace: "local" }) as any,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const deleteResponse = app.inject({ method: "DELETE", url: "/sessions/s1" });
|
|
|
|
|
await deleteStarted;
|
|
|
|
|
const resumeResponse = app.inject({ method: "POST", url: "/sessions/s1/resume" })
|
|
|
|
|
.then((response) => { resumeSettled = true; return response; });
|
|
|
|
|
await new Promise((resolve) => setImmediate(resolve));
|
|
|
|
|
expect(resumeSettled).toBe(false);
|
|
|
|
|
|
|
|
|
|
deletion.resolve();
|
|
|
|
|
await deleteResponse;
|
|
|
|
|
const resumed = await resumeResponse;
|
|
|
|
|
|
|
|
|
|
expect(resumed.statusCode).toBe(500);
|
|
|
|
|
expect(current).toBeUndefined();
|
|
|
|
|
expect(createCalls).toBe(0);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("a replaced runtime cannot publish or fail the newly resumed session", async () => {
|
|
|
|
|
const controlledBridge = (initialState: string) => {
|
|
|
|
|
let state = initialState;
|
|
|
|
@@ -448,7 +971,11 @@ test("a replaced runtime cannot publish or fail the newly resumed session", asyn
|
|
|
|
|
createFor: () => { current = runtimes.shift(); return current; },
|
|
|
|
|
configure: async () => {},
|
|
|
|
|
start: () => {},
|
|
|
|
|
teardown: () => { current = undefined; },
|
|
|
|
|
teardownIfCurrent: (_id: string, expected: any) => {
|
|
|
|
|
if (current !== expected) return false;
|
|
|
|
|
current = undefined;
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
} as any,
|
|
|
|
|
hub: {
|
|
|
|
|
clear: () => {},
|
|
|
|
@@ -505,7 +1032,11 @@ test("a deleted runtime cannot repopulate or fail the forgotten session", async
|
|
|
|
|
createFor: () => { current = runtime; return runtime; },
|
|
|
|
|
configure: async () => {},
|
|
|
|
|
start: () => {},
|
|
|
|
|
teardown: () => { current = undefined; },
|
|
|
|
|
teardownIfCurrent: (_id: string, expected: any) => {
|
|
|
|
|
if (current !== expected) return false;
|
|
|
|
|
current = undefined;
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
} as any,
|
|
|
|
|
hub: {
|
|
|
|
|
publish: (_id: string, event: string, data: object) => {
|
|
|
|
@@ -604,7 +1135,12 @@ test("POST resume tears down a created runtime when bridge binding fails", async
|
|
|
|
|
const app = buildApp(loadConfig({ THT_HARNESS_DIR: "../harness" }), {
|
|
|
|
|
mgr: {
|
|
|
|
|
get: () => current,
|
|
|
|
|
teardown: () => { teardownCalls += 1; current = undefined; },
|
|
|
|
|
teardownIfCurrent: (_id: string, expected: any) => {
|
|
|
|
|
if (current !== expected) return false;
|
|
|
|
|
teardownCalls += 1;
|
|
|
|
|
current = undefined;
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
createFor: () => {
|
|
|
|
|
current = {
|
|
|
|
|
bridge: {
|
|
|
|
@@ -688,9 +1224,17 @@ test("DELETE /sessions/:id calls deleteSession", async () => {
|
|
|
|
|
|
|
|
|
|
test("DELETE /sessions/:id tears down the runtime before deleting on disk", async () => {
|
|
|
|
|
const order: string[] = [];
|
|
|
|
|
const runtime = {} as any;
|
|
|
|
|
const app = buildApp(loadConfig({ THT_HARNESS_DIR: "../harness" }), {
|
|
|
|
|
thtRunner: { deleteSession: async (id: string) => { order.push(`del:${id}`); } } as any,
|
|
|
|
|
mgr: { teardown: (id: string) => { order.push(`teardown:${id}`); } } as any,
|
|
|
|
|
mgr: {
|
|
|
|
|
get: () => runtime,
|
|
|
|
|
teardownIfCurrent: (id: string, expected: any) => {
|
|
|
|
|
expect(expected).toBe(runtime);
|
|
|
|
|
order.push(`teardown:${id}`);
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
} as any,
|
|
|
|
|
hub: { forget: (id: string) => { order.push(`forget:${id}`); } } as any,
|
|
|
|
|
getSettings: () => ({ workspace: "w" }) as any,
|
|
|
|
|
spawnFn: () => nodeSpawn("node", [FAKE, SCRIPT]) as any,
|
|
|
|
@@ -704,7 +1248,7 @@ test("POST /sessions/:id/close clears transient hub state without forgetting its
|
|
|
|
|
const calls: string[] = [];
|
|
|
|
|
const app = buildApp(loadConfig({ THT_HARNESS_DIR: "../harness" }), {
|
|
|
|
|
thtRunner: { closeSession: async () => {} } as any,
|
|
|
|
|
mgr: { teardown: () => {} } as any,
|
|
|
|
|
mgr: { get: () => undefined } as any,
|
|
|
|
|
hub: {
|
|
|
|
|
clear: (id: string) => { calls.push(`clear:${id}`); },
|
|
|
|
|
forget: (id: string) => { calls.push(`forget:${id}`); },
|
|
|
|
@@ -833,14 +1377,20 @@ test("POST /sessions returns after bridge attachment but starts only after retri
|
|
|
|
|
emitClientEvent: () => {},
|
|
|
|
|
};
|
|
|
|
|
const runtime = { bridge } as any;
|
|
|
|
|
let current: any;
|
|
|
|
|
const mgr = {
|
|
|
|
|
createFor: () => runtime,
|
|
|
|
|
get: () => current,
|
|
|
|
|
createFor: () => { current = runtime; return runtime; },
|
|
|
|
|
configure: async () => {},
|
|
|
|
|
start: () => {
|
|
|
|
|
expect(bridgeAttached).toBe(true);
|
|
|
|
|
started = true;
|
|
|
|
|
},
|
|
|
|
|
teardown: () => {},
|
|
|
|
|
teardownIfCurrent: (_id: string, expected: any) => {
|
|
|
|
|
if (current !== expected) return false;
|
|
|
|
|
current = undefined;
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
} as any;
|
|
|
|
|
const app = buildApp(loadConfig({ THT_HARNESS_DIR: "../harness" }), {
|
|
|
|
|
mgr,
|
|
|
|
@@ -871,13 +1421,18 @@ test("POST /sessions bootstrap failure emits only a fixed recovery message", asy
|
|
|
|
|
const runtime = { bridge } as any;
|
|
|
|
|
const rawFailure =
|
|
|
|
|
"connect https://secret.invalid/bootstrap?token=DO_NOT_LEAK using /srv/private/model-key";
|
|
|
|
|
let current: any;
|
|
|
|
|
const app = buildApp(loadConfig({ THT_HARNESS_DIR: "../harness" }), {
|
|
|
|
|
mgr: {
|
|
|
|
|
get: () => undefined,
|
|
|
|
|
createFor: () => runtime,
|
|
|
|
|
get: () => current,
|
|
|
|
|
createFor: () => { current = runtime; return runtime; },
|
|
|
|
|
configure: async () => { throw new Error(rawFailure); },
|
|
|
|
|
start: () => {},
|
|
|
|
|
teardown: () => {},
|
|
|
|
|
teardownIfCurrent: (_id: string, expected: any) => {
|
|
|
|
|
if (current !== expected) return false;
|
|
|
|
|
current = undefined;
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
} as any,
|
|
|
|
|
hub: {
|
|
|
|
|
publish: (_id: string, event: string, data: any) => published.push({ event, data }),
|
|
|
|
|