-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
tests.js
108 lines (94 loc) · 2.53 KB
/
tests.js
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 fs from 'fs';
import path from 'path';
import { assert } from './public/utils.js';
import * as model from './public/model.js';
import { compile } from './public/compiler.js';
function assertThrows(fn)
{
let throws = false;
try
{
fn();
}
catch (e)
{
throws = true;
}
assert (throws);
}
// Test username validation
{
assert (typeof model.MAX_USERNAME_LENGTH == 'number');
model.validateUserName('Foo');
model.validateUserName('el');
model.validateUserName('Foo_bar2');
model.validateUserName('Foo Bar 2');
model.validateUserName('_Long_User_Name_');
// Invalid usernames
assertThrows(_ => model.validateUserName(' foo '));
assertThrows(_ => model.validateUserName('foo)'));
assertThrows(_ => model.validateUserName(''));
assertThrows(_ => model.validateUserName('overly long username foobar'));
}
// Test CreateNode
{
var m = new model.Model();
m.new();
m.update(new model.CreateNode('AudioOut', 0, 0));
assert (m.hasNode('AudioOut'));
m.serialize();
}
// Test undo/redo and SetParam
{
var m = new model.Model();
m.new();
let knobId = m.update(new model.CreateNode('Knob', 0, 0));
m.update(new model.DeleteNodes([knobId]));
m.undo();
assert (m.hasNode('Knob'));
m.redo();
m.undo();
m.update(new model.SetParam(knobId, "value", 0.5));
m.serialize();
}
// Test copy/paste
{
var m = new model.Model();
m.new();
let knob0 = m.update(new model.CreateNode('Knob', 0, 0));
let knob1 = m.update(new model.CreateNode('Knob', 10, 10));
var data = m.copy([knob0, knob1]);
m.update(new model.Paste(data, 20, 20));
assert (m.numNodes == 4);
}
// Test grouping
{
var m = new model.Model();
m.new();
m.update(new model.CreateNode('Add', 0, 0));
m.update(new model.CreateNode('Add', 10, 10));
m.update(new model.ConnectNodes("0", 1, "1", 0));
m.update(new model.GroupNodes(["1"]));
assert (m.numNodes == 2);
}
// Try loading all of our example projects
fs.readdirSync('examples').forEach(fileName =>
{
// Read the example file
let filePath = path.join('examples', fileName);
console.log(filePath);
let data = fs.readFileSync(filePath, 'utf8')
// Test deserialization
let m = new model.Model();
m.deserialize(data);
// Test serialization
let out = m.serialize();
assert (out.length > 0);
// Test the compiler
let unit = compile(m.state);
let genSample = new Function(
'time',
'nodes',
unit.src
);
});