-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvalidate.spec.ts
More file actions
180 lines (160 loc) · 6.37 KB
/
Copy pathvalidate.spec.ts
File metadata and controls
180 lines (160 loc) · 6.37 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
import { describe, expect, it } from 'vitest';
import { validateInteractConfig, assertValidInteractConfig, InteractValidationError } from '../src';
const VALID_CONFIG = {
interactions: [
{
key: 'el',
trigger: 'viewEnter',
effects: [{ namedEffect: { type: 'FadeIn' }, duration: 400, fill: 'backwards' }],
},
],
};
// Unused effects produce UNUSED_EFFECT warnings (via unusedDefinitions rule, rule.code = 'UNUSED_DEFINITION')
const CONFIG_WITH_WARNING = {
effects: { unused: { namedEffect: { type: 'FadeIn' } } },
interactions: [
{
key: 'el',
trigger: 'viewEnter',
effects: [{ namedEffect: { type: 'SlideIn' }, duration: 400, fill: 'backwards' }],
},
],
};
// Missing sequenceId reference → SEQUENCE_ID_NOT_FOUND (error)
const CONFIG_WITH_ERROR = {
interactions: [{ key: 'el', trigger: 'viewEnter', sequences: [{ sequenceId: 'missing' }] }],
};
describe('validateInteractConfig', () => {
it('returns valid=true with no errors for a valid config', () => {
const result = validateInteractConfig(VALID_CONFIG);
expect(result.valid).toBe(true);
expect(result.errors).toHaveLength(0);
});
it('returns valid=false for a config with structural errors', () => {
const result = validateInteractConfig({ interactions: 'not-an-array' });
expect(result.valid).toBe(false);
expect(result.errors.length).toBeGreaterThan(0);
});
it('returns valid=true (warnings only) for a config with only warnings', () => {
const result = validateInteractConfig(CONFIG_WITH_WARNING);
expect(result.valid).toBe(true);
expect(result.errors.some((e) => e.severity === 'warning')).toBe(true);
});
it('returns valid=false for a config with semantic errors', () => {
const result = validateInteractConfig(CONFIG_WITH_ERROR);
expect(result.valid).toBe(false);
expect(result.errors.some((e) => e.code === 'SEQUENCE_ID_NOT_FOUND')).toBe(true);
});
it('sorts errors by path lexicographically', () => {
const config = {
effects: {
a: { namedEffect: { type: 'Foo' } },
b: { namedEffect: { type: 'Bar' } },
},
interactions: [
{
key: 'el',
trigger: 'viewEnter',
effects: [{ namedEffect: { type: 'Baz' }, duration: 400 }],
},
],
};
const result = validateInteractConfig(config);
const unusedErrors = result.errors.filter((e) => e.code === 'UNUSED_EFFECT');
expect(unusedErrors).toHaveLength(2);
expect(unusedErrors[0].path).toEqual(['effects', 'a']);
expect(unusedErrors[1].path).toEqual(['effects', 'b']);
});
});
describe('ValidateOptions', () => {
it('strict promotes warnings to errors, making valid=false', () => {
const result = validateInteractConfig(CONFIG_WITH_WARNING, { strict: true });
expect(result.valid).toBe(false);
expect(result.errors.every((e) => e.severity === 'error')).toBe(true);
});
it("severityOverrides with 'off' skips the rule entirely", () => {
// 'UNUSED_DEFINITION' is the rule.code for the unusedDefinitions rule
const result = validateInteractConfig(CONFIG_WITH_WARNING, {
severityOverrides: { UNUSED_DEFINITION: 'off' },
});
expect(result.errors.filter((e) => e.code === 'UNUSED_EFFECT')).toHaveLength(0);
});
it("severityOverrides can promote a warning to 'error'", () => {
const result = validateInteractConfig(CONFIG_WITH_WARNING, {
severityOverrides: { UNUSED_DEFINITION: 'error' },
});
const unusedErr = result.errors.find((e) => e.code === 'UNUSED_EFFECT');
expect(unusedErr?.severity).toBe('error');
});
// Semantic rule categories registered for the rule-derived checks (A/C/D + animationEnd graph).
const RETRIGGER_CONFIG = {
interactions: [
{
key: 'el',
trigger: 'viewEnter',
effects: [{ namedEffect: { type: 'FadeIn' }, duration: 400, triggerType: 'alternate' }],
},
],
};
it("severityOverrides 'off' silences a semantic warning category (SAME_ELEMENT_RETRIGGER)", () => {
const result = validateInteractConfig(RETRIGGER_CONFIG, {
severityOverrides: { SAME_ELEMENT_RETRIGGER: 'off' },
});
expect(result.errors.filter((e) => e.code === 'SAME_ELEMENT_RETRIGGER')).toHaveLength(0);
});
it("severityOverrides can promote a semantic warning to 'error'", () => {
const result = validateInteractConfig(RETRIGGER_CONFIG, {
severityOverrides: { SAME_ELEMENT_RETRIGGER: 'error' },
});
const err = result.errors.find((e) => e.code === 'SAME_ELEMENT_RETRIGGER');
expect(err?.severity).toBe('error');
expect(result.valid).toBe(false);
});
it('strict promotes semantic warnings to errors', () => {
const result = validateInteractConfig(RETRIGGER_CONFIG, { strict: true });
expect(result.valid).toBe(false);
expect(result.errors.every((e) => e.severity === 'error')).toBe(true);
});
it('max truncates the returned error list', () => {
const config = {
effects: {
a: { namedEffect: { type: 'Foo' } },
b: { namedEffect: { type: 'Bar' } },
c: { namedEffect: { type: 'Baz' } },
},
interactions: [
{
key: 'el',
trigger: 'viewEnter',
effects: [{ namedEffect: { type: 'Qux' }, duration: 400 }],
},
],
};
const result = validateInteractConfig(config, { max: 1 });
expect(result.errors).toHaveLength(1);
});
});
describe('assertValidInteractConfig', () => {
it('does not throw for a valid config', () => {
expect(() => assertValidInteractConfig(VALID_CONFIG)).not.toThrow();
});
it('throws InteractValidationError for an invalid config', () => {
expect(() => assertValidInteractConfig(CONFIG_WITH_ERROR)).toThrow(InteractValidationError);
});
it('thrown error carries the validation errors array', () => {
try {
assertValidInteractConfig(CONFIG_WITH_ERROR);
} catch (e) {
expect(e).toBeInstanceOf(InteractValidationError);
expect((e as InteractValidationError).errors.length).toBeGreaterThan(0);
expect((e as InteractValidationError).errors[0].code).toBe('SEQUENCE_ID_NOT_FOUND');
}
});
it('narrows the type to InteractConfig after assertion', () => {
const config: unknown = VALID_CONFIG;
assertValidInteractConfig(config);
// TypeScript now knows config: InteractConfig — no runtime assertion needed,
// the fact that no throw occurred is the guarantee.
expect(config).toBeDefined();
});
});