-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathproject.spec.ts
More file actions
108 lines (94 loc) · 3.73 KB
/
project.spec.ts
File metadata and controls
108 lines (94 loc) · 3.73 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
import { describe, it, expect, beforeEach } from 'vitest'
import { Project, Session } from '../project'
import { Construct } from '../construct'
import { Diagnostics } from '../diagnostics'
class TestConstruct extends Construct {
constructor (logicalId: any) {
super('test', logicalId)
}
describe (): string {
return `Test:${this.logicalId}`
}
synthesize () {
return null
}
}
describe('Construct logicalId validation', () => {
beforeEach(() => {
Session.reset()
Session.project = { addResource: () => {} } as any
})
it('should pass validation for valid logicalIds', async () => {
const validIds = ['my-check', 'my_check', 'myCheck123', 'my/check', 'my#check', 'my.check']
for (const id of validIds) {
const construct = new TestConstruct(id)
const diagnostics = new Diagnostics()
await construct.validate(diagnostics)
expect(diagnostics.isFatal(), `Expected "${id}" to be valid`).toBe(false)
}
})
it('should store the original logicalId without modification', () => {
const construct = new TestConstruct('my check')
expect(construct.logicalId).toBe('my check')
})
it('should produce an error diagnostic for logicalIds with spaces', async () => {
const construct = new TestConstruct('my check')
const diagnostics = new Diagnostics()
await construct.validate(diagnostics)
expect(diagnostics.isFatal()).toBe(true)
expect(diagnostics.observations).toEqual(expect.arrayContaining([
expect.objectContaining({
message: expect.stringContaining('contains invalid characters'),
}),
]))
})
it('should produce an error diagnostic for logicalIds with special characters', async () => {
const construct = new TestConstruct('my@check!')
const diagnostics = new Diagnostics()
await construct.validate(diagnostics)
expect(diagnostics.isFatal()).toBe(true)
expect(diagnostics.observations).toEqual(expect.arrayContaining([
expect.objectContaining({
message: expect.stringContaining('contains invalid characters'),
}),
]))
})
it('should produce an error diagnostic for empty logicalIds', async () => {
const construct = new TestConstruct('')
const diagnostics = new Diagnostics()
await construct.validate(diagnostics)
expect(diagnostics.isFatal()).toBe(true)
expect(diagnostics.observations).toEqual(expect.arrayContaining([
expect.objectContaining({
message: expect.stringContaining('contains invalid characters'),
}),
]))
})
it('should produce a diagnostic for non-string logicalId instead of throwing', async () => {
const construct = new TestConstruct(123 as any)
expect(construct.logicalId).toBe('123')
const diagnostics = new Diagnostics()
await construct.validate(diagnostics)
expect(diagnostics.isFatal()).toBe(true)
expect(diagnostics.observations).toEqual(expect.arrayContaining([
expect.objectContaining({
message: expect.stringContaining('Expected a string but received type "number"'),
}),
]))
})
it('should produce a diagnostic for duplicate logicalId instead of throwing', async () => {
Session.reset()
const project = new Project('test-project', { name: 'Test' })
Session.project = project
project.addResource('check', 'duplicate-id', new TestConstruct('duplicate-id'))
project.addResource('check', 'duplicate-id', new TestConstruct('duplicate-id'))
const diagnostics = new Diagnostics()
await project.validate(diagnostics)
expect(diagnostics.isFatal()).toBe(true)
expect(diagnostics.observations).toEqual(expect.arrayContaining([
expect.objectContaining({
message: expect.stringContaining('A check with logicalId "duplicate-id" already exists'),
}),
]))
})
})