-
-
Notifications
You must be signed in to change notification settings - Fork 436
/
Copy pathsketches-service-impl.test.ts
174 lines (167 loc) · 4.66 KB
/
sketches-service-impl.test.ts
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
import type { Mutable } from '@theia/core/lib/common/types';
import { FileUri } from '@theia/core/lib/common/file-uri';
import stableJsonStringify from 'fast-json-stable-stringify';
import assert from 'node:assert/strict';
import { basename, join } from 'node:path';
import { SketchContainer, SketchRef } from '../../common/protocol';
import { discoverSketches } from '../../node/sketches-service-impl';
const testSketchbook = join(
__dirname,
'..',
'..',
'..',
'src',
'test',
'node',
'__test_sketchbook__'
);
const sketchFolderAsSketchbook = join(testSketchbook, 'a_sketch');
const emptySketchbook = join(testSketchbook, 'empty');
describe('discover-sketches', () => {
it('should recursively discover all sketches in a folder', async () => {
const actual = await discoverSketches(
testSketchbook,
SketchContainer.create('test')
);
containersDeepEquals(
actual,
expectedTestSketchbookContainer(
testSketchbook,
testSketchbookContainerTemplate
)
);
});
it('should handle when the sketchbook is a sketch folder', async () => {
const actual = await discoverSketches(
sketchFolderAsSketchbook,
SketchContainer.create('foo-bar')
);
const name = basename(sketchFolderAsSketchbook);
containersDeepEquals(actual, {
children: [],
label: 'foo-bar',
sketches: [
{
name,
uri: FileUri.create(sketchFolderAsSketchbook).toString(),
},
],
});
});
it('should handle empty sketchbook', async () => {
const actual = await discoverSketches(
emptySketchbook,
SketchContainer.create('empty')
);
containersDeepEquals(actual, SketchContainer.create('empty'));
});
});
function containersDeepEquals(
actual: SketchContainer,
expected: SketchContainer
) {
const stableActual = JSON.parse(stableJsonStringify(actual));
const stableExpected = JSON.parse(stableJsonStringify(expected));
assert.deepStrictEqual(stableActual, stableExpected); // TODO: get rid of `fast-json-stable-stringify`
}
/**
* A `template://` schema will be resolved against the actual `rootPath` location at runtime.
* For example if `rootPath` is `/path/to/a/folder/` and the template URI is `template://foo/bar/My_Sketch/My_Sketch.ino`,
* then the resolved, expected URI will be `file:///path/to/a/folder/foo/bar/My_Sketch/My_Sketch.ino`.
* The path of a template URI must be relative.
*/
function expectedTestSketchbookContainer(
rootPath: string,
containerTemplate: SketchContainer,
label?: string
): SketchContainer {
let rootUri = FileUri.create(rootPath).toString();
if (rootUri.charAt(rootUri.length - 1) !== '/') {
rootUri += '/';
}
const adjustUri = (sketch: Mutable<SketchRef>) => {
assert.strictEqual(sketch.uri.startsWith('template://'), true);
assert.strictEqual(sketch.uri.startsWith('template:///'), false);
sketch.uri = sketch.uri.replace('template://', rootUri).toString();
return sketch;
};
const adjustContainer = (container: SketchContainer) => {
container.sketches.forEach(adjustUri);
container.children.forEach(adjustContainer);
return <Mutable<SketchContainer>>container;
};
const container = adjustContainer(containerTemplate);
if (label) {
container.label = label;
}
return container;
}
const testSketchbookContainerTemplate: SketchContainer = {
label: 'test',
children: [
{
label: 'project1',
children: [
{
label: 'CodeA',
children: [],
sketches: [
{
name: 'version1A',
uri: 'template://project1/CodeA/version1A',
},
{
name: 'version2A',
uri: 'template://project1/CodeA/version2A',
},
],
},
{
label: 'CodeB',
children: [],
sketches: [
{
name: 'version1B',
uri: 'template://project1/CodeB/version1B',
},
{
name: 'version2B',
uri: 'template://project1/CodeB/version2B',
},
],
},
],
sketches: [],
},
{
label: 'nested_4',
children: [
{
label: 'nested_3',
children: [],
sketches: [
{
name: 'nested_2',
uri: 'template://nested_4/nested_3/nested_2',
},
],
},
],
sketches: [],
},
],
sketches: [
{
name: 'bar++',
uri: 'template://bar%2B%2B',
},
{
name: 'bar++ 2',
uri: 'template://bar%2B%2B%202',
},
{
name: 'a_sketch',
uri: 'template://a_sketch',
},
],
};