-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai-tools.test.ts
More file actions
98 lines (92 loc) · 3.18 KB
/
Copy pathai-tools.test.ts
File metadata and controls
98 lines (92 loc) · 3.18 KB
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
90
91
92
93
94
95
96
97
98
import { describe, expect, it, vi } from "vitest";
import { executeBuiltinAiTool } from "./ai-tools";
const now = new Date("2026-08-02T12:34:56.000+09:00");
describe("Ask AI built-in tools", () => {
it("returns an exact localized time without network access", async () => {
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone || "UTC";
const offsetMinutes = -now.getTimezoneOffset();
const offset = `${offsetMinutes >= 0 ? "+" : "-"}`
+ `${String(Math.floor(Math.abs(offsetMinutes) / 60)).padStart(2, "0")}`
+ `:${String(Math.abs(offsetMinutes) % 60).padStart(2, "0")}`;
const result = await executeBuiltinAiTool("get_current_time", "{}", {
locale: "ko",
now: () => now,
getLocation: () => ({ status: "unavailable" }),
searchWeb: vi.fn(),
});
expect(result.ok).toBe(true);
if (!result.ok) throw new Error("expected time result");
expect(result.data).toMatchObject({
iso: now.toISOString(),
timezone,
utcOffset: offset,
});
});
it("returns exact live GPS only when current and rejects demo/cache data", async () => {
const live = await executeBuiltinAiTool("get_current_location", "{}", {
locale: "en",
now: () => now,
getLocation: () => ({
status: "fresh",
fetchedAt: now.getTime() - 1_000,
value: {
coordinate: { latitude: 37.5, longitude: 127.01 },
source: "live",
accuracy: 4.2,
heading: 180,
speed: 1.5,
},
}),
searchWeb: vi.fn(),
});
expect(live).toMatchObject({
ok: true,
data: { latitude: 37.5, longitude: 127.01, accuracyMeters: 4.2 },
});
const demo = await executeBuiltinAiTool("get_current_location", "{}", {
locale: "en",
now: () => now,
getLocation: () => ({
status: "fresh",
fetchedAt: now.getTime(),
value: {
coordinate: { latitude: 37.5, longitude: 127.01 },
source: "demo",
},
}),
searchWeb: vi.fn(),
});
expect(demo).toEqual({
ok: false,
error: { code: "LOCATION_UNAVAILABLE", message: "Live GPS is unavailable" },
});
});
it("bounds web search queries and returns citations and usage", async () => {
const searchWeb = vi.fn().mockResolvedValue({
answer: "Grounded answer [1]",
sources: [{ title: "Source", url: "https://example.com/a" }],
usage: { inputTokens: 12, outputTokens: 8, webSearchCalls: 1 },
});
const result = await executeBuiltinAiTool(
"search_web",
JSON.stringify({ query: "latest Even Realities firmware" }),
{
locale: "en",
now: () => now,
getLocation: () => ({ status: "unavailable" }),
searchWeb,
},
);
expect(searchWeb).toHaveBeenCalledWith(
"latest Even Realities firmware",
"en",
);
expect(result).toMatchObject({ ok: true, data: { answer: "Grounded answer [1]" } });
await expect(executeBuiltinAiTool("search_web", "{}", {
locale: "en",
now: () => now,
getLocation: () => ({ status: "unavailable" }),
searchWeb,
})).resolves.toMatchObject({ ok: false, error: { code: "INVALID_ARGUMENTS" } });
});
});