import fs from 'fs'; import path from 'path'; import assert from 'assert'; import { createRequire } from 'module'; const require = createRequire(import.meta.url); require('../content/selectors.js'); const parser = require('../content/parser.js'); const OPEN = '
'; const CLOSE = '
'; function loadFixture(name) { const html = fs.readFileSync(path.join(process.cwd(), 'test/fixtures', name), 'utf8'); const a = html.indexOf(OPEN); const b = html.lastIndexOf(CLOSE); const inner = (a >= 0 && b > a) ? html.slice(a + OPEN.length, b) : ''; const text = inner.split('<').join('').split('>').join('').trim(); const node = { nodeType: 1, className: 'ds-markdown', childNodes: [{ nodeType: 3, nodeValue: text }] }; return { querySelectorAll: function (sel) { return sel === '.ds-markdown' ? [node] : []; } }; } const tests = []; function t(name, fn) { tests.push({ name: name, fn: fn }); } t('dsh count', function () { assert.equal(parser.parseChatText(loadFixture('reply_dsh.html')).calls.length, 1); }); t('dsh tool', function () { assert.equal(parser.parseChatText(loadFixture('reply_dsh.html')).calls[0].tool, 'dsh_read'); }); t('dsh path', function () { assert.equal(parser.parseChatText(loadFixture('reply_dsh.html')).calls[0].args.path, 'a.txt'); }); t('b64 count', function () { assert.equal(parser.parseChatText(loadFixture('reply_b64.html')).calls.length, 1); }); t('b64 tool', function () { assert.equal(parser.parseChatText(loadFixture('reply_b64.html')).calls[0].tool, 'dsh_read'); }); t('file path', function () { assert.equal(parser.parseChatText(loadFixture('reply_file.html')).calls[0].args.path, 't.txt'); }); t('file content', function () { assert.ok(parser.parseChatText(loadFixture('reply_file.html')).calls[0].args.content.indexOf('line one') >= 0); }); t('broken no throw', function () { const r = parser.parseChatText(loadFixture('reply_broken.html')); assert.equal(r.calls.length, 0); }); let pass = 0, fail = 0; for (const x of tests) { try { x.fn(); console.log('ok ' + x.name); pass++; } catch (e) { console.log('FAIL ' + x.name + ' ' + e.message); fail++; } } console.log(pass + '/' + (pass + fail)); process.exit(fail > 0 ? 1 : 0);