benjivers/jev-mcp Public Public: anyone can read this; nobody can change it.

workbenchthis machine
Files 70d5d708f3dbeab8fa42d6f7ad0e6733dfd66f3b
122 lines · 5.3 KB · ~1.6k tok Raw History
1
// Unit tests for the pure helpers. No network, no child process.
2
3
import assert from "node:assert/strict";
4
import test from "node:test";
5
import { APIError } from "@typesafe-ai/sdk";
6
import {
7
  assertStateWithinLimit,
8
  assertUniqueIds,
9
  buildChoiceCriteria,
10
  describeError,
11
  gateConfidence,
12
  gateProbability,
13
  MAX_CHOICE_OPTIONS,
14
  readPositiveInt,
15
  resolveNoneKey,
16
  stateSize,
17
} from "../dist/lib.js";
18
19
test("readPositiveInt falls back loudly instead of yielding NaN", () => {
20
  assert.deepEqual(readPositiveInt(undefined, 15000, "T"), { value: 15000 });
21
  assert.deepEqual(readPositiveInt("  ", 15000, "T"), { value: 15000 });
22
  assert.equal(readPositiveInt("500", 15000, "T").value, 500);
23
  assert.equal(readPositiveInt("12.7", 15000, "T").value, 12);
24
25
  for (const bad of ["abc", "0", "-5", "NaN"]) {
26
    const parsed = readPositiveInt(bad, 15000, "T");
27
    assert.equal(parsed.value, 15000, `${bad} should fall back`);
28
    assert.ok(parsed.warning, `${bad} should warn`);
29
  }
30
});
31
32
test("resolveNoneKey steps aside when the caller already uses the name", () => {
33
  assert.equal(resolveNoneKey([]), "none");
34
  assert.equal(resolveNoneKey(["a", "b"]), "none");
35
  assert.equal(resolveNoneKey(["none"]), "none_of_these");
36
  assert.equal(resolveNoneKey(["none", "none_of_these"]), "none_of_these_2");
37
});
38
39
test("buildChoiceCriteria never overwrites a caller's own option", () => {
40
  const callerMeaning = "The request was already handled; no action needed.";
41
  const { criteria, noneKey } = buildChoiceCriteria({ none: callerMeaning, escalate: "Send to a human" }, true);
42
43
  assert.equal(criteria.none, callerMeaning, "the caller's option must survive verbatim");
44
  assert.equal(noneKey, "none_of_these");
45
  assert.equal(criteria.none_of_these, "None of the other options fits.");
46
});
47
48
test("buildChoiceCriteria reports the no-match key it used", () => {
49
  const { criteria, noneKey } = buildChoiceCriteria({ a: "d", b: "d" }, true);
50
  assert.equal(noneKey, "none");
51
  assert.deepEqual(Object.keys(criteria).sort(), ["a", "b", "none"]);
52
53
  const off = buildChoiceCriteria({ a: "d", b: "d" }, false);
54
  assert.equal(off.noneKey, null);
55
  assert.deepEqual(Object.keys(off.criteria).sort(), ["a", "b"]);
56
});
57
58
test("buildChoiceCriteria rejects empty, single-outcome, and oversized option sets", () => {
59
  assert.throws(() => buildChoiceCriteria({}, true), /at least one option/);
60
  assert.throws(() => buildChoiceCriteria({ only: "d" }, false), /at least two outcomes/);
61
62
  const tooMany = Object.fromEntries(Array.from({ length: MAX_CHOICE_OPTIONS + 1 }, (_, i) => [`o${i}`, null]));
63
  assert.throws(() => buildChoiceCriteria(tooMany, false), /at most 255 options/);
64
65
  const exactlyAtLimit = Object.fromEntries(Array.from({ length: MAX_CHOICE_OPTIONS }, (_, i) => [`o${i}`, null]));
66
  assert.doesNotThrow(() => buildChoiceCriteria(exactlyAtLimit, false));
67
});
68
69
test("assertUniqueIds names the duplicates rather than dropping questions", () => {
70
  assert.doesNotThrow(() => assertUniqueIds(["a", "b", "c"]));
71
  assert.throws(() => assertUniqueIds(["a", "b", "a"]), /Duplicate question ids: "a"/);
72
});
73
74
test("state size is measured, and oversized state is rejected not truncated", () => {
75
  assert.equal(stateSize("abcde"), 5);
76
  assert.equal(stateSize({ a: 1 }), JSON.stringify({ a: 1 }).length);
77
78
  assert.doesNotThrow(() => assertStateWithinLimit("abc", 10));
79
  assert.throws(() => assertStateWithinLimit("a".repeat(20), 10), /above the 10 limit/);
80
});
81
82
test("gateConfidence maps confidence onto an action", () => {
83
  assert.equal(gateConfidence(0.95, 0.8, 0.5), "act");
84
  assert.equal(gateConfidence(0.8, 0.8, 0.5), "act");
85
  assert.equal(gateConfidence(0.6, 0.8, 0.5), "review");
86
  assert.equal(gateConfidence(0.2, 0.8, 0.5), "abstain");
87
  assert.equal(gateConfidence(null, 0.8, 0.5), "abstain");
88
  assert.equal(gateConfidence(undefined, 0.8, 0.5), "abstain");
89
});
90
91
test("gateProbability keeps the middle band uncertain instead of rounding", () => {
92
  assert.equal(gateProbability(0.9, 0.7, 0.3), "yes");
93
  assert.equal(gateProbability(0.7, 0.7, 0.3), "yes");
94
  assert.equal(gateProbability(0.51, 0.7, 0.3), "uncertain", "0.51 is a coin flip, not a yes");
95
  assert.equal(gateProbability(0.3, 0.7, 0.3), "no");
96
  assert.equal(gateProbability(0.05, 0.7, 0.3), "no");
97
});
98
99
test("describeError separates failures a caller should treat differently", () => {
100
  const missing = describeError(new Error("No API key. Set TYPESAFE_API_KEY in the environment."));
101
  assert.equal(missing.kind, "no_api_key");
102
  assert.equal(missing.retryable, false);
103
104
  const headers = new Headers({ "x-typesafe-request-id": "req_abc" });
105
  const rateLimited = describeError(APIError.fromResponse(429, {}, headers));
106
  assert.equal(rateLimited.kind, "rate_limit");
107
  assert.equal(rateLimited.retryable, true);
108
  assert.equal(rateLimited.status, 429);
109
  assert.equal(rateLimited.requestId, "req_abc");
110
111
  const unauthorized = describeError(APIError.fromResponse(401, {}, new Headers()));
112
  assert.equal(unauthorized.kind, "authentication");
113
  assert.equal(unauthorized.retryable, false, "a bad key never succeeds on retry");
114
115
  const malformed = describeError(APIError.fromResponse(422, {}, new Headers()));
116
  assert.equal(malformed.kind, "invalid_request");
117
  assert.equal(malformed.retryable, false);
118
119
  const overloaded = describeError(APIError.fromResponse(529, {}, new Headers()));
120
  assert.equal(overloaded.kind, "server_error");
121
  assert.equal(overloaded.retryable, true);
122
});
↑↓ navigate ↵ open esc close
↑↓ navigate ↵ open esc close

Keyboard shortcuts