forked from sugarlabs/musicblocks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaidebugger.test.js
More file actions
220 lines (194 loc) · 8.31 KB
/
aidebugger.test.js
File metadata and controls
220 lines (194 loc) · 8.31 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/**
* MusicBlocks
*
* @author kh-ub-ayb
*
* @copyright 2026 kh-ub-ayb
*
* @license
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
const fs = require("fs");
const path = require("path");
// Load the AIDebuggerWidget class by reading the source and evaluating it
const source = fs.readFileSync(path.resolve(__dirname, "../aidebugger.js"), "utf-8");
new Function(
source + "\nif (typeof global !== 'undefined') { global.AIDebuggerWidget = AIDebuggerWidget; }"
)();
// Mock globals
global._ = str => str;
global._THIS_IS_MUSIC_BLOCKS_ = true;
describe("AIDebuggerWidget", () => {
describe("Constructor", () => {
test("initializes basic properties", () => {
const debuggerWidget = new AIDebuggerWidget();
expect(debuggerWidget.chatHistory).toEqual([]);
expect(debuggerWidget.promptCount).toBe(0);
expect(typeof debuggerWidget.conversationId).toBe("string");
expect(debuggerWidget.conversationId.startsWith("conv_")).toBe(true);
expect(debuggerWidget.activity).toBeNull();
expect(debuggerWidget.widgetWindow).toBeNull();
expect(debuggerWidget.chatLog).toBeNull();
expect(debuggerWidget.messageInput).toBeNull();
expect(debuggerWidget.sendButton).toBeNull();
});
test("_generateConversationId returns unique IDs", () => {
const debuggerWidget = new AIDebuggerWidget();
const id1 = debuggerWidget._generateConversationId();
const id2 = debuggerWidget._generateConversationId();
expect(id1).not.toBe(id2);
expect(id1.startsWith("conv_")).toBe(true);
});
});
describe("Debugging Helper Methods", () => {
let debuggerWidget;
beforeEach(() => {
debuggerWidget = new AIDebuggerWidget();
});
describe("_getNumericValue", () => {
test("returns null for invalid blockId or blockMap", () => {
expect(debuggerWidget._getNumericValue(null, {})).toBeNull();
expect(debuggerWidget._getNumericValue("id1", {})).toBeNull();
});
test("returns value for number block (simple)", () => {
const blockMap = {
id1: ["id1", "number"]
};
expect(debuggerWidget._getNumericValue("id1", blockMap)).toBe("number");
});
test("returns value for number block (array format)", () => {
const blockMap = {
id1: ["id1", ["number", { value: 42 }]]
};
expect(debuggerWidget._getNumericValue("id1", blockMap)).toBe(42);
});
test("returns null for non-number block", () => {
const blockMap = {
id1: ["id1", ["text", { value: "hello" }]]
};
expect(debuggerWidget._getNumericValue("id1", blockMap)).toBeNull();
});
});
describe("_getTextValue", () => {
test("returns value for text block", () => {
const blockMap = {
id1: ["id1", ["text", { value: "hello world" }]]
};
expect(debuggerWidget._getTextValue("id1", blockMap)).toBe("hello world");
});
test("returns null for non-text block", () => {
const blockMap = {
id1: ["id1", ["number", { value: 42 }]]
};
expect(debuggerWidget._getTextValue("id1", blockMap)).toBeNull();
});
});
describe("_getDrumName", () => {
test("returns value for drumname block", () => {
const blockMap = {
id1: ["id1", ["drumname", { value: "snare" }]]
};
expect(debuggerWidget._getDrumName("id1", blockMap)).toBe("snare");
});
test("returns null for non-drumname block", () => {
const blockMap = {
id1: ["id1", ["number", { value: 42 }]]
};
expect(debuggerWidget._getDrumName("id1", blockMap)).toBeNull();
});
});
describe("_getNamedBoxValue", () => {
test("returns value for namedbox block", () => {
const blockMap = {
id1: ["id1", ["namedbox", { value: "myVar" }]]
};
expect(debuggerWidget._getNamedBoxValue("id1", blockMap)).toBe("myVar");
});
test("returns value for namedarg block", () => {
const blockMap = {
id1: ["id1", ["namedarg", { value: "myArg" }]]
};
expect(debuggerWidget._getNamedBoxValue("id1", blockMap)).toBe("myArg");
});
test("returns null for non-namedbox block", () => {
const blockMap = {
id1: ["id1", ["text", { value: "hello" }]]
};
expect(debuggerWidget._getNamedBoxValue("id1", blockMap)).toBeNull();
});
});
describe("_isBase64Data", () => {
test("flags valid base64 image prefixes correctly", () => {
expect(debuggerWidget._isBase64Data("data:image/png;base64,iVBORw0K")).toBe(true);
expect(debuggerWidget._isBase64Data("data:audio/mp3;base64,SUQzBAA")).toBe(true);
});
test("flags invalid base64 prefixes correctly", () => {
expect(debuggerWidget._isBase64Data("data:text/html;base64,PGh0bWw+")).toBe(false);
expect(debuggerWidget._isBase64Data("https://example.com/image.png")).toBe(false);
expect(debuggerWidget._isBase64Data(12345)).toBe(false);
expect(debuggerWidget._isBase64Data(null)).toBe(false);
});
});
describe("_getBlockRepresentation", () => {
test("formats basic action block", () => {
const blockMap = {
action1: ["action1", ["action", null], [null, "action_name"]],
action_name: ["action_name", ["text", { value: "Jump" }]]
};
const result = debuggerWidget._getBlockRepresentation(
"action",
null,
blockMap["action1"],
blockMap,
1,
false,
null
);
expect(result).toBe('Action: "Jump"');
});
test("formats forward block", () => {
const blockMap = {
forward1: ["forward1", ["forward", null], [null, "dist"]],
dist: ["dist", ["number", { value: 100 }]]
};
const result = debuggerWidget._getBlockRepresentation(
"forward",
null,
blockMap["forward1"],
blockMap,
1,
false,
null
);
expect(result).toBe("Move Forward → 100 Steps");
});
test("formats setmasterbpm2 block", () => {
const blockMap = {
bpm1: ["bpm1", ["setmasterbpm2", null], null, [null, "val"]],
val: ["val", ["number", { value: 120 }]]
};
const result = debuggerWidget._getBlockRepresentation(
"setmasterbpm2",
null,
blockMap["bpm1"],
blockMap,
1,
false,
null
);
expect(result).toBe("Set Master BPM → 120 BPM");
});
});
});
});