this repo has no description
1import assert from 'node:assert'
2import { describe, test } from 'node:test'
3import { TaggedStringParser } from '../../src/TaggedStringParser.ts'
4
5describe('Position Tracking', () => {
6 describe('position field', () => {
7 test('should track position of single entity', () => {
8 const parser = new TaggedStringParser()
9 const result = parser.parse('[operation:OP-123]')
10
11 assert.strictEqual(result.entities[0].position, 0)
12 })
13
14 test('should track positions of multiple entities', () => {
15 const parser = new TaggedStringParser()
16 const result = parser.parse('[operation:OP-123] started with [changes:5]')
17
18 assert.strictEqual(result.entities[0].position, 0)
19 assert.strictEqual(result.entities[1].position, 32)
20 })
21
22 test('should track position with text before entity', () => {
23 const parser = new TaggedStringParser()
24 const result = parser.parse('Starting [operation:OP-123] now')
25
26 assert.strictEqual(result.entities[0].position, 9)
27 })
28 })
29
30 describe('endPosition calculation', () => {
31 test('should calculate endPosition with single-character delimiters (default)', () => {
32 const parser = new TaggedStringParser()
33 const result = parser.parse('[operation:OP-123]')
34
35 assert.strictEqual(result.entities[0].position, 0)
36 assert.strictEqual(result.entities[0].endPosition, 18)
37 })
38
39 test('should calculate endPosition with multi-character delimiters', () => {
40 const parser = new TaggedStringParser({
41 openDelimiter: '{{',
42 closeDelimiter: '}}',
43 })
44 const result = parser.parse('{{operation:OP-123}}')
45
46 assert.strictEqual(result.entities[0].position, 0)
47 assert.strictEqual(result.entities[0].endPosition, 20)
48 })
49
50 test('should calculate endPosition for multiple entities in one message', () => {
51 const parser = new TaggedStringParser()
52 const result = parser.parse(
53 '[operation:OP-123] started with [changes:5] to [stack:ST-456]',
54 )
55
56 assert.strictEqual(result.entities.length, 3)
57
58 assert.strictEqual(result.entities[0].position, 0)
59 assert.strictEqual(result.entities[0].endPosition, 18)
60
61 assert.strictEqual(result.entities[1].position, 32)
62 assert.strictEqual(result.entities[1].endPosition, 43)
63
64 assert.strictEqual(result.entities[2].position, 47)
65 assert.strictEqual(result.entities[2].endPosition, 61)
66 })
67
68 test('should calculate endPosition for entity at start of message', () => {
69 const parser = new TaggedStringParser()
70 const result = parser.parse('[operation:OP-123] started')
71
72 assert.strictEqual(result.entities[0].position, 0)
73 assert.strictEqual(result.entities[0].endPosition, 18)
74 })
75
76 test('should calculate endPosition for entity in middle of message', () => {
77 const parser = new TaggedStringParser()
78 const result = parser.parse('Starting [operation:OP-123] now')
79
80 assert.strictEqual(result.entities[0].position, 9)
81 assert.strictEqual(result.entities[0].endPosition, 27)
82 })
83
84 test('should calculate endPosition for entity at end of message', () => {
85 const parser = new TaggedStringParser()
86 const result = parser.parse('Completed [operation:OP-123]')
87
88 assert.strictEqual(result.entities[0].position, 10)
89 assert.strictEqual(result.entities[0].endPosition, 28)
90 })
91
92 test('should calculate endPosition correctly with custom single-character delimiters', () => {
93 const parser = new TaggedStringParser({
94 openDelimiter: '<',
95 closeDelimiter: '>',
96 })
97 const result = parser.parse('<operation:OP-123>')
98
99 assert.strictEqual(result.entities[0].position, 0)
100 assert.strictEqual(result.entities[0].endPosition, 18)
101 })
102
103 test('should calculate endPosition correctly with longer multi-character delimiters', () => {
104 const parser = new TaggedStringParser({
105 openDelimiter: '<<<',
106 closeDelimiter: '>>>',
107 })
108 const result = parser.parse('<<<operation:OP-123>>>')
109
110 assert.strictEqual(result.entities[0].position, 0)
111 assert.strictEqual(result.entities[0].endPosition, 22)
112 })
113
114 test('should calculate endPosition for multiple entities with custom delimiters', () => {
115 const parser = new TaggedStringParser({
116 openDelimiter: '{{',
117 closeDelimiter: '}}',
118 })
119 const result = parser.parse(
120 'User {{user:john}} performed {{count:10}} actions',
121 )
122
123 assert.strictEqual(result.entities.length, 2)
124
125 assert.strictEqual(result.entities[0].position, 5)
126 assert.strictEqual(result.entities[0].endPosition, 18)
127
128 assert.strictEqual(result.entities[1].position, 29)
129 assert.strictEqual(result.entities[1].endPosition, 41)
130 })
131 })
132})