Spaces:
Running

File size: 3,467 Bytes
03608f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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 };
}