-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathindex.test.ts
More file actions
231 lines (214 loc) · 6.68 KB
/
index.test.ts
File metadata and controls
231 lines (214 loc) · 6.68 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import { describe, expect, it, beforeEach } from 'bun:test';
import { resolveSdtMetadata, clearSdtMetadataCache } from './index.js';
describe('resolveSdtMetadata', () => {
beforeEach(() => {
clearSdtMetadataCache();
});
it('normalizes field annotation metadata', () => {
const metadata = resolveSdtMetadata({
nodeType: 'fieldAnnotation',
attrs: {
type: 'text',
fieldId: 'field-123',
displayLabel: 'Customer',
defaultDisplayLabel: 'Customer',
alias: 'Customer Name',
fieldColor: '#ff00ff',
borderColor: 'None',
highlighted: 'false',
fontFamily: 'Calibri',
fontSize: '12pt',
textColor: '#333333',
textHighlight: '#ffff00',
linkUrl: 'https://example.com',
imageSrc: null,
rawHtml: { html: '<p>hello</p>' },
size: { width: '120', height: 32 },
extras: { foo: 'bar' },
multipleImage: 'true',
hash: 'abc123',
generatorIndex: '2',
sdtId: '456',
hidden: 'false',
visibility: 'Hidden',
isLocked: 'true',
bold: 'true',
italic: false,
underline: 'true',
},
});
expect(metadata).toEqual({
type: 'fieldAnnotation',
fieldId: 'field-123',
variant: 'text',
fieldType: undefined,
displayLabel: 'Customer',
defaultDisplayLabel: 'Customer',
alias: 'Customer Name',
fieldColor: '#ff00ff',
borderColor: undefined,
highlighted: false,
fontFamily: 'Calibri',
fontSize: '12pt',
textColor: '#333333',
textHighlight: '#ffff00',
linkUrl: 'https://example.com',
imageSrc: null,
rawHtml: { html: '<p>hello</p>' },
size: { width: 120, height: 32 },
extras: { foo: 'bar' },
multipleImage: true,
hash: 'abc123',
generatorIndex: 2,
sdtId: '456',
hidden: false,
visibility: 'hidden',
isLocked: true,
formatting: { bold: true, underline: true },
marks: undefined,
});
});
it('supports structured content blocks', () => {
const metadata = resolveSdtMetadata({
nodeType: 'structuredContentBlock',
attrs: { id: '42', tag: 'block', alias: 'Block Alias', sdtPr: { foo: 'bar' } },
});
expect(metadata).toEqual({
type: 'structuredContent',
scope: 'block',
id: '42',
tag: 'block',
alias: 'Block Alias',
sdtPr: { foo: 'bar' },
});
});
it('carries appearance through for inline structured content (SD-3110)', () => {
const metadata = resolveSdtMetadata({
nodeType: 'structuredContent',
attrs: { id: '7', tag: 'citation', alias: 'Harvey citation', appearance: 'hidden' },
});
expect(metadata).toMatchObject({
type: 'structuredContent',
scope: 'inline',
appearance: 'hidden',
});
});
it('drops unknown appearance values rather than letting them flow to the renderer', () => {
const metadata = resolveSdtMetadata({
nodeType: 'structuredContent',
attrs: { id: '8', tag: 'x', appearance: 'malformed' },
});
expect(metadata).toMatchObject({ type: 'structuredContent', scope: 'inline' });
expect((metadata as { appearance?: string }).appearance).toBeUndefined();
});
it('omits appearance when the source attr is missing', () => {
const metadata = resolveSdtMetadata({
nodeType: 'structuredContent',
attrs: { id: '9', tag: 'x' },
});
expect((metadata as { appearance?: string }).appearance).toBeUndefined();
});
it('normalizes document section metadata', () => {
const metadata = resolveSdtMetadata({
nodeType: 'documentSection',
attrs: { id: 's1', title: 'Section', description: 'Desc', sectionType: 'legal', isLocked: 'true' },
});
expect(metadata).toEqual({
type: 'documentSection',
id: 's1',
title: 'Section',
description: 'Desc',
sectionType: 'legal',
isLocked: true,
sdBlockId: null,
});
});
it('returns undefined for unsupported node types', () => {
expect(resolveSdtMetadata({ nodeType: 'unknown', attrs: {} })).toBeUndefined();
});
it('uses cache when cache key is provided', () => {
const attrs: Record<string, unknown> = {
type: 'text',
fieldId: 'cache-field',
displayLabel: 'Cached label',
hash: 'cache-hash',
};
const first = resolveSdtMetadata({ nodeType: 'fieldAnnotation', attrs });
attrs.displayLabel = 'Mutated label';
const second = resolveSdtMetadata({ nodeType: 'fieldAnnotation', attrs });
expect(second).toBe(first);
expect(second?.displayLabel).toBe('Cached label');
});
it('handles field annotation with minimal attrs (only fieldId)', () => {
const metadata = resolveSdtMetadata({
nodeType: 'fieldAnnotation',
attrs: { fieldId: 'MINIMAL_FIELD' },
});
expect(metadata).toEqual({
type: 'fieldAnnotation',
fieldId: 'MINIMAL_FIELD',
variant: undefined,
fieldType: undefined,
displayLabel: undefined,
defaultDisplayLabel: undefined,
alias: undefined,
fieldColor: undefined,
borderColor: undefined,
highlighted: true,
fontFamily: null,
fontSize: null,
textColor: null,
textHighlight: null,
linkUrl: null,
imageSrc: null,
rawHtml: undefined,
size: null,
extras: null,
multipleImage: false,
hash: null,
generatorIndex: null,
sdtId: null,
hidden: false,
visibility: undefined,
isLocked: false,
formatting: undefined,
marks: undefined,
});
});
it('handles field annotation with missing fieldId (defaults to empty string)', () => {
const metadata = resolveSdtMetadata({
nodeType: 'fieldAnnotation',
attrs: {},
});
expect(metadata?.fieldId).toBe('');
expect(metadata?.type).toBe('fieldAnnotation');
});
it('supports all field annotation variants', () => {
const variants = ['text', 'image', 'signature', 'checkbox', 'html', 'link'] as const;
variants.forEach((variant) => {
const metadata = resolveSdtMetadata({
nodeType: 'fieldAnnotation',
attrs: { type: variant, fieldId: `field-${variant}` },
});
expect(metadata?.variant).toBe(variant);
});
});
it('handles docPartObject metadata', () => {
const metadata = resolveSdtMetadata({
nodeType: 'docPartObject',
attrs: {
docPartGallery: 'Table of Contents',
id: 'toc-unique-1',
alias: 'TOC',
instruction: 'TOC \\o "1-3"',
},
});
expect(metadata).toEqual({
type: 'docPartObject',
gallery: 'Table of Contents',
uniqueId: 'toc-unique-1',
alias: 'TOC',
instruction: 'TOC \\o "1-3"',
});
});
});