@artemiskit/sdk 0.3.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +229 -0
- package/README.md +44 -1
- package/dist/artemiskit.d.ts +149 -0
- package/dist/artemiskit.d.ts.map +1 -0
- package/dist/builders/index.d.ts +352 -0
- package/dist/builders/index.d.ts.map +1 -0
- package/dist/builders/index.js +354 -0
- package/dist/contracts/index.d.ts +253 -0
- package/dist/contracts/index.d.ts.map +1 -0
- package/dist/contracts/index.js +53 -0
- package/dist/guardian/action-validator.d.ts +92 -0
- package/dist/guardian/action-validator.d.ts.map +1 -0
- package/dist/guardian/circuit-breaker.d.ts +211 -0
- package/dist/guardian/circuit-breaker.d.ts.map +1 -0
- package/dist/guardian/guardian.d.ts +213 -0
- package/dist/guardian/guardian.d.ts.map +1 -0
- package/dist/guardian/guardrails.d.ts +107 -0
- package/dist/guardian/guardrails.d.ts.map +1 -0
- package/dist/guardian/index.d.ts +35 -0
- package/dist/guardian/index.d.ts.map +1 -0
- package/dist/guardian/intent-classifier.d.ts +81 -0
- package/dist/guardian/intent-classifier.d.ts.map +1 -0
- package/dist/guardian/interceptor.d.ts +141 -0
- package/dist/guardian/interceptor.d.ts.map +1 -0
- package/dist/guardian/policy.d.ts +52 -0
- package/dist/guardian/policy.d.ts.map +1 -0
- package/dist/guardian/semantic-validator.d.ts +61 -0
- package/dist/guardian/semantic-validator.d.ts.map +1 -0
- package/dist/guardian/types.d.ts +653 -0
- package/dist/guardian/types.d.ts.map +1 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +44918 -28644
- package/dist/matchers/core.d.ts +81 -0
- package/dist/matchers/core.d.ts.map +1 -0
- package/dist/matchers/index.d.ts +6 -0
- package/dist/matchers/index.d.ts.map +1 -0
- package/dist/matchers/index.js +13 -59
- package/dist/matchers/jest.d.ts +46 -0
- package/dist/matchers/jest.d.ts.map +1 -0
- package/dist/matchers/jest.js +0 -46
- package/dist/matchers/vitest.d.ts +24 -0
- package/dist/matchers/vitest.d.ts.map +1 -0
- package/dist/matchers/vitest.js +0 -46
- package/dist/types-only.d.ts +31 -0
- package/dist/types-only.d.ts.map +1 -0
- package/dist/types-only.js +1 -0
- package/dist/types.d.ts +379 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/utils/index.d.ts +112 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +58 -0
- package/package.json +19 -4
- package/src/__tests__/builders.test.ts +450 -0
- package/src/__tests__/circuit-breaker.test.ts +704 -0
- package/src/__tests__/compare.test.ts +469 -0
- package/src/__tests__/contracts.test.ts +354 -0
- package/src/__tests__/guardian-modes.test.ts +215 -0
- package/src/__tests__/guardrails.test.ts +404 -0
- package/src/__tests__/semantic-validator.test.ts +421 -0
- package/src/__tests__/utils.test.ts +324 -0
- package/src/__tests__/validate.test.ts +310 -0
- package/src/artemiskit.ts +479 -5
- package/src/builders/index.ts +700 -0
- package/src/contracts/index.ts +357 -0
- package/src/guardian/circuit-breaker.ts +74 -28
- package/src/guardian/guardian.ts +307 -20
- package/src/guardian/guardrails.ts +143 -1
- package/src/guardian/index.ts +35 -1
- package/src/guardian/interceptor.ts +87 -11
- package/src/guardian/semantic-validator.ts +399 -0
- package/src/guardian/types.ts +413 -2
- package/src/index.ts +258 -14
- package/src/types-only.ts +276 -0
- package/src/types.ts +186 -0
- package/src/utils/index.ts +225 -0
- package/tsconfig.json +2 -1
- package/adapters/openai/dist/index.js +0 -5625
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for SDK contract types and define* functions
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { describe, expect, it } from 'bun:test';
|
|
6
|
+
import type { GenerateResult, ModelCapabilities } from '@artemiskit/core';
|
|
7
|
+
import { defineAdapter, defineEvaluator, definePlugin, defineStorage } from '../contracts';
|
|
8
|
+
|
|
9
|
+
describe('Contracts - defineAdapter', () => {
|
|
10
|
+
it('should validate and return a valid adapter', () => {
|
|
11
|
+
const adapter = defineAdapter({
|
|
12
|
+
provider: 'test-provider',
|
|
13
|
+
async generate(_options) {
|
|
14
|
+
return {
|
|
15
|
+
id: 'test-id',
|
|
16
|
+
model: 'test-model',
|
|
17
|
+
text: 'test response',
|
|
18
|
+
tokens: { prompt: 10, completion: 20, total: 30 },
|
|
19
|
+
latencyMs: 100,
|
|
20
|
+
} as GenerateResult;
|
|
21
|
+
},
|
|
22
|
+
async capabilities() {
|
|
23
|
+
return {
|
|
24
|
+
streaming: false,
|
|
25
|
+
functionCalling: false,
|
|
26
|
+
toolUse: false,
|
|
27
|
+
maxContext: 4096,
|
|
28
|
+
} as ModelCapabilities;
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
expect(adapter.provider).toBe('test-provider');
|
|
33
|
+
expect(typeof adapter.generate).toBe('function');
|
|
34
|
+
expect(typeof adapter.capabilities).toBe('function');
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('should throw if provider is missing', () => {
|
|
38
|
+
expect(() =>
|
|
39
|
+
defineAdapter({
|
|
40
|
+
provider: '',
|
|
41
|
+
async generate() {
|
|
42
|
+
return {} as GenerateResult;
|
|
43
|
+
},
|
|
44
|
+
async capabilities() {
|
|
45
|
+
return {} as ModelCapabilities;
|
|
46
|
+
},
|
|
47
|
+
})
|
|
48
|
+
).toThrow('Adapter must have a provider property');
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('should throw if generate is not a function', () => {
|
|
52
|
+
expect(() =>
|
|
53
|
+
defineAdapter({
|
|
54
|
+
provider: 'test',
|
|
55
|
+
generate: 'not a function' as unknown as () => Promise<GenerateResult>,
|
|
56
|
+
async capabilities() {
|
|
57
|
+
return {} as ModelCapabilities;
|
|
58
|
+
},
|
|
59
|
+
})
|
|
60
|
+
).toThrow('Adapter must have a generate method');
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('should throw if capabilities is not a function', () => {
|
|
64
|
+
expect(() =>
|
|
65
|
+
defineAdapter({
|
|
66
|
+
provider: 'test',
|
|
67
|
+
async generate() {
|
|
68
|
+
return {} as GenerateResult;
|
|
69
|
+
},
|
|
70
|
+
capabilities: 'not a function' as unknown as () => Promise<ModelCapabilities>,
|
|
71
|
+
})
|
|
72
|
+
).toThrow('Adapter must have a capabilities method');
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('should accept optional methods', () => {
|
|
76
|
+
const adapter = defineAdapter({
|
|
77
|
+
provider: 'full-adapter',
|
|
78
|
+
async generate() {
|
|
79
|
+
return {} as GenerateResult;
|
|
80
|
+
},
|
|
81
|
+
async capabilities() {
|
|
82
|
+
return {} as ModelCapabilities;
|
|
83
|
+
},
|
|
84
|
+
async *stream(_options, _onChunk) {
|
|
85
|
+
yield 'chunk1';
|
|
86
|
+
yield 'chunk2';
|
|
87
|
+
},
|
|
88
|
+
async embed(_text) {
|
|
89
|
+
return [0.1, 0.2, 0.3];
|
|
90
|
+
},
|
|
91
|
+
async close() {
|
|
92
|
+
// Cleanup
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
expect(adapter.stream).toBeDefined();
|
|
97
|
+
expect(adapter.embed).toBeDefined();
|
|
98
|
+
expect(adapter.close).toBeDefined();
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
describe('Contracts - defineEvaluator', () => {
|
|
103
|
+
it('should validate and return a valid evaluator', () => {
|
|
104
|
+
const evaluator = defineEvaluator({
|
|
105
|
+
type: 'custom-evaluator',
|
|
106
|
+
async evaluate(_response, _expected, _context) {
|
|
107
|
+
return {
|
|
108
|
+
passed: true,
|
|
109
|
+
score: 1.0,
|
|
110
|
+
reason: 'Test passed',
|
|
111
|
+
};
|
|
112
|
+
},
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
expect(evaluator.type).toBe('custom-evaluator');
|
|
116
|
+
expect(typeof evaluator.evaluate).toBe('function');
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it('should throw if type is missing', () => {
|
|
120
|
+
expect(() =>
|
|
121
|
+
defineEvaluator({
|
|
122
|
+
type: '',
|
|
123
|
+
async evaluate() {
|
|
124
|
+
return { passed: true, score: 1 };
|
|
125
|
+
},
|
|
126
|
+
})
|
|
127
|
+
).toThrow('Evaluator must have a type property');
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it('should throw if evaluate is not a function', () => {
|
|
131
|
+
expect(() =>
|
|
132
|
+
defineEvaluator({
|
|
133
|
+
type: 'test',
|
|
134
|
+
evaluate: 'not a function' as unknown as () => Promise<{ passed: boolean; score: number }>,
|
|
135
|
+
})
|
|
136
|
+
).toThrow('Evaluator must have an evaluate method');
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it('should work with details in result', async () => {
|
|
140
|
+
const evaluator = defineEvaluator({
|
|
141
|
+
type: 'detailed-evaluator',
|
|
142
|
+
async evaluate(response) {
|
|
143
|
+
const wordCount = response.split(/\s+/).length;
|
|
144
|
+
return {
|
|
145
|
+
passed: wordCount > 5,
|
|
146
|
+
score: Math.min(wordCount / 10, 1),
|
|
147
|
+
reason: `Word count: ${wordCount}`,
|
|
148
|
+
details: { wordCount, threshold: 5 },
|
|
149
|
+
};
|
|
150
|
+
},
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
const result = await evaluator.evaluate('This is a test response with many words', {} as any);
|
|
154
|
+
expect(result.passed).toBe(true);
|
|
155
|
+
expect(result.details).toBeDefined();
|
|
156
|
+
expect(result.details?.wordCount).toBe(8); // "This is a test response with many words" = 8 words
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
describe('Contracts - defineStorage', () => {
|
|
161
|
+
it('should validate and return a valid storage adapter', () => {
|
|
162
|
+
const storage = defineStorage({
|
|
163
|
+
async save(manifest) {
|
|
164
|
+
return manifest.run_id;
|
|
165
|
+
},
|
|
166
|
+
async load(_runId) {
|
|
167
|
+
return {} as any;
|
|
168
|
+
},
|
|
169
|
+
async list(_options) {
|
|
170
|
+
return [];
|
|
171
|
+
},
|
|
172
|
+
async delete(_runId) {
|
|
173
|
+
// Delete implementation
|
|
174
|
+
},
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
expect(typeof storage.save).toBe('function');
|
|
178
|
+
expect(typeof storage.load).toBe('function');
|
|
179
|
+
expect(typeof storage.list).toBe('function');
|
|
180
|
+
expect(typeof storage.delete).toBe('function');
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it('should throw if save is missing', () => {
|
|
184
|
+
expect(() =>
|
|
185
|
+
defineStorage({
|
|
186
|
+
save: 'not a function' as any,
|
|
187
|
+
async load() {
|
|
188
|
+
return {} as any;
|
|
189
|
+
},
|
|
190
|
+
async list() {
|
|
191
|
+
return [];
|
|
192
|
+
},
|
|
193
|
+
async delete() {},
|
|
194
|
+
})
|
|
195
|
+
).toThrow('Storage must have a save method');
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
it('should throw if load is missing', () => {
|
|
199
|
+
expect(() =>
|
|
200
|
+
defineStorage({
|
|
201
|
+
async save() {
|
|
202
|
+
return 'id';
|
|
203
|
+
},
|
|
204
|
+
load: 'not a function' as any,
|
|
205
|
+
async list() {
|
|
206
|
+
return [];
|
|
207
|
+
},
|
|
208
|
+
async delete() {},
|
|
209
|
+
})
|
|
210
|
+
).toThrow('Storage must have a load method');
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
it('should throw if list is missing', () => {
|
|
214
|
+
expect(() =>
|
|
215
|
+
defineStorage({
|
|
216
|
+
async save() {
|
|
217
|
+
return 'id';
|
|
218
|
+
},
|
|
219
|
+
async load() {
|
|
220
|
+
return {} as any;
|
|
221
|
+
},
|
|
222
|
+
list: 'not a function' as any,
|
|
223
|
+
async delete() {},
|
|
224
|
+
})
|
|
225
|
+
).toThrow('Storage must have a list method');
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it('should throw if delete is missing', () => {
|
|
229
|
+
expect(() =>
|
|
230
|
+
defineStorage({
|
|
231
|
+
async save() {
|
|
232
|
+
return 'id';
|
|
233
|
+
},
|
|
234
|
+
async load() {
|
|
235
|
+
return {} as any;
|
|
236
|
+
},
|
|
237
|
+
async list() {
|
|
238
|
+
return [];
|
|
239
|
+
},
|
|
240
|
+
delete: 'not a function' as any,
|
|
241
|
+
})
|
|
242
|
+
).toThrow('Storage must have a delete method');
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
it('should accept optional compare method', () => {
|
|
246
|
+
const storage = defineStorage({
|
|
247
|
+
async save(manifest) {
|
|
248
|
+
return manifest.run_id;
|
|
249
|
+
},
|
|
250
|
+
async load() {
|
|
251
|
+
return {} as any;
|
|
252
|
+
},
|
|
253
|
+
async list() {
|
|
254
|
+
return [];
|
|
255
|
+
},
|
|
256
|
+
async delete() {},
|
|
257
|
+
async compare(_baselineId, _currentId) {
|
|
258
|
+
return {} as any;
|
|
259
|
+
},
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
expect(storage.compare).toBeDefined();
|
|
263
|
+
});
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
describe('Contracts - definePlugin', () => {
|
|
267
|
+
it('should validate and return a valid plugin', () => {
|
|
268
|
+
const plugin = definePlugin({
|
|
269
|
+
name: 'my-plugin',
|
|
270
|
+
version: '1.0.0',
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
expect(plugin.name).toBe('my-plugin');
|
|
274
|
+
expect(plugin.version).toBe('1.0.0');
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
it('should throw if name is missing', () => {
|
|
278
|
+
expect(() =>
|
|
279
|
+
definePlugin({
|
|
280
|
+
name: '',
|
|
281
|
+
version: '1.0.0',
|
|
282
|
+
})
|
|
283
|
+
).toThrow('Plugin must have a name');
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
it('should throw if version is missing', () => {
|
|
287
|
+
expect(() =>
|
|
288
|
+
definePlugin({
|
|
289
|
+
name: 'test-plugin',
|
|
290
|
+
version: '',
|
|
291
|
+
})
|
|
292
|
+
).toThrow('Plugin must have a version');
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
it('should accept optional lifecycle methods', () => {
|
|
296
|
+
const plugin = definePlugin({
|
|
297
|
+
name: 'full-plugin',
|
|
298
|
+
version: '1.0.0',
|
|
299
|
+
async init() {
|
|
300
|
+
// Initialize
|
|
301
|
+
},
|
|
302
|
+
async cleanup() {
|
|
303
|
+
// Cleanup
|
|
304
|
+
},
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
expect(plugin.init).toBeDefined();
|
|
308
|
+
expect(plugin.cleanup).toBeDefined();
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
it('should accept adapters, evaluators, and storage factories', () => {
|
|
312
|
+
const plugin = definePlugin({
|
|
313
|
+
name: 'extended-plugin',
|
|
314
|
+
version: '1.0.0',
|
|
315
|
+
adapters: {
|
|
316
|
+
custom: async (_config) => ({
|
|
317
|
+
provider: 'custom',
|
|
318
|
+
async generate() {
|
|
319
|
+
return {} as GenerateResult;
|
|
320
|
+
},
|
|
321
|
+
async capabilities() {
|
|
322
|
+
return {} as ModelCapabilities;
|
|
323
|
+
},
|
|
324
|
+
}),
|
|
325
|
+
},
|
|
326
|
+
evaluators: {
|
|
327
|
+
custom: (_config) => ({
|
|
328
|
+
type: 'custom',
|
|
329
|
+
async evaluate() {
|
|
330
|
+
return { passed: true, score: 1 };
|
|
331
|
+
},
|
|
332
|
+
}),
|
|
333
|
+
},
|
|
334
|
+
storage: {
|
|
335
|
+
custom: async (_config) => ({
|
|
336
|
+
async save() {
|
|
337
|
+
return 'id';
|
|
338
|
+
},
|
|
339
|
+
async load() {
|
|
340
|
+
return {} as any;
|
|
341
|
+
},
|
|
342
|
+
async list() {
|
|
343
|
+
return [];
|
|
344
|
+
},
|
|
345
|
+
async delete() {},
|
|
346
|
+
}),
|
|
347
|
+
},
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
expect(plugin.adapters).toBeDefined();
|
|
351
|
+
expect(plugin.evaluators).toBeDefined();
|
|
352
|
+
expect(plugin.storage).toBeDefined();
|
|
353
|
+
});
|
|
354
|
+
});
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for Guardian mode normalization (v0.3.2+)
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { describe, expect, mock, test } from 'bun:test';
|
|
6
|
+
import { Guardian, createGuardian, normalizeGuardianMode } from '../guardian';
|
|
7
|
+
|
|
8
|
+
describe('Guardian Mode Normalization', () => {
|
|
9
|
+
describe('normalizeGuardianMode', () => {
|
|
10
|
+
test('should return canonical modes unchanged', () => {
|
|
11
|
+
expect(normalizeGuardianMode('observe')).toBe('observe');
|
|
12
|
+
expect(normalizeGuardianMode('selective')).toBe('selective');
|
|
13
|
+
expect(normalizeGuardianMode('strict')).toBe('strict');
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
test('should map legacy modes to canonical modes', () => {
|
|
17
|
+
// Capture console.warn calls
|
|
18
|
+
const warnCalls: string[] = [];
|
|
19
|
+
const originalWarn = console.warn;
|
|
20
|
+
console.warn = (...args: unknown[]) => {
|
|
21
|
+
warnCalls.push(String(args[0]));
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
try {
|
|
25
|
+
expect(normalizeGuardianMode('testing')).toBe('observe');
|
|
26
|
+
expect(normalizeGuardianMode('guardian')).toBe('strict');
|
|
27
|
+
expect(normalizeGuardianMode('hybrid')).toBe('selective');
|
|
28
|
+
|
|
29
|
+
// Verify deprecation warnings were emitted
|
|
30
|
+
expect(warnCalls.length).toBe(3);
|
|
31
|
+
expect(warnCalls[0]).toContain("Mode 'testing' is deprecated");
|
|
32
|
+
expect(warnCalls[0]).toContain("Use 'observe' instead");
|
|
33
|
+
expect(warnCalls[1]).toContain("Mode 'guardian' is deprecated");
|
|
34
|
+
expect(warnCalls[1]).toContain("Use 'strict' instead");
|
|
35
|
+
expect(warnCalls[2]).toContain("Mode 'hybrid' is deprecated");
|
|
36
|
+
expect(warnCalls[2]).toContain("Use 'selective' instead");
|
|
37
|
+
} finally {
|
|
38
|
+
console.warn = originalWarn;
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
describe('Guardian constructor mode handling', () => {
|
|
44
|
+
test('should default to strict mode', () => {
|
|
45
|
+
const guardian = createGuardian();
|
|
46
|
+
expect(guardian.getMode()).toBe('strict');
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test('should accept canonical modes', () => {
|
|
50
|
+
const observeGuardian = createGuardian({ mode: 'observe' });
|
|
51
|
+
expect(observeGuardian.getMode()).toBe('observe');
|
|
52
|
+
|
|
53
|
+
const selectiveGuardian = createGuardian({ mode: 'selective' });
|
|
54
|
+
expect(selectiveGuardian.getMode()).toBe('selective');
|
|
55
|
+
|
|
56
|
+
const strictGuardian = createGuardian({ mode: 'strict' });
|
|
57
|
+
expect(strictGuardian.getMode()).toBe('strict');
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test('should normalize legacy modes with deprecation warning', () => {
|
|
61
|
+
const warnCalls: string[] = [];
|
|
62
|
+
const originalWarn = console.warn;
|
|
63
|
+
console.warn = (...args: unknown[]) => {
|
|
64
|
+
warnCalls.push(String(args[0]));
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
try {
|
|
68
|
+
const testingGuardian = createGuardian({ mode: 'testing' });
|
|
69
|
+
expect(testingGuardian.getMode()).toBe('observe');
|
|
70
|
+
|
|
71
|
+
const guardianModeGuardian = createGuardian({ mode: 'guardian' });
|
|
72
|
+
expect(guardianModeGuardian.getMode()).toBe('strict');
|
|
73
|
+
|
|
74
|
+
const hybridGuardian = createGuardian({ mode: 'hybrid' });
|
|
75
|
+
expect(hybridGuardian.getMode()).toBe('selective');
|
|
76
|
+
|
|
77
|
+
// Verify mode deprecation warnings were logged (filter out semantic validation warnings)
|
|
78
|
+
const modeDeprecationWarnings = warnCalls.filter((w) => w.includes('deprecated'));
|
|
79
|
+
expect(modeDeprecationWarnings.length).toBe(3);
|
|
80
|
+
} finally {
|
|
81
|
+
console.warn = originalWarn;
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
describe('shouldBlock based on mode', () => {
|
|
87
|
+
test('observe mode should never block', () => {
|
|
88
|
+
const guardian = createGuardian({ mode: 'observe' });
|
|
89
|
+
|
|
90
|
+
const lowViolation = {
|
|
91
|
+
id: '1',
|
|
92
|
+
type: 'injection_detection' as const,
|
|
93
|
+
severity: 'low' as const,
|
|
94
|
+
message: 'Test',
|
|
95
|
+
timestamp: new Date(),
|
|
96
|
+
action: 'block' as const,
|
|
97
|
+
blocked: true,
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const criticalViolation = {
|
|
101
|
+
id: '2',
|
|
102
|
+
type: 'injection_detection' as const,
|
|
103
|
+
severity: 'critical' as const,
|
|
104
|
+
message: 'Test',
|
|
105
|
+
timestamp: new Date(),
|
|
106
|
+
action: 'block' as const,
|
|
107
|
+
blocked: true,
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
expect(guardian.shouldBlock(lowViolation)).toBe(false);
|
|
111
|
+
expect(guardian.shouldBlock(criticalViolation)).toBe(false);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
test('selective mode should block only high/critical severity', () => {
|
|
115
|
+
const guardian = createGuardian({ mode: 'selective' });
|
|
116
|
+
|
|
117
|
+
const lowViolation = {
|
|
118
|
+
id: '1',
|
|
119
|
+
type: 'injection_detection' as const,
|
|
120
|
+
severity: 'low' as const,
|
|
121
|
+
message: 'Test',
|
|
122
|
+
timestamp: new Date(),
|
|
123
|
+
action: 'block' as const,
|
|
124
|
+
blocked: true,
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
const mediumViolation = {
|
|
128
|
+
id: '2',
|
|
129
|
+
type: 'injection_detection' as const,
|
|
130
|
+
severity: 'medium' as const,
|
|
131
|
+
message: 'Test',
|
|
132
|
+
timestamp: new Date(),
|
|
133
|
+
action: 'block' as const,
|
|
134
|
+
blocked: true,
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
const highViolation = {
|
|
138
|
+
id: '3',
|
|
139
|
+
type: 'injection_detection' as const,
|
|
140
|
+
severity: 'high' as const,
|
|
141
|
+
message: 'Test',
|
|
142
|
+
timestamp: new Date(),
|
|
143
|
+
action: 'block' as const,
|
|
144
|
+
blocked: true,
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
const criticalViolation = {
|
|
148
|
+
id: '4',
|
|
149
|
+
type: 'injection_detection' as const,
|
|
150
|
+
severity: 'critical' as const,
|
|
151
|
+
message: 'Test',
|
|
152
|
+
timestamp: new Date(),
|
|
153
|
+
action: 'block' as const,
|
|
154
|
+
blocked: true,
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
expect(guardian.shouldBlock(lowViolation)).toBe(false);
|
|
158
|
+
expect(guardian.shouldBlock(mediumViolation)).toBe(false);
|
|
159
|
+
expect(guardian.shouldBlock(highViolation)).toBe(true);
|
|
160
|
+
expect(guardian.shouldBlock(criticalViolation)).toBe(true);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
test('strict mode should always block', () => {
|
|
164
|
+
const guardian = createGuardian({ mode: 'strict' });
|
|
165
|
+
|
|
166
|
+
const lowViolation = {
|
|
167
|
+
id: '1',
|
|
168
|
+
type: 'injection_detection' as const,
|
|
169
|
+
severity: 'low' as const,
|
|
170
|
+
message: 'Test',
|
|
171
|
+
timestamp: new Date(),
|
|
172
|
+
action: 'block' as const,
|
|
173
|
+
blocked: true,
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
const criticalViolation = {
|
|
177
|
+
id: '2',
|
|
178
|
+
type: 'injection_detection' as const,
|
|
179
|
+
severity: 'critical' as const,
|
|
180
|
+
message: 'Test',
|
|
181
|
+
timestamp: new Date(),
|
|
182
|
+
action: 'block' as const,
|
|
183
|
+
blocked: true,
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
expect(guardian.shouldBlock(lowViolation)).toBe(true);
|
|
187
|
+
expect(guardian.shouldBlock(criticalViolation)).toBe(true);
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
describe('getContentValidationConfig', () => {
|
|
192
|
+
test('should return default config when not specified', () => {
|
|
193
|
+
const guardian = createGuardian();
|
|
194
|
+
const config = guardian.getContentValidationConfig();
|
|
195
|
+
|
|
196
|
+
expect(config.strategy).toBe('semantic');
|
|
197
|
+
expect(config.semanticThreshold).toBe(0.9);
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
test('should return custom config when specified', () => {
|
|
201
|
+
const guardian = createGuardian({
|
|
202
|
+
contentValidation: {
|
|
203
|
+
strategy: 'pattern',
|
|
204
|
+
semanticThreshold: 0.8,
|
|
205
|
+
categories: ['prompt_injection'],
|
|
206
|
+
},
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
const config = guardian.getContentValidationConfig();
|
|
210
|
+
expect(config.strategy).toBe('pattern');
|
|
211
|
+
expect(config.semanticThreshold).toBe(0.8);
|
|
212
|
+
expect(config.categories).toContain('prompt_injection');
|
|
213
|
+
});
|
|
214
|
+
});
|
|
215
|
+
});
|