You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
3.2 KiB
61 lines
3.2 KiB
const mod = await import('node:module'); |
|
const require = mod.createRequire(import.meta.url); |
|
const parser = require('../content/parser.js'); |
|
|
|
const L = String.fromCharCode(10214); |
|
const R = String.fromCharCode(10215); |
|
const LF = String.fromCharCode(10); |
|
const Q = String.fromCharCode(34); |
|
const LB = String.fromCharCode(123); |
|
const RB = String.fromCharCode(125); |
|
const BS = String.fromCharCode(92); |
|
const LT = String.fromCharCode(60); |
|
|
|
let pass = 0; |
|
let fail = 0; |
|
const ok = (name, cond) => cond ? (pass++, console.log('OK ' + name)) : (fail++, console.log('FAIL ' + name)); |
|
|
|
// Test 1: DSH JSON |
|
const json1 = LB + Q + 'tool' + Q + ':' + Q + 'dsh_read' + Q + ',' + Q + 'args' + Q + ':' + LB + Q + 'path' + Q + ':' + Q + 'README.md' + Q + RB + RB; |
|
const t1 = L + 'DSH' + R + json1 + L + '/DSH' + R; |
|
const r1 = parser.extractCalls(t1, 3); |
|
ok('DSH count', r1.calls.length === 1); |
|
ok('DSH tool', !!r1.calls[0] ? r1.calls[0].tool === 'dsh_read' : false); |
|
ok('DSH path', !!r1.calls[0] ? r1.calls[0].args.path === 'README.md' : false); |
|
|
|
// Test 2: DSH_B64 |
|
const b64 = 'eyJ0b29sIjoiZHNoX3RyZWUiLCJhcmdzIjp7InBhdGgiOiIuIn19'; |
|
const t2 = L + 'DSH_B64' + R + b64 + L + '/DSH_B64' + R; |
|
const r2 = parser.extractCalls(t2, 3); |
|
ok('B64 count', r2.calls.length === 1); |
|
ok('B64 tool', !!r2.calls[0] ? r2.calls[0].tool === 'dsh_tree' : false); |
|
|
|
// Test 3: DSH_FILE |
|
const phpBody = LT + '?php' + LF + 'namespace app' + BS + 'models;' + LF; |
|
const t3 = L + 'DSH_FILE path=' + Q + 't.php' + Q + R + LF + phpBody + L + '/DSH_FILE' + R; |
|
const r3 = parser.extractCalls(t3, 3); |
|
ok('FILE count', r3.calls.length === 1); |
|
ok('FILE path', !!r3.calls[0] ? r3.calls[0].args.path === 't.php' : false); |
|
ok('FILE tool', !!r3.calls[0] ? r3.calls[0].tool === 'dsh_write' : false); |
|
ok('FILE content', !!r3.calls[0] ? r3.calls[0].args.content.indexOf('namespace app' + BS + 'models') !== -1 : false); |
|
|
|
// Test 4: batch format expands into multiple calls with policy |
|
const b1 = L + 'DSH' + R + LB + Q + 'batch' + Q + ':[' + LB + Q + 'tool' + Q + ':' + Q + 'dsh_read' + Q + ',' + Q + 'args' + Q + ':' + LB + Q + 'path' + Q + ':' + Q + 'a.txt' + Q + RB + RB + ',' + LB + Q + 'tool' + Q + ':' + Q + 'dsh_read' + Q + ',' + Q + 'args' + Q + ':' + LB + Q + 'path' + Q + ':' + Q + 'b.txt' + Q + RB + RB + '],' + Q + 'policy' + Q + ':' + LB + Q + 'on_error' + Q + ':' + Q + 'stop' + Q + RB + RB + L + '/DSH' + R; |
|
const r4 = parser.extractCalls(b1, 0); |
|
ok('batch count', r4.calls.length === 2); |
|
ok('batch policy', !!r4.policy ? r4.policy.on_error === 'stop' : false); |
|
ok('batch path1', !!r4.calls[0] ? r4.calls[0].args.path === 'a.txt' : false); |
|
ok('batch path2', !!r4.calls[1] ? r4.calls[1].args.path === 'b.txt' : false); |
|
|
|
// Test 5: maxCalls=0 means no limit |
|
let many = ''; |
|
for (let i = 0; i < 20; i++) { many += L + 'DSH' + R + LB + Q + 'tool' + Q + ':' + Q + 'dsh_read' + Q + ',' + Q + 'args' + Q + ':' + LB + Q + 'path' + Q + ':' + Q + 'f' + i + '.txt' + Q + RB + RB + L + '/DSH' + R; } |
|
const r5 = parser.extractCalls(many, 0); |
|
ok('no limit count', r5.calls.length === 20); |
|
|
|
// Test 6: maxCalls=5 still works (backward compat) |
|
const r6 = parser.extractCalls(many, 5); |
|
ok('limit 5 count', r6.calls.length === 5); |
|
|
|
console.log('passed ' + pass + ', failed ' + fail); |
|
if (fail > 0) process.exit(1);
|
|
|