Spaces:
Running
Running
File size: 5,425 Bytes
c95195c | 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 | 'use strict';
const assert = require('node:assert/strict');
const { spawnSync } = require('node:child_process');
const { mkdtempSync, readFileSync, rmSync, writeFileSync } = require('node:fs');
const { tmpdir } = require('node:os');
const path = require('node:path');
const { test } = require('node:test');
const root = path.join(__dirname, '..');
const models = path.join(root, 'space', 'models');
const cli = path.join(root, 'tacitus.cjs');
const request = JSON.stringify(JSON.parse(readFileSync(path.join(root, 'fixtures', 'request.json'), 'utf8')));
const limit = 64 * 1024;
function run(input, args = [], directory = models) {
const child = spawnSync(process.execPath, [cli, '--models', directory, ...args], {
encoding: 'utf8',
input,
timeout: 20000
});
if (child.error) throw child.error;
return child;
}
function parsedLines(output) {
return output.trim().split('\n').map(line => JSON.parse(line));
}
test('single requests accept exactly 64 KiB and reject one extra byte', () => {
const exact = request.padEnd(limit);
const valid = run(exact);
assert.equal(valid.status, 0, valid.stderr);
assert.equal(JSON.parse(valid.stdout).task, 'evidence');
const oversized = run(exact + ' ');
assert.equal(oversized.status, 1);
assert.match(oversized.stderr, /64 KiB/);
assert.equal(oversized.stdout, '');
});
test('JSONL skips blanks, handles CRLF and recovers after oversized and invalid UTF-8 rows', () => {
const oversized = Buffer.alloc(2 * 1024 * 1024, 120);
const invalid = Buffer.from([0xc3, 0x28]);
const batch = Buffer.concat([
Buffer.from(request.padEnd(limit) + '\r\n \r\n'),
oversized,
Buffer.from('\r'),
invalid,
Buffer.from('\n' + request)
]);
const result = run(batch, ['--jsonl']);
assert.equal(result.status, 2, result.stderr);
const [first, tooLarge, badEncoding, last] = parsedLines(result.stdout);
assert.deepEqual(tooLarge, { line: 3, error: 'Input exceeds 64 KiB per request.' });
assert.deepEqual(badEncoding, { line: 4, error: 'Input must be valid UTF-8.' });
assert.equal(first.task, 'evidence');
assert.deepEqual(last, first);
});
test('an oversized final JSONL row is reported once without a line ending', () => {
const response = run(request + '\n' + ' '.repeat(limit + 1), ['--jsonl']);
assert.equal(response.status, 2, response.stderr);
const outputs = parsedLines(response.stdout);
assert.equal(outputs.length, 2);
assert.deepEqual(outputs[1], { line: 2, error: 'Input exceeds 64 KiB per request.' });
});
test('file mode works with spaces and a UTF-8 BOM', t => {
const temporary = mkdtempSync(path.join(tmpdir(), 'tacitus-cli-'));
t.after(() => rmSync(temporary, { recursive: true, force: true }));
const file = path.join(temporary, 'input request.json');
writeFileSync(file, '\uFEFF' + request);
const result = run(undefined, ['--input', file]);
assert.equal(result.status, 0, result.stderr);
assert.equal(JSON.parse(result.stdout).task, 'evidence');
const missing = run(undefined, ['--input', path.join(temporary, 'missing.json')]);
assert.equal(missing.status, 1);
assert.match(missing.stderr, /Input file is missing/);
});
test('known triggers select the exact Unicode occurrence and reject unsupported constraints', () => {
const text = '🗳️ The council requested records. The union requested a meeting.';
const start = Array.from(text.slice(0, text.lastIndexOf('requested'))).length;
const trigger = { start, end: start + 9, event_type: 'Request' };
const valid = { task: 'events', text, include_participants: true, trigger };
const requests = [
valid,
{ ...valid, trigger: { ...trigger, start: start + 1 } },
{ ...valid, trigger: { ...trigger, event_type: 'Agreement' } },
{ ...valid, trigger: { ...trigger, confidence: 1 } },
{ ...valid, task: 'actors' }
];
const result = run(requests.map(item => JSON.stringify(item)).join('\n'), ['--jsonl']);
assert.equal(result.status, 2, result.stderr);
const [output, unaligned, unknownType, extra, wrongTask] = parsedLines(result.stdout);
assert.equal(output.trigger_source, 'provided');
assert.equal(output.events.length, 1);
assert.deepEqual(output.events[0].trigger, {
start, end: start + 9, type: 'Request', text: 'requested'
});
assert.equal(output.events[0].participant_status, 'scored');
for (const participant of output.events[0].participants) {
assert.equal(participant.text, Array.from(text).slice(participant.start, participant.end).join(''));
}
assert.match(unaligned.error, /token boundaries/);
assert.match(unknownType.error, /event type/);
assert.match(extra.error, /trigger must be an object/);
assert.match(wrongTask.error, /apply to Events/);
});
test('local weights reject a same-size mutation and identify missing model files', t => {
const temporary = mkdtempSync(path.join(tmpdir(), 'tacitus-weights-'));
t.after(() => rmSync(temporary, { recursive: true, force: true }));
const missing = run(request, [], temporary);
assert.equal(missing.status, 1);
assert.match(missing.stderr, /Model file is missing for evidence/);
const raw = readFileSync(path.join(models, 'evidence.json'));
raw[0] ^= 1;
writeFileSync(path.join(temporary, 'evidence.json'), raw);
const invalid = run(request, [], temporary);
assert.equal(invalid.status, 1);
assert.match(invalid.stderr, /Model integrity check failed for evidence/);
});
|