okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-029-urgent (bc694233b)99866a47a

main
2.4 KiB43 linesraw
1// QA report F01: reasoning wrappers and stray tag fragments never show in an answer or its Markdown export.
2import test from 'node:test';
3import assert from 'node:assert/strict';
4import { readFile } from 'node:fs/promises';
5import { transform } from 'esbuild';
6async function loadTs(path) {
7  const { code } = await transform(await readFile(new URL(path, import.meta.url), 'utf8'), { loader: 'ts', format: 'esm' });
8  return import(`data:text/javascript;base64,${Buffer.from(code).toString('base64')}`);
9}
10const { tidyAnswer } = await loadTs('../src/lib/answer-text.ts');
11const { appendText } = await loadTs('../src/lib/deliverables.ts').catch(() => ({}));
12
13test('a literal <analysis> or <think> wrapper is removed, the words stay', () => {
14  assert.equal(tidyAnswer('<analysis>The answer is 4.'), 'The answer is 4.');
15  assert.equal(tidyAnswer('Sure.\n<think>\nplan\n</think>\nIt is 4.'), 'Sure.\n\nplan\n\nIt is 4.');
16  assert.equal(tidyAnswer('x </ANALYSIS> y <Thinking style="a"> z'), 'x  y  z');
17});
18
19test('a tag that lost its "<" at the very start is not shown as a prefix', () => {
20  assert.equal(tidyAnswer('p> CEDAR-742'), 'CEDAR-742');
21  assert.equal(tidyAnswer('  /p> done'), 'done');
22  assert.equal(tidyAnswer('analysis> 4'), '4');
23});
24
25test('ordinary text, code and comparisons are untouched', () => {
26  for (const text of ['CEDAR-742', 'if a > b then c', 'x -> y', '```html\n<p>hi</p>\n```', 'The p> in the middle', '<div>kept</div>', 'a <b and c> d', 'Result: 5 > 3', '']) assert.equal(tidyAnswer(text), text);
27});
28
29test('streaming: a tag that arrives in pieces is removed once it is complete', () => {
30  let content = '';
31  for (const piece of ['<', 'ana', 'lysis', '>', 'The code is ', 'p', '> no']) content = tidyAnswer(content + piece);
32  assert.equal(content, 'The code is p> no');
33  let lead = '';
34  for (const piece of ['p', '>', ' CEDAR', '-742']) lead = tidyAnswer(lead + piece);
35  assert.equal(lead, 'CEDAR-742');
36});
37
38test('the store applies it to the answer, the ordered pieces and the Markdown export', async () => {
39  const store = await readFile(new URL('../src/lib/store.ts', import.meta.url), 'utf8');
40  assert.match(store, /tidyAnswer\(m\.content\+text\)/);   // applyDelta, which the queued pieces go through
41  assert.match(store, /m\.role === 'assistant' \? tidyAnswer\(m\.content\) : m\.content\)/);
42  assert.match(await readFile(new URL('../src/lib/deliverables.ts', import.meta.url), 'utf8'), /tidyAnswer\(last\.text \+ text\)/);
43});