Spaces:
Running

WorkPilot-AI / web /src /test /fixtures.ts
Aynkhn's picture
Deploy WorkPilot demo (part 3)
03608f2 verified
Raw History Blame Contribute Delete
3.47 kB
import { vi } from "vitest";
import type { ApprovalItem, Me } from "@/lib/types";
export const ME: Me = {
user: { id: "u1", email: "owner@example.pk", totp_enrolled: true },
tenant: { id: "t1", name: "Karachi Crafts", plan: "free", locale: "en", timezone: "Asia/Karachi" },
role: "owner",
tenants: [{ id: "t1", name: "Karachi Crafts", role: "owner" }],
};
export function refundItem(over: Partial<ApprovalItem> = {}): ApprovalItem {
return {
id: "a-refund",
tool: "demo.refund_order",
status: "pending",
reason: "no policy permits this call automatically",
policy_rule: null,
args: { order_number: "ORD-1021", amount: 20000, reason: "damaged" },
provenance: { order_number: "untrusted", amount: "untrusted", reason: "untrusted" },
preview: { order: "ORD-1021", amount: 20000, refunded_before: 0, refunded_after: 20000 },
required_role: "manager",
requires_stepup: true,
run_id: "r1",
created_at: new Date(Date.now() - 125_000).toISOString(),
expires_at: new Date(Date.now() + 86_400_000).toISOString(),
wait_seconds: 125,
kind: "review",
risk_tier: 2,
args_schema: {
properties: {
order_number: { type: "string", pattern: String.raw`^ORD-\d{3,8}$`, title: "Order Number" },
amount: { type: "integer", exclusiveMinimum: 0, title: "Amount" },
reason: { type: "string", maxLength: 200, title: "Reason" },
},
required: ["order_number", "amount", "reason"],
},
facts: { order_found: true, order_total: 25000, refundable: 25000 },
...over,
};
}
export function cancelItem(over: Partial<ApprovalItem> = {}): ApprovalItem {
return refundItem({
id: "a-cancel",
tool: "demo.cancel_order",
args: { order_number: "ORD-1001" },
provenance: { order_number: "trusted" },
preview: { order: "ORD-1001", status_before: "pending", status_after: "cancelled" },
requires_stepup: false,
facts: {},
args_schema: { properties: { order_number: { type: "string", title: "Order Number" } } },
...over,
});
}
type Handler = (url: URL, init: RequestInit | undefined) => unknown;
/** Route fetch by "METHOD /path"; the session BFF and /me are authenticated by default. */
export function mockFetch(routes: Record<string, Handler> = {}) {
const calls: { method: string; path: string; body: unknown; headers: Record<string, string> }[] = [];
const all: Record<string, Handler> = {
"POST /api/session/refresh": () => ({ access_token: "tok", tenant_id: "t1" }),
"GET /me": () => ME,
...routes,
};
const fn = vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => {
const url = new URL(String(input), "http://localhost");
const method = (init?.method ?? "GET").toUpperCase();
const key = `${method} ${url.pathname}`;
calls.push({
method,
path: url.pathname,
body: init?.body ? JSON.parse(String(init.body)) : undefined,
headers: (init?.headers ?? {}) as Record<string, string>,
});
const h = all[key];
if (!h) return new Response(JSON.stringify({ detail: "not found" }), { status: 404 });
const out = h(url, init);
if (out instanceof Response) return out;
return new Response(JSON.stringify(out), {
status: 200,
headers: { "Content-Type": "application/json" },
});
});
vi.stubGlobal("fetch", fn);
return { fn, calls };
}