File size: 4,246 Bytes
4b84f18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
'use strict';
const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const vm = require('node:vm');
const modulePath = path.join(__dirname, '../static/protocols.js');

test('protocol study module is available for the workbook', () => {
  assert.ok(fs.existsSync(modulePath), 'Expected the local protocol study module');
});

test('known topics provide study questions and primary references without accepting arbitrary IDs', () => {
  const protocols = require(modulePath);
  assert.deepEqual(protocols.topics.map(topic => topic.id), ['http', 'tls', 'dns', 'ssh']);
  for (const id of ['http', 'tls', 'dns', 'ssh']) {
    const topic = protocols.getTopic(id);
    assert.equal(topic.id, id);
    assert.equal(topic.questions.length, 3);
    assert.ok(topic.title && topic.summary && topic.boundary && topic.explorerLesson);
    assert.ok(topic.sources.every(source => source.label && new URL(source.href).hostname === 'www.rfc-editor.org'));
  }
  for (const id of ['unknown', '__proto__', 'constructor', null, undefined]) {
    assert.equal(protocols.getTopic(id), null);
    assert.throws(() => protocols.draftExpected('', id), /choose|unknown|select/i);
  }
  assert.equal(protocols.explorerUrl, 'https://protocol-explorer-alpha.vercel.app/');
});

test('draft appends study context while preserving every existing character', () => {
  const protocols = require(modulePath);
  const existing = '  Existing expected controls.\r\nLeave this wording.  ';
  const drafted = protocols.draftExpected(existing, 'tls');
  assert.ok(drafted.startsWith(existing + '\n\n'));
  assert.match(drafted.slice(existing.length), /study context/i);
  assert.match(drafted.slice(existing.length), /not observations, evidence, or conclusions/i);
  assert.ok(drafted.includes(protocols.getTopic('tls').questions[0]));
  assert.equal(existing, '  Existing expected controls.\r\nLeave this wording.  ');
});

test('repeating the same topic does not duplicate or alter the draft', () => {
  const protocols = require(modulePath);
  const once = protocols.draftExpected('Expected behavior', 'http');
  assert.equal(protocols.draftExpected(once, 'http'), once);
  const both = protocols.draftExpected(once, 'ssh');
  assert.ok(both.startsWith(once + '\n\n'));
  assert.ok(both.includes(protocols.getTopic('ssh').questions[0]));
});

test('draft retains primary reference labels and URLs for exported study context', () => {
  const protocols = require(modulePath);
  const drafted = protocols.draftExpected('Existing controls', 'dns');
  assert.ok(drafted.includes('RFC 1035 — DNS implementation and specification'));
  assert.ok(drafted.includes('https://www.rfc-editor.org/rfc/rfc1035.html'));
  assert.ok(drafted.includes('RFC 4033 — DNS security introduction and requirements'));
  assert.ok(drafted.includes('https://www.rfc-editor.org/rfc/rfc4033.html'));
});

test('draft permits exactly 2000 characters and rejects overflow without truncation', () => {
  const protocols = require(modulePath);
  const emptyDraft = protocols.draftExpected('', 'dns');
  const prefixLength = 2000 - emptyDraft.length - 2;
  assert.ok(prefixLength > 0);
  const prefix = 'x'.repeat(prefixLength);
  const drafted = protocols.draftExpected(prefix, 'dns');
  assert.equal(drafted.length, 2000);
  assert.ok(drafted.startsWith(prefix + '\n\n'));
  assert.equal(protocols.draftExpected(drafted, 'dns'), drafted);
  assert.throws(() => protocols.draftExpected(prefix + 'x', 'dns'), /2000/);
  assert.throws(() => protocols.draftExpected(drafted + 'x', 'dns'), /2000/);
});

test('draft rejects non-text values instead of coercing or discarding them', () => {
  const protocols = require(modulePath);
  for (const value of [null, undefined, {}, 123]) {
    assert.throws(() => protocols.draftExpected(value, 'http'), /text|string/i);
  }
});

test('browser global supports drafting without storage or network capabilities', () => {
  const context = vm.createContext({});
  vm.runInContext(fs.readFileSync(modulePath, 'utf8'), context);
  assert.equal(context.VeyraProtocols.getTopic('dns').id, 'dns');
  assert.match(context.VeyraProtocols.draftExpected('', 'dns'), /study context/i);
});