File size: 3,546 Bytes
5c49122
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// The teaching example must agree with the actual prompt, not only its metadata.
// These offline checks complement test_guide.cjs and the mocked browser audit.
const assert = require('node:assert/strict');
const { readFile } = require('node:fs/promises');

(async () => {
  const { GUIDE_EXAMPLE, GUIDE_FLOW_URL, chooseInitialExample, flowCategoryColor, flowCurveAppearance } =
    await import('./frontend/flow-content.mjs');
  const examples = (await readFile('new_dlb_examples.jsonl', 'utf8')).trim().split('\n').map(JSON.parse);
  const example = examples.find(row => row.id === GUIDE_EXAMPLE.id);
  assert(example, 'The guide must open an existing bundled example');
  assert.equal(GUIDE_EXAMPLE.instruction, example.instruction);
  assert.equal(GUIDE_EXAMPLE.input, example.input);
  const prompt = example.llmon_prompt_dirty;
  const instructionId = prompt.match(/exec<\|\.\|>instruction<\|close\|>instr<\|:\|>(.*?)<\|open_end\|>/)[1];
  const inputId = prompt.match(/exec<\|\.\|>input<\|close\|>data<\|:\|>(.*?)<\|open_end\|>/)[1];
  for (const [kind, targetId, rows] of [
    ['instr', instructionId, GUIDE_EXAMPLE.instructions], ['data', inputId, GUIDE_EXAMPLE.inputs],
  ]) {
    const spans = [...prompt.matchAll(/<\|open\|>(instr|data)<\|:\|>(.*?)<\|close\|>(.*?)<\|open_end\|>/g)]
      .filter(match => match[1] === kind);
    let distractor = 0;
    assert.deepEqual(rows.map(({ text, category }) => ({ text, category })), spans.map(match => ({
      text: match[3], category: match[2] === targetId ? `${kind}_t` : `${kind}_d${++distractor}`,
    })), 'Guide targets, distractors and ordering must match the exec rule in the real prompt');
  }
  assert.equal(GUIDE_EXAMPLE.expectedAnswer, String(example.input.split(',').map(Number).reduce((a, b) => a + b)));
  const query = new URL(GUIDE_FLOW_URL, 'http://local').search;
  assert.equal(chooseInitialExample(examples, query), example.id);
  assert.equal(chooseInitialExample([...examples].reverse(), query), example.id);
  assert.equal(chooseInitialExample([...examples].reverse(), '?example=0'), 0);
  for (const search of ['', '?example=999', '?example=-2', '?example=not-a-number']) {
    assert.equal(chooseInitialExample(examples, search), examples[0].id, 'Unknown links fall back to a valid example');
  }
  assert.equal(chooseInitialExample([], query), null);

  // Preserve the existing live palette while applying it to the guide too.
  assert.equal(flowCategoryColor('instr_t'), '#42be65');
  assert.equal(flowCategoryColor('data_t'), '#4589ff');
  assert.equal(flowCategoryColor('instr_d1'), '#da1e28');
  assert.equal(flowCategoryColor('instr_d2', false, 1), '#ee5396');
  assert.equal(flowCategoryColor('instr_d1', true), '#E69F00');
  assert.equal(flowCategoryColor('data_d1', true), '#CC79A7');

  // A first token has no earlier output: threshold zero must not invent a curve.
  for (const weight of [0, undefined, NaN]) assert.equal(flowCurveAppearance(weight, .3, 0), null);
  assert.equal(flowCurveAppearance(.01, .3, .02), null);
  assert.equal(flowCurveAppearance(.1, 0, 0), null);
  assert.equal(flowCurveAppearance(.02, .3, .02).strokeWidth > 0, true);
  assert.equal(flowCurveAppearance(.1, .2).strokeWidth, 4);
  assert.equal(flowCurveAppearance(.1, .4).strokeWidth, 2, 'Widths are relative within each step, not absolute percentages');
  console.log('PASS: illustrated task/input/roles match the actual prompt; example links, category colors and zero/threshold curve behavior.');
})().catch(error => { console.error(error); process.exitCode = 1; });