-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathutil.spec.ts
More file actions
199 lines (176 loc) · 5.99 KB
/
util.spec.ts
File metadata and controls
199 lines (176 loc) · 5.99 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
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
*
* Copyright Oxide Computer Company
*/
import { describe, expect, it, test } from 'vitest'
import { diskCan, genName, instanceCan, parsePortRange, synthesizeData } from './util'
describe('parsePortRange', () => {
describe('parses', () => {
it('single ports up to 5 digits', () => {
expect(parsePortRange('0')).toEqual([0, 0])
expect(parsePortRange('1')).toEqual([1, 1])
expect(parsePortRange('123')).toEqual([123, 123])
expect(parsePortRange('12356')).toEqual([12356, 12356])
})
it('ranges', () => {
expect(parsePortRange('123-456')).toEqual([123, 456])
expect(parsePortRange('1-45690')).toEqual([1, 45690])
expect(parsePortRange('5-5')).toEqual([5, 5])
})
it('with surrounding whitespace', () => {
expect(parsePortRange('123-456 ')).toEqual([123, 456])
expect(parsePortRange(' 1-45690')).toEqual([1, 45690])
expect(parsePortRange(' 5-5 \n')).toEqual([5, 5])
})
})
describe('rejects', () => {
it('nonsense', () => {
expect(parsePortRange('12a5')).toEqual(null)
expect(parsePortRange('lkajsdfha')).toEqual(null)
})
it('p2 < p1', () => {
expect(parsePortRange('123-45')).toEqual(null)
})
it('too many digits', () => {
expect(parsePortRange('239032')).toEqual(null)
})
})
})
test('genName', () => {
expect(genName('a'.repeat(64), 'b'.repeat(64))).toMatch(/^a{27}-b{27}-[0-9a-f]{6}$/)
expect(genName('a'.repeat(64), 'b'.repeat(64), 'c'.repeat(64))).toMatch(
/^a{18}-b{18}-c{18}-[0-9a-f]{6}$/
)
// Test a bunch of lengths to make sure we don't overflow the max length
for (let i = 2; i <= 128; i = 2 * i) {
const singlePartName = genName('a'.repeat(i))
expect(singlePartName.length).toBeLessThanOrEqual(63)
expect(singlePartName).toMatch(/^a+-[0-9a-f]{6}$/)
const doublePartName = genName('a'.repeat(i / 2), 'b'.repeat(i / 2))
expect(doublePartName.length).toBeLessThanOrEqual(63)
expect(doublePartName).toMatch(/^a+-b+-[0-9a-f]{6}$/)
}
})
const pt = (timestamp: Date, value: number) => ({
timestamp,
datum: { datum: value, type: 'i64' as const },
})
describe('synthesizeData', () => {
const start = new Date(2023, 3, 2)
const mid1 = new Date(2023, 3, 3)
const mid2 = new Date(2023, 3, 4)
const end = new Date(2023, 3, 5)
it('returns undefined when either input list is undefined', () => {
expect(synthesizeData(undefined, undefined, start, end, (x) => x)).toEqual(undefined)
expect(synthesizeData([], undefined, start, end, (x) => x)).toEqual(undefined)
expect(synthesizeData(undefined, [], start, end, (x) => x)).toEqual(undefined)
})
it('adds 0s at start and end when there is no data', () => {
expect(synthesizeData([], [], start, end, (x) => x)).toEqual([
{ timestamp: start.getTime(), value: 0 },
{ timestamp: end.getTime(), value: 0 },
])
})
it("adds start and end when there's data in range", () => {
const result = synthesizeData(
[pt(mid1, 4), pt(mid2, 5)],
[pt(new Date(0), 3)],
start,
end,
(x) => x
)
expect(result).toEqual([
{ timestamp: start.getTime(), value: 3 },
{ timestamp: mid1.getTime(), value: 4 },
{ timestamp: mid2.getTime(), value: 5 },
{ timestamp: end.getTime(), value: 5 },
])
})
it('valueTransform is applied to both data in range and synthetic start and end', () => {
const result = synthesizeData(
[pt(mid1, 4), pt(mid2, 5)],
[pt(new Date(0), 3)],
start,
end,
(x) => 2 * x
)
expect(result).toEqual([
{ timestamp: start.getTime(), value: 6 },
{ timestamp: mid1.getTime(), value: 8 },
{ timestamp: mid2.getTime(), value: 10 },
{ timestamp: end.getTime(), value: 10 },
])
})
it('does not add synthentic start when existing data point matches start time exactly', () => {
const result = synthesizeData(
[pt(start, 4), pt(mid1, 5)],
[pt(new Date(0), 3)],
start,
end,
(x) => x
)
expect(result).toEqual([
{ timestamp: start.getTime(), value: 4 },
{ timestamp: mid1.getTime(), value: 5 },
{ timestamp: end.getTime(), value: 5 },
])
})
})
test('instanceCan', () => {
expect(instanceCan.start({ runState: 'running' })).toBe(false)
expect(instanceCan.start({ runState: 'stopped' })).toBe(true)
// @ts-expect-error typechecker rejects actions that don't exist
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
instanceCan.abc
})
test('diskCan', () => {
expect(diskCan.delete({ state: { state: 'creating' } })).toBe(false)
expect(diskCan.delete({ state: { state: 'attached', instance: 'xyz' } })).toBe(false)
expect(diskCan.delete({ state: { state: 'detached' } })).toBe(true)
// snapshot requires distributed, non-read-only disk type
expect(
diskCan.snapshot({
state: { state: 'detached' },
diskType: 'distributed',
readOnly: false,
})
).toBe(true)
expect(
diskCan.snapshot({
state: { state: 'attached', instance: 'x' },
diskType: 'distributed',
readOnly: false,
})
).toBe(true)
expect(
diskCan.snapshot({
state: { state: 'creating' },
diskType: 'distributed',
readOnly: false,
})
).toBe(false)
expect(
diskCan.snapshot({ state: { state: 'detached' }, diskType: 'local', readOnly: false })
).toBe(false)
expect(
diskCan.snapshot({
state: { state: 'attached', instance: 'x' },
diskType: 'local',
readOnly: false,
})
).toBe(false)
// read-only disks cannot be snapshotted
expect(
diskCan.snapshot({
state: { state: 'detached' },
diskType: 'distributed',
readOnly: true,
})
).toBe(false)
// @ts-expect-error typechecker rejects actions that don't exist
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
diskCan.abc
})