-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.ts
More file actions
69 lines (61 loc) · 2.15 KB
/
Copy pathtest.ts
File metadata and controls
69 lines (61 loc) · 2.15 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
import { createRuntime, Intent, Proposal } from "@yav-ai/clp";
import { classifierContract } from "./contract";
const ALLOWED_CATEGORIES = [
"tech",
"sports",
"business",
"entertainment",
"science",
];
async function runTests() {
console.log("=== Classifier Tests ===\n");
// Test 1: AI classification works
console.log("Test 1: AI classifies tech text");
const ai = async (_intent: Intent): Promise<Proposal> => ({
category: "tech",
});
const app1 = createRuntime(classifierContract, ai);
app1.dispatch("classifyText", { text: "AI is great" });
await app1.propose("classifyText");
app1.acceptProposal("classifyText");
app1.commit("applyClassification");
console.log("Result:", app1.getState("lastCategory"));
console.log("PASS\n");
// Test 2: Guard blocks invalid category
console.log("Test 2: Guard blocks invalid category");
const app2 = createRuntime(classifierContract);
app2.dispatch("classifyText", { text: "test", category: "invalid" });
try {
app2.commit("applyClassification");
console.log("FAIL: Should have thrown\n");
} catch (e) {
console.log("PASS: Blocked -", (e as Error).message, "\n");
}
// Test 3: Guard blocks empty text
console.log("Test 3: Guard blocks empty text");
const app3 = createRuntime(classifierContract);
app3.dispatch("classifyText", { text: "" });
try {
app3.commit("applyClassification");
console.log("FAIL: Should have thrown\n");
} catch (e) {
console.log("PASS: Blocked -", (e as Error).message, "\n");
}
// Test 4: AI can only propose allowed categories
console.log("Test 4: AI proposes category, user must accept");
const badAI = async (_intent: Intent): Promise<Proposal> => ({
category: "forbidden",
});
const app4 = createRuntime(classifierContract, badAI);
app4.dispatch("classifyText", { text: "hello" });
await app4.propose("classifyText");
app4.acceptProposal("classifyText");
try {
app4.commit("applyClassification");
console.log("FAIL: Should have thrown\n");
} catch (e) {
console.log("PASS: Guard blocked AI proposal:", (e as Error).message, "\n");
}
console.log("=== All Tests Complete ===");
}
runTests();