Files
openworker/harness/examples/quickstart/test/integration.test.mjs

143 lines
5.2 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import { mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { startHarness, createAgents, streamAnswer } from "../dist/agent.js";
import { createApplication } from "../web/server.mjs";
import { makePdf, modelFixture } from "./fixtures.mjs";
for (const pdfMode of ["native", "text"]) test(
`actual Rust SDK host streams summaries and isolated ${pdfMode} PDF answers through the web API`,
{ skip: !process.env.OPENWORKER_EXECUTABLE, timeout: 120000 },
async (t) => {
const fixture = await modelFixture();
const root = await mkdtemp(join(tmpdir(), "ow-tracks-"));
let app, h;
const saved = { ...process.env };
Object.assign(process.env, {
OPENWORKER_MODEL: "fixture",
OPENWORKER_MODEL_URL: fixture.url,
OPENAI_API_KEY: "fixture-only",
});
delete process.env.OPENWORKER_PROVIDER;
t.after(async () => {
await app?.close();
await h?.shutdown();
await fixture.close();
await rm(root, { recursive: true, force: true });
for (const key of [
"OPENWORKER_MODEL",
"OPENWORKER_MODEL_URL",
"OPENAI_API_KEY",
"OPENWORKER_PROVIDER",
]) {
if (saved[key] === undefined) delete process.env[key];
else process.env[key] = saved[key];
}
});
const runtime = await startHarness(join(root, "desktop"));
h = runtime.harness;
const session = await createAgents(
h,
runtime.model,
).summarizer.newSession();
const events = [];
await streamAnswer(
session,
'Summarize src/main.ts: export const greeting = "hello";',
(event) => events.push(event),
);
assert.ok(events.some((e) => e.type === "snapshot"));
assert.match(events.at(-1).text, /src\/main.ts/);
app = await createApplication({
pdfMode,
stateDir: join(root, "web"),
accessToken: "test-access",
});
await new Promise((resolve) => app.server.listen(0, "127.0.0.1", resolve));
const url = `http://127.0.0.1:${app.server.address().port}`;
assert.equal(
(await fetch(url + "/api/documents", { method: "POST" })).status,
401,
);
assert.equal(
(
await fetch(url + "/api/documents", {
method: "POST",
headers: {
Authorization: "Bearer test-access",
Origin: "http://attacker.invalid",
},
})
).status,
403,
);
const upload = async (text) => {
const r = await fetch(url + "/api/documents", {
method: "POST",
headers: {
Authorization: "Bearer test-access",
"Content-Type": "application/pdf",
},
body: makePdf(text),
});
assert.equal(r.status, 201);
return r.json();
};
const orbit = await upload("Orbit revenue is 42 credits.");
const cedar = await upload("Cedar budget is 17 credits.");
assert.notEqual(orbit.id, cedar.id);
const ask = async (id, question) => {
const r = await fetch(url + "/api/ask", {
method: "POST",
headers: {
Authorization: "Bearer test-access",
"Content-Type": "application/json",
},
body: JSON.stringify({ id, question }),
});
assert.equal(r.status, 200);
const events = (await r.text()).trim().split("\n").map(JSON.parse);
assert.ok(events.some((e) => e.type === "snapshot"));
assert.equal(events.at(-1).type, "done");
return events.at(-1).text;
};
assert.match(
await ask(orbit.id, "What is the revenue?"),
/42 credits.*\[p\. 1\]/,
);
assert.match(
await ask(cedar.id, "What is the budget?"),
/17 credits.*\[p\. 1\]/,
);
assert.match(await ask(orbit.id, "Repeat the revenue."), /42 credits/);
assert.ok(
!JSON.stringify(fixture.requests[2]).includes("Orbit revenue"),
"PDF sessions must not share document content",
);
const nativeFiles = fixture.requests.flatMap(request => request.input ?? [])
.flatMap(item => item.content ?? []).filter(part => part.type === "input_file");
if (pdfMode === "native") assert.ok(nativeFiles.length >= 2, "PDFs must reach the provider as native file parts");
else assert.equal(nativeFiles.length, 0, "Text mode must use extracted text");
assert.ok(nativeFiles.every(part => part.file_data.startsWith("data:application/pdf;base64,")));
if (pdfMode === "native") {
const cedarInput = (fixture.requests[2].input ?? []).flatMap(item => item.content ?? [])
.filter(part => part.type === "input_file")
.map(part => Buffer.from(part.file_data.split(",").at(-1), "base64").toString("utf8")).join("");
assert.match(cedarInput, /Cedar budget/);
assert.ok(!cedarInput.includes("Orbit revenue"));
}
for (const request of fixture.requests)
assert.deepEqual(request.tools ?? [], []);
const unknown = await fetch(url + "/api/ask", {
method: "POST",
headers: {
Authorization: "Bearer test-access",
"Content-Type": "application/json",
},
body: JSON.stringify({ id: "missing", question: "Hi" }),
});
assert.equal(unknown.status, 404);
},
);