links-notation 0.23.0 → 0.24.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,99 @@
1
+ import { readFileSync } from 'node:fs';
2
+ import { expect, test } from 'bun:test';
3
+ import {
4
+ BinaryLinoCodec,
5
+ decodeReferenceLiteral,
6
+ encodeReferenceLiteral,
7
+ formatBinaryReference,
8
+ formatLinks,
9
+ FormatConfig,
10
+ Link,
11
+ Parser,
12
+ StreamParser,
13
+ } from '../src/index.js';
14
+
15
+ const references = readFileSync(
16
+ new globalThis.URL(
17
+ '../../docs/protocol/reference-literals.txt',
18
+ import.meta.url
19
+ ),
20
+ 'utf8'
21
+ )
22
+ .trim()
23
+ .split('\n')
24
+ .filter((line) => !line.startsWith('#'))
25
+ .map((hex) => (hex === '-' ? '' : Buffer.from(hex, 'hex').toString('utf8')));
26
+
27
+ test('native reference fidelity', () => {
28
+ for (const id of references) {
29
+ const original = new Link(id, [
30
+ new Link(id),
31
+ new Link('nested', [new Link(id)]),
32
+ ]);
33
+ for (const lessParentheses of [
34
+ false,
35
+ true,
36
+ new FormatConfig({ maxInlineRefs: 1, preferInline: false }),
37
+ ]) {
38
+ const text = formatLinks([original], lessParentheses);
39
+ expect(new Parser().parse(text)).toEqual([original]);
40
+ }
41
+ }
42
+ });
43
+
44
+ test('reference literals at every chunk split', () => {
45
+ for (const id of references) {
46
+ expect(decodeReferenceLiteral(encodeReferenceLiteral(id))).toBe(id);
47
+ for (const literal of [
48
+ Link.escapeReference(id),
49
+ formatBinaryReference(id),
50
+ encodeReferenceLiteral(id),
51
+ ]) {
52
+ const text = `(root: ${literal})\n(${literal}: fixture)`;
53
+ const expected = [
54
+ new Link('root', [new Link(id)]),
55
+ new Link(id, [new Link('fixture')]),
56
+ ];
57
+ expect(new Parser().parse(text)).toEqual(expected);
58
+ for (let split = 0; split <= text.length; split++) {
59
+ const stream = new StreamParser();
60
+ stream.write(text.slice(0, split));
61
+ stream.write(text.slice(split));
62
+ expect(stream.end()).toEqual(expected);
63
+ }
64
+ }
65
+ }
66
+ }, 10000);
67
+
68
+ test('binary reference fidelity', () => {
69
+ const codec = new BinaryLinoCodec();
70
+ for (const id of references) {
71
+ const original = [new Link(id, [new Link(id)])];
72
+ const decoded = codec.decode(codec.encode(original));
73
+ expect(decoded).toEqual(original);
74
+ expect(codec.parseDocument(codec.formatDocument(decoded))).toEqual(
75
+ original
76
+ );
77
+ }
78
+ });
79
+
80
+ test('malformed reference literals are rejected', () => {
81
+ const invalid = readFileSync(
82
+ new globalThis.URL(
83
+ '../../docs/protocol/invalid-reference-literals.txt',
84
+ import.meta.url
85
+ ),
86
+ 'utf8'
87
+ )
88
+ .trim()
89
+ .split('\n')
90
+ .filter((line) => !line.startsWith('#'));
91
+ for (const literal of invalid) {
92
+ expect(() => decodeReferenceLiteral(literal)).toThrow();
93
+ expect(() => new Parser().parse(literal)).toThrow();
94
+ }
95
+ expect(() => decodeReferenceLiteral('~1{61}\n')).toThrow();
96
+ expect(decodeReferenceLiteral('~1{C3A9}')).toBe('é');
97
+ expect(() => encodeReferenceLiteral('\ud800')).toThrow();
98
+ expect(() => Link.escapeReference('\ud800')).toThrow();
99
+ });