返回 CodeWhale
pet.test.mjs
根目录 / pet / tests / pet.test.mjs
1 import test from 'node:test';
2 import assert from 'node:assert/strict';
3 import { readFileSync } from 'node:fs';
4 import { PetSim, REST_STATE, digest, CHANNELS } from '../dist/core/pet-sim.js';
5 import { compilePetTelemetry, encodePetJSONL, decodePetJSONL } from '../dist/core/pet-telemetry.js';
6
7 const points = readFileSync(new URL('../public/whale-points.tsv', import.meta.url), 'utf8').trim().split('\n').map(row => row.trim().split(/\s+/).map(Number));
8 const event = (overrides = {}) => ({ schemaVersion: 1, id: 'e', traceId: 't', startTime: 0, endTime: 400,
9 agentId: 'parent', category: 'tool', name: 'work', status: 'success', attributes: {}, ...overrides });
10
11 test('span time beats onset count; long tools remain measured; containers are excluded', () => {
12 const e = [event({ id: 'root', category: 'orchestration', endTime: 4000 }),
13 event({ id: 'long', parentId: 'root', category: 'reasoning', endTime: 4000 }),
14 ...Array.from({ length: 8 }, (_, i) => event({ id: String(i), startTime: i * 10, endTime: i * 10 }))];
15 const b = compilePetTelemetry(e);
16 assert.equal(b[0].channel, 'reasoning'); assert.equal(b[9].channel, 'reasoning');
17 assert.equal(b[0].activeMs[9], 0); assert.equal(b[0].activeMs[0], 400);
18 });
19 test('half-open bins retain endpoint onsets and distinguish unknown duration from idle', () => {
20 const b = compilePetTelemetry([event({ endTime: 800 }), event({ id: 'last', startTime: 800, endTime: 800, category: 'human' })], 1600);
21 assert.equal(b[2].channel, 'human'); assert.equal(b[2].onsets[11], 1); assert.equal(b[3].observed, 0); assert.equal(b[3].channel, 'other');
22 const open = compilePetTelemetry([event({ openEnded: true, endTime: 3000 })], 1200);
23 assert.equal(open[0].onsets[1], 1); assert.equal(open[0].activeMs[1], 0); assert.equal(open[1].observed, 0);
24 });
25 test('repeat density is causal; independent future events do not rewrite past buckets', () => {
26 const first = [event({ endTime: 80 })], future = Array.from({ length: 7 }, (_, i) => event({ id: `f${i}`, startTime: 400 + i * 400, endTime: 480 + i * 400 }));
27 const a = compilePetTelemetry(first), b = compilePetTelemetry([...first, ...future]);
28 assert.deepEqual(a[0], b[0]); assert.ok(b[4].coherence < b[0].coherence);
29 });
30 test('errors, human spans and concurrent peers select semantic gaits', () => {
31 assert.equal(compilePetTelemetry([event({ category: 'reasoning', status: 'error' })])[0].channel, 'error');
32 const wait = compilePetTelemetry([event({ category: 'human', status: 'pending' })])[0];
33 assert.equal(wait.channel, 'human'); assert.equal(wait.waiting, true);
34 const peers = compilePetTelemetry(['a', 'b', 'c'].map(id => event({ id, agentId: id })))[0];
35 assert.equal(peers.channel, 'agent'); assert.deepEqual(peers.agentIds, ['a', 'b', 'c']);
36 });
37 test('updates deduplicate, multiple traces reject, wire rejects corrupt values', () => {
38 assert.equal(compilePetTelemetry([event(), event({ endTime: 800 })])[0].onsets[1], 1);
39 assert.throws(() => compilePetTelemetry([event(), event({ traceId: 'another' })]), /one trace/);
40 const b = compilePetTelemetry([event()]); assert.deepEqual(decodePetJSONL(encodePetJSONL(b)), b);
41 for (const edit of [{ activity: null }, { observed: 2 }, { sequence: 8 }, { channel: 'fake' }, { onsets: [1] }])
42 assert.throws(() => decodePetJSONL(JSON.stringify({ ...b[0], ...edit })));
43 assert.throws(() => compilePetTelemetry([event({ endTime: Infinity })]));
44 });
45 test('reduced motion has no settling, jitter, colour fade or tear decay under constant input', () => {
46 for (const ch of CHANNELS) {
47 const sim = new PetSim(points), state = { ...REST_STATE, channel: ch.key, activity: .8, coherence: .4 };
48 sim.step(1 / 30, state, { motion: false, sensitivity: 1 }); const first = digest(sim);
49 for (let i = 0; i < 90; i++) sim.step(1 / 30, state, { motion: false, sensitivity: 1 });
50 assert.equal(digest(sim), first, ch.key);
51 }
52 });
53
53 lines Plain Text