-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathutil.test.ts
More file actions
136 lines (111 loc) · 4.57 KB
/
Copy pathutil.test.ts
File metadata and controls
136 lines (111 loc) · 4.57 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { describe, test, expect, mock, afterEach } from "bun:test"
import os from "node:os"
import { errorSummary, setBoundedMap, isMetricEnabled, isTraceEnabled, safeUsername } from "../src/util.ts"
import { MAX_PENDING } from "../src/types.ts"
describe("safeUsername", () => {
const originalUserInfo = os.userInfo
afterEach(() => {
os.userInfo = originalUserInfo
})
test("returns the OS username on success", () => {
const u = safeUsername()
expect(typeof u).toBe("string")
expect((u as string).length).toBeGreaterThan(0)
})
test("returns undefined when os.userInfo() throws", () => {
os.userInfo = mock(() => { throw new Error("no passwd entry") }) as typeof os.userInfo
expect(safeUsername()).toBeUndefined()
})
})
describe("errorSummary", () => {
test("returns 'unknown' for undefined", () => {
expect(errorSummary(undefined)).toBe("unknown")
})
test("returns name when no data", () => {
expect(errorSummary({ name: "APIError" })).toBe("APIError")
})
test("returns name when data has no message", () => {
expect(errorSummary({ name: "APIError", data: { code: 500 } })).toBe("APIError")
})
test("returns name: message when data has message", () => {
expect(errorSummary({ name: "APIError", data: { message: "rate limited" } })).toBe(
"APIError: rate limited",
)
})
test("returns name when data is a primitive", () => {
expect(errorSummary({ name: "APIError", data: "oops" })).toBe("APIError")
})
})
describe("setBoundedMap", () => {
test("adds an entry to the map", () => {
const map = new Map<string, number>()
setBoundedMap(map, "a", 1)
expect(map.get("a")).toBe(1)
})
test("evicts the oldest entry when at capacity", () => {
const map = new Map<string, number>()
for (let i = 0; i < MAX_PENDING; i++) {
setBoundedMap(map, `key-${i}`, i)
}
expect(map.size).toBe(MAX_PENDING)
expect(map.has("key-0")).toBe(true)
setBoundedMap(map, "overflow", 999)
expect(map.size).toBe(MAX_PENDING)
expect(map.has("key-0")).toBe(false)
expect(map.has("overflow")).toBe(true)
})
test("does not evict when below capacity", () => {
const map = new Map<string, number>()
setBoundedMap(map, "a", 1)
setBoundedMap(map, "b", 2)
expect(map.size).toBe(2)
expect(map.has("a")).toBe(true)
})
test("overwrites an existing key without evicting", () => {
const map = new Map<string, number>()
setBoundedMap(map, "a", 1)
setBoundedMap(map, "a", 2)
expect(map.get("a")).toBe(2)
expect(map.size).toBe(1)
})
})
describe("isMetricEnabled", () => {
test("returns true when disabled set is empty", () => {
expect(isMetricEnabled("session.count", { disabledMetrics: new Set() })).toBe(true)
})
test("returns false when metric is in the disabled set", () => {
expect(isMetricEnabled("session.count", { disabledMetrics: new Set(["session.count"]) })).toBe(false)
})
test("returns true when a different metric is disabled", () => {
expect(isMetricEnabled("session.count", { disabledMetrics: new Set(["cache.count"]) })).toBe(true)
})
test("is case-sensitive — does not match mismatched case", () => {
expect(isMetricEnabled("session.count", { disabledMetrics: new Set(["Session.Count"]) })).toBe(true)
})
test("unknown metric names in disabled set do not affect known metrics", () => {
expect(isMetricEnabled("retry.count", { disabledMetrics: new Set(["does.not.exist"]) })).toBe(true)
})
})
describe("isTraceEnabled", () => {
test("returns true when disabled set is empty", () => {
expect(isTraceEnabled("session", { disabledTraces: new Set() })).toBe(true)
})
test("returns false when trace type is in the disabled set", () => {
expect(isTraceEnabled("session", { disabledTraces: new Set(["session"]) })).toBe(false)
})
test("returns false for llm when llm is disabled", () => {
expect(isTraceEnabled("llm", { disabledTraces: new Set(["llm"]) })).toBe(false)
})
test("returns false for tool when tool is disabled", () => {
expect(isTraceEnabled("tool", { disabledTraces: new Set(["tool"]) })).toBe(false)
})
test("returns true when a different trace type is disabled", () => {
expect(isTraceEnabled("session", { disabledTraces: new Set(["tool"]) })).toBe(true)
})
test("is case-sensitive — does not match mismatched case", () => {
expect(isTraceEnabled("session", { disabledTraces: new Set(["Session"]) })).toBe(true)
})
test("unknown trace names in disabled set do not affect known types", () => {
expect(isTraceEnabled("llm", { disabledTraces: new Set(["does_not_exist"]) })).toBe(true)
})
})