149 lines
4.1 KiB
TypeScript
149 lines
4.1 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it } from "bun:test";
|
|
import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
|
|
import { ConversationStore } from "../../src/conversations/store.js";
|
|
import { ResultReferenceResolver } from "../../src/results/resolver.js";
|
|
import {
|
|
RESULT_REFERENCE_KIND,
|
|
RESULT_REFERENCE_SOURCE,
|
|
ResultReferenceStore,
|
|
} from "../../src/results/store.js";
|
|
import { createTestDatabase } from "../helpers/postgres.js";
|
|
|
|
describe("ResultReferenceResolver", () => {
|
|
let payloadDir: string;
|
|
let conversationStore: ConversationStore;
|
|
let store: ResultReferenceStore;
|
|
let resolver: ResultReferenceResolver;
|
|
let dispose: (() => Promise<void>) | undefined;
|
|
|
|
beforeEach(async () => {
|
|
payloadDir = await mkdtemp(join(tmpdir(), "tjwater-result-ref-"));
|
|
const testDb = await createTestDatabase();
|
|
conversationStore = new ConversationStore(testDb.db);
|
|
store = new ResultReferenceStore(testDb.db, payloadDir, 60_000);
|
|
resolver = new ResultReferenceResolver(store);
|
|
dispose = async () => {
|
|
await testDb.dispose();
|
|
await rm(payloadDir, { force: true, recursive: true });
|
|
};
|
|
await Promise.all([conversationStore.initialize(), store.initialize()]);
|
|
});
|
|
|
|
afterEach(async () => {
|
|
await dispose?.();
|
|
});
|
|
|
|
it("stores referenced results as metadata plus an external payload file", async () => {
|
|
await conversationStore.ensure({
|
|
actorKey: "actor-1",
|
|
projectId: "project-1",
|
|
projectKey: "project-key-1",
|
|
sessionId: "client-1",
|
|
userId: "user-1",
|
|
});
|
|
|
|
const record = await resolver.register({
|
|
actorKey: "actor-1",
|
|
data: [{ id: "J1" }, { id: "J2" }],
|
|
kind: RESULT_REFERENCE_KIND.dynamicHttpResult,
|
|
projectId: "project-1",
|
|
projectKey: "project-key-1",
|
|
schemaVersion: 1,
|
|
sessionId: "client-1",
|
|
source: RESULT_REFERENCE_SOURCE.dynamicHttp,
|
|
traceId: "trace-1",
|
|
});
|
|
|
|
expect(record.payloadPath).toContain("/payloads/");
|
|
const payload = JSON.parse(await readFile(record.payloadPath!, "utf8")) as {
|
|
data?: unknown;
|
|
};
|
|
expect(payload.data).toEqual([{ id: "J1" }, { id: "J2" }]);
|
|
|
|
const result = await resolver.getAuthorized(
|
|
record.resultRef,
|
|
{
|
|
actorKey: "actor-1",
|
|
projectId: "project-1",
|
|
},
|
|
{
|
|
maxItems: 1,
|
|
},
|
|
);
|
|
|
|
expect(result?.kind).toBe(RESULT_REFERENCE_KIND.dynamicHttpResult);
|
|
expect(result?.source).toBe(RESULT_REFERENCE_SOURCE.dynamicHttp);
|
|
expect(result?.data).toEqual([{ id: "J1" }]);
|
|
});
|
|
|
|
it("reuses an existing external render payload file as the payload locator", async () => {
|
|
await conversationStore.ensure({
|
|
actorKey: "actor-3",
|
|
projectId: "project-3",
|
|
projectKey: "project-key-3",
|
|
sessionId: "client-3",
|
|
userId: "user-3",
|
|
});
|
|
|
|
const filePath = join(payloadDir, "render-wrapper.json");
|
|
await writeFile(
|
|
filePath,
|
|
JSON.stringify(
|
|
{
|
|
metadata: {
|
|
createdAt: "2026-05-21T00:00:00.000Z",
|
|
projectId: "project-3",
|
|
},
|
|
location: {
|
|
file_path: filePath,
|
|
},
|
|
data: {
|
|
node_area_map: {
|
|
J1: "DMA-1",
|
|
J2: 2,
|
|
},
|
|
area_ids: ["DMA-1", " DMA-2 "],
|
|
},
|
|
},
|
|
null,
|
|
2,
|
|
),
|
|
"utf8",
|
|
);
|
|
|
|
const record = await resolver.registerRenderPayloadFile(filePath, {
|
|
actorKey: "actor-3",
|
|
projectId: "project-3",
|
|
projectKey: "project-key-3",
|
|
sessionId: "client-3",
|
|
source: RESULT_REFERENCE_SOURCE.migration,
|
|
traceId: "trace-3",
|
|
});
|
|
|
|
expect(record.payloadPath).toBe(filePath);
|
|
|
|
const result = await resolver.getFullAuthorized(
|
|
record.resultRef,
|
|
{
|
|
actorKey: "actor-3",
|
|
projectId: "project-3",
|
|
sessionId: "client-3",
|
|
},
|
|
{
|
|
expectedKind: RESULT_REFERENCE_KIND.renderJunctionsPayload,
|
|
},
|
|
);
|
|
|
|
expect(result?.data).toEqual({
|
|
node_area_map: {
|
|
J1: "DMA-1",
|
|
J2: "2",
|
|
},
|
|
area_ids: ["DMA-1", "DMA-2"],
|
|
});
|
|
});
|
|
});
|