squiffy-runtime 6.0.1 → 7.0.0-beta.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/assets.d.ts +3 -0
- package/dist/assets.test.d.ts +1 -0
- package/dist/classes.test.d.ts +1 -0
- package/dist/confirmDialog.d.ts +8 -0
- package/dist/events.d.ts +16 -0
- package/dist/every.d.ts +12 -0
- package/dist/every.test.d.ts +1 -0
- package/dist/helpers.test.d.ts +1 -0
- package/dist/numbers.d.ts +3 -0
- package/dist/panels.d.ts +14 -0
- package/dist/panels.test.d.ts +1 -0
- package/dist/paths.d.ts +15 -0
- package/dist/paths.test.d.ts +1 -0
- package/dist/persistent.test.d.ts +1 -0
- package/dist/playerSettings.d.ts +17 -0
- package/dist/pluginManager.d.ts +2 -1
- package/dist/preview-host.d.ts +33 -0
- package/dist/preview-host.js +125 -0
- package/dist/preview-host.js.map +7 -0
- package/dist/preview-host.test.d.ts +1 -0
- package/dist/preview-html.d.ts +2 -0
- package/dist/preview-html.js +2 -0
- package/dist/preview-protocol.d.ts +69 -0
- package/dist/preview.d.ts +15 -0
- package/dist/preview.main.d.ts +1 -0
- package/dist/preview.test.d.ts +1 -0
- package/dist/saveSlots.d.ts +16 -0
- package/dist/saves.test.d.ts +1 -0
- package/dist/savesDialog.d.ts +43 -0
- package/dist/savesDialog.test.d.ts +1 -0
- package/dist/scripts.test.d.ts +1 -0
- package/dist/settings.test.d.ts +1 -0
- package/dist/settingsDialog.d.ts +17 -0
- package/dist/skill.test.d.ts +1 -0
- package/dist/sound.d.ts +35 -0
- package/dist/sound.test.d.ts +1 -0
- package/dist/squiffy.page.css +155 -0
- package/dist/squiffy.preview.html +1631 -0
- package/dist/squiffy.runtime.css +1123 -20
- package/dist/squiffy.runtime.d.ts +13 -1
- package/dist/squiffy.runtime.global.js +146 -76
- package/dist/squiffy.runtime.global.js.map +1 -1
- package/dist/squiffy.runtime.js +7105 -4681
- package/dist/squiffy.runtime.js.map +1 -1
- package/dist/state.d.ts +17 -0
- package/dist/storyLists.d.ts +4 -0
- package/dist/storyLists.test.d.ts +1 -0
- package/dist/style.test.d.ts +1 -0
- package/dist/textProcessor.d.ts +27 -0
- package/dist/transitions.test.d.ts +1 -0
- package/dist/types.d.ts +137 -1
- package/dist/types.plugins.d.ts +2 -0
- package/dist/uiLinks.test.d.ts +1 -0
- package/dist/utils.d.ts +2 -1
- package/dist/utils.test.d.ts +1 -0
- package/package.json +25 -11
- package/src/__snapshots__/squiffy.runtime.test.ts.snap +22 -22
- package/src/animation.test.ts +44 -0
- package/src/animation.ts +9 -5
- package/src/assets.test.ts +225 -0
- package/src/assets.ts +92 -0
- package/src/classes.test.ts +201 -0
- package/src/confirmDialog.ts +66 -0
- package/src/events.ts +20 -0
- package/src/every.test.ts +25 -0
- package/src/every.ts +94 -0
- package/src/helpers.test.ts +34 -0
- package/src/numbers.ts +21 -0
- package/src/panels.test.ts +225 -0
- package/src/panels.ts +187 -0
- package/src/paths.test.ts +66 -0
- package/src/paths.ts +155 -0
- package/src/persistent.test.ts +155 -0
- package/src/playerSettings.ts +128 -0
- package/src/pluginManager.ts +6 -1
- package/src/plugins/animate.ts +34 -9
- package/src/plugins/live.ts +18 -5
- package/src/plugins/plugins.test.ts +93 -6
- package/src/plugins/replaceLabel.ts +7 -3
- package/src/preview-host.test.ts +171 -0
- package/src/preview-host.ts +190 -0
- package/src/preview-protocol.ts +79 -0
- package/src/preview.main.ts +34 -0
- package/src/preview.test.ts +367 -0
- package/src/preview.ts +331 -0
- package/src/saveSlots.ts +159 -0
- package/src/saves.test.ts +199 -0
- package/src/savesDialog.test.ts +290 -0
- package/src/savesDialog.ts +499 -0
- package/src/scripts.test.ts +130 -0
- package/src/settings.test.ts +233 -0
- package/src/settingsDialog.ts +160 -0
- package/src/skill.test.ts +67 -0
- package/src/sound.test.ts +273 -0
- package/src/sound.ts +207 -0
- package/src/squiffy.page.css +155 -0
- package/src/squiffy.runtime.css +1123 -20
- package/src/squiffy.runtime.test.ts +2806 -44
- package/src/squiffy.runtime.ts +1130 -64
- package/src/state.ts +172 -11
- package/src/storyLists.test.ts +134 -0
- package/src/storyLists.ts +57 -0
- package/src/style.test.ts +89 -0
- package/src/textProcessor.ts +739 -58
- package/src/transitions.test.ts +343 -0
- package/src/types.plugins.ts +7 -0
- package/src/types.ts +226 -2
- package/src/uiLinks.test.ts +156 -0
- package/src/updater.ts +26 -8
- package/src/utils.test.ts +60 -0
- package/src/utils.ts +46 -14
- package/vite.config.ts +3 -4
- package/vite.preview.config.ts +83 -0
- package/dist/infixExpressions.d.ts +0 -2
- package/src/infixExpressions.test.ts +0 -187
- package/src/infixExpressions.ts +0 -323
- /package/dist/{infixExpressions.test.d.ts → animation.test.d.ts} +0 -0
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
import { afterEach, beforeAll, beforeEach, describe, expect, test, vi } from "vitest";
|
|
2
|
+
import globalJsdom from "global-jsdom";
|
|
3
|
+
import { compile } from "squiffy-compiler";
|
|
4
|
+
import { createPreview } from "./preview.js";
|
|
5
|
+
import type { PreviewMessage } from "./preview-protocol.js";
|
|
6
|
+
import type { Panel, PlayerSetting, Story } from "./types.js";
|
|
7
|
+
|
|
8
|
+
beforeAll(() => {
|
|
9
|
+
vi.stubGlobal("crypto", { randomUUID: () => "00000000-0000-0000-0000-000000000000" });
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
let cleanup: () => void;
|
|
13
|
+
let objectUrls: string[];
|
|
14
|
+
|
|
15
|
+
beforeEach(() => {
|
|
16
|
+
cleanup = globalJsdom("<!DOCTYPE html><html><head></head><body><div id=\"squiffy\"></div></body></html>");
|
|
17
|
+
objectUrls = [];
|
|
18
|
+
// jsdom has no object URLs
|
|
19
|
+
URL.createObjectURL = () => {
|
|
20
|
+
const url = `blob:preview/${objectUrls.length}`;
|
|
21
|
+
objectUrls.push(url);
|
|
22
|
+
return url;
|
|
23
|
+
};
|
|
24
|
+
URL.revokeObjectURL = () => {};
|
|
25
|
+
// Nor scrolling, which the preview's scroll: "element" uses
|
|
26
|
+
window.Element.prototype.scrollIntoView = () => {};
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
afterEach(() => {
|
|
30
|
+
cleanup();
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// The compiled story module, as a host would send it
|
|
34
|
+
const compileJs = async (script: string, panels?: Panel[], settings?: PlayerSetting[]) => {
|
|
35
|
+
const result = await compile({ script, panels, settings });
|
|
36
|
+
if (!result.success) throw new Error("Compile failed");
|
|
37
|
+
return result.getJs();
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// Node can't import from blob URLs, so evaluate the module's one export here instead
|
|
41
|
+
const loadModule = async (js: string) => {
|
|
42
|
+
const body = js.replace("export const story = {};", "const story = {};") + "\nreturn story;";
|
|
43
|
+
return { story: new Function(body)() as Story };
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const setUp = () => {
|
|
47
|
+
const messages: PreviewMessage[] = [];
|
|
48
|
+
const element = document.getElementById("squiffy")!;
|
|
49
|
+
const preview = createPreview({ element, send: m => messages.push(m), loadModule });
|
|
50
|
+
return { preview, messages, element };
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const script = `@title Test
|
|
54
|
+
[[start]]:
|
|
55
|
+
@set score=1
|
|
56
|
+
Welcome. Go to the [[cellar]] or [look].
|
|
57
|
+
|
|
58
|
+
[look]:
|
|
59
|
+
@set looked
|
|
60
|
+
You look around. 
|
|
61
|
+
|
|
62
|
+
[[cellar]]:
|
|
63
|
+
It's dark down here.
|
|
64
|
+
`;
|
|
65
|
+
|
|
66
|
+
describe("preview", () => {
|
|
67
|
+
test("loads a story and reports what happens", async () => {
|
|
68
|
+
const { preview, messages, element } = setUp();
|
|
69
|
+
await preview.receive({ type: "load", story: { js: await compileJs(script) } });
|
|
70
|
+
|
|
71
|
+
expect(element.textContent).toContain("Welcome.");
|
|
72
|
+
expect(messages).toEqual([
|
|
73
|
+
{ type: "themeFixed", fixed: false },
|
|
74
|
+
{ type: "usesSound", usesSound: false },
|
|
75
|
+
{ type: "canGoBackChanged", canGoBack: false },
|
|
76
|
+
// The story-wide turn count (#377), which isn't internal, so it's reported
|
|
77
|
+
{ type: "attributeSet", name: "turns", value: 0 },
|
|
78
|
+
{ type: "attributeSet", name: "score", value: 1 },
|
|
79
|
+
{ type: "navigated", section: "start" },
|
|
80
|
+
{ type: "loaded" },
|
|
81
|
+
]);
|
|
82
|
+
|
|
83
|
+
messages.length = 0;
|
|
84
|
+
element.querySelector<HTMLElement>("a[data-passage='look']")!.click();
|
|
85
|
+
await vi.waitFor(() => expect(messages.some(m => m.type == "navigated")).toBe(true));
|
|
86
|
+
expect(messages).toEqual([
|
|
87
|
+
{ type: "attributeSet", name: "turns", value: 1 },
|
|
88
|
+
// Where the player was before (#56), which isn't internal either
|
|
89
|
+
{ type: "attributeSet", name: "previous", value: "start" },
|
|
90
|
+
{ type: "attributeSet", name: "looked", value: true },
|
|
91
|
+
{ type: "canGoBackChanged", canGoBack: true },
|
|
92
|
+
{ type: "navigated", section: "start", passage: "look" },
|
|
93
|
+
]);
|
|
94
|
+
|
|
95
|
+
messages.length = 0;
|
|
96
|
+
await preview.receive({ type: "back" });
|
|
97
|
+
expect(element.textContent).not.toContain("You look around");
|
|
98
|
+
expect(messages).toContainEqual({ type: "navigated", section: "start" });
|
|
99
|
+
expect(messages).toContainEqual({ type: "canGoBackChanged", canGoBack: false });
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test("reports a story with sound, mutes it, and stops it when another story is loaded (#444)", async () => {
|
|
103
|
+
const played: { src: string; muted: boolean; paused: boolean }[] = [];
|
|
104
|
+
vi.stubGlobal("Audio", class {
|
|
105
|
+
muted = false;
|
|
106
|
+
paused = true;
|
|
107
|
+
volume = 1;
|
|
108
|
+
loop = false;
|
|
109
|
+
preload = "";
|
|
110
|
+
constructor(public src = "") {
|
|
111
|
+
played.push(this);
|
|
112
|
+
}
|
|
113
|
+
play() {
|
|
114
|
+
this.paused = false;
|
|
115
|
+
return Promise.resolve();
|
|
116
|
+
}
|
|
117
|
+
pause() {
|
|
118
|
+
this.paused = true;
|
|
119
|
+
}
|
|
120
|
+
addEventListener() {}
|
|
121
|
+
});
|
|
122
|
+
try {
|
|
123
|
+
const { preview, messages } = setUp();
|
|
124
|
+
await preview.receive({ type: "muted", muted: true });
|
|
125
|
+
await preview.receive({ type: "load", story: { js: await compileJs("@music theme.mp3\nHello.") } });
|
|
126
|
+
expect(messages).toContainEqual({ type: "usesSound", usesSound: true });
|
|
127
|
+
const music = played.filter(a => !a.paused);
|
|
128
|
+
expect(music.map(a => a.muted)).toEqual([true]);
|
|
129
|
+
|
|
130
|
+
await preview.receive({ type: "muted", muted: false });
|
|
131
|
+
expect(music[0].muted).toBe(false);
|
|
132
|
+
|
|
133
|
+
await preview.receive({ type: "load", story: { js: await compileJs("Hello.") } });
|
|
134
|
+
expect(played.filter(a => !a.paused)).toEqual([]);
|
|
135
|
+
}
|
|
136
|
+
finally {
|
|
137
|
+
vi.unstubAllGlobals();
|
|
138
|
+
vi.stubGlobal("crypto", { randomUUID: () => "00000000-0000-0000-0000-000000000000" });
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
test("starts at a section or a passage", async () => {
|
|
143
|
+
const { preview, messages, element } = setUp();
|
|
144
|
+
const js = await compileJs(script);
|
|
145
|
+
|
|
146
|
+
await preview.receive({ type: "load", story: { js }, startAt: { section: "cellar" } });
|
|
147
|
+
expect(element.textContent).toContain("It's dark");
|
|
148
|
+
expect(element.textContent).not.toContain("Welcome");
|
|
149
|
+
|
|
150
|
+
messages.length = 0;
|
|
151
|
+
await preview.receive({ type: "load", story: { js }, startAt: { section: "start", passage: "look" } });
|
|
152
|
+
expect(element.textContent).toContain("Welcome.");
|
|
153
|
+
expect(element.textContent).toContain("You look around.");
|
|
154
|
+
expect(messages.filter(m => m.type == "navigated")).toEqual([
|
|
155
|
+
{ type: "navigated", section: "start" },
|
|
156
|
+
{ type: "navigated", section: "start", passage: "look" },
|
|
157
|
+
]);
|
|
158
|
+
|
|
159
|
+
// An unknown section starts from the beginning
|
|
160
|
+
await preview.receive({ type: "load", story: { js }, startAt: { section: "nowhere" } });
|
|
161
|
+
expect(element.textContent).toContain("Welcome.");
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
test("resolves assets to object URLs it makes itself", async () => {
|
|
165
|
+
const { preview, element } = setUp();
|
|
166
|
+
await preview.receive({
|
|
167
|
+
type: "assets",
|
|
168
|
+
assets: { files: { "./images/cat.png": new Uint8Array([1, 2, 3]) }, urls: { "other.png": "https://example.com/other.png" } },
|
|
169
|
+
});
|
|
170
|
+
await preview.receive({ type: "load", story: { js: await compileJs(script) }, startAt: { section: "start", passage: "look" } });
|
|
171
|
+
expect(element.querySelector("img")!.getAttribute("src")).toBe(objectUrls[0]);
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
test("restart, and hot updates that keep the story's state", async () => {
|
|
175
|
+
const { preview, element } = setUp();
|
|
176
|
+
await preview.receive({ type: "load", story: { js: await compileJs(script) } });
|
|
177
|
+
element.querySelector<HTMLElement>("a[data-section='cellar']")!.click();
|
|
178
|
+
await vi.waitFor(() => expect(element.textContent).toContain("It's dark"));
|
|
179
|
+
|
|
180
|
+
await preview.receive({ type: "update", js: await compileJs(script.replace("It's dark", "It's pitch black")) });
|
|
181
|
+
expect(element.textContent).toContain("It's pitch black");
|
|
182
|
+
expect(element.textContent).toContain("Welcome.");
|
|
183
|
+
|
|
184
|
+
await preview.receive({ type: "restart" });
|
|
185
|
+
await vi.waitFor(() => expect(element.textContent).not.toContain("pitch black"));
|
|
186
|
+
expect(element.textContent).toContain("Welcome.");
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
test("hot updates replace the story's stylesheets, keeping its state", async () => {
|
|
190
|
+
const { preview, element, messages } = setUp();
|
|
191
|
+
const js = await compileJs(script);
|
|
192
|
+
const styles = () => [...document.head.querySelectorAll("style")].map(s => s.textContent);
|
|
193
|
+
const fixed = () => messages.filter(m => m.type == "themeFixed");
|
|
194
|
+
await preview.receive({ type: "load", story: { js, css: ["body { color: red; }", "p { margin: 0; }"] } });
|
|
195
|
+
element.querySelector<HTMLElement>("a[data-section='cellar']")!.click();
|
|
196
|
+
await vi.waitFor(() => expect(element.textContent).toContain("It's dark"));
|
|
197
|
+
|
|
198
|
+
messages.length = 0;
|
|
199
|
+
await preview.receive({ type: "update", js, css: ["body { color: blue; }"] });
|
|
200
|
+
// After the preview's own (empty) theme style
|
|
201
|
+
expect(styles()).toEqual(["", "body { color: blue; }"]);
|
|
202
|
+
expect(element.textContent).toContain("It's dark");
|
|
203
|
+
expect(fixed()).toEqual([{ type: "themeFixed", fixed: false }]);
|
|
204
|
+
|
|
205
|
+
// A stylesheet that sets color-scheme
|
|
206
|
+
messages.length = 0;
|
|
207
|
+
await preview.receive({ type: "update", js, css: [":root { color-scheme: light; }"] });
|
|
208
|
+
expect(fixed()).toEqual([{ type: "themeFixed", fixed: true }]);
|
|
209
|
+
|
|
210
|
+
// The same stylesheets again, or none given, change nothing
|
|
211
|
+
messages.length = 0;
|
|
212
|
+
await preview.receive({ type: "update", js, css: [":root { color-scheme: light; }"] });
|
|
213
|
+
await preview.receive({ type: "update", js });
|
|
214
|
+
expect(styles()).toEqual(["", ":root { color-scheme: light; }"]);
|
|
215
|
+
expect(fixed()).toEqual([]);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
test("swaps the url()s in the story's stylesheets for its assets' URLs", async () => {
|
|
219
|
+
const { preview } = setUp();
|
|
220
|
+
const js = await compileJs(script);
|
|
221
|
+
const css = `@font-face { src: url("fonts/a.woff2") format("woff2"), url(missing.woff); }
|
|
222
|
+
body { background: url('./bg.png'), url(https://example.com/x.png); }`;
|
|
223
|
+
const style = () => [...document.head.querySelectorAll("style")][1].textContent;
|
|
224
|
+
await preview.receive({ type: "assets", assets: { files: { "fonts/a.woff2": new Uint8Array([1]), "bg.png": new Uint8Array([2]) } } });
|
|
225
|
+
await preview.receive({ type: "load", story: { js, css: [css] } });
|
|
226
|
+
expect(style()).toBe(`@font-face { src: url("blob:preview/0") format("woff2"), url(missing.woff); }
|
|
227
|
+
body { background: url("blob:preview/1"), url(https://example.com/x.png); }`);
|
|
228
|
+
|
|
229
|
+
// New assets go to the stylesheets that are already there
|
|
230
|
+
await preview.receive({ type: "assets", assets: { files: { "fonts/a.woff2": new Uint8Array([3]) } } });
|
|
231
|
+
expect(style()).toContain("src: url(\"blob:preview/2\")");
|
|
232
|
+
expect(style()).toContain("url('./bg.png')");
|
|
233
|
+
// The same stylesheet again changes nothing
|
|
234
|
+
await preview.receive({ type: "update", js, css: [css] });
|
|
235
|
+
expect(style()).toContain("src: url(\"blob:preview/2\")");
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
test("adds the story's stylesheets, replacing the previous story's", async () => {
|
|
239
|
+
const { preview } = setUp();
|
|
240
|
+
const js = await compileJs(script);
|
|
241
|
+
await preview.receive({ type: "load", story: { js, css: ["body { color: red; }"] } });
|
|
242
|
+
await preview.receive({ type: "load", story: { js, css: ["body { color: blue; }"] } });
|
|
243
|
+
// After the preview's own (empty) theme style
|
|
244
|
+
expect([...document.head.querySelectorAll("style")].map(s => s.textContent)).toEqual(["", "body { color: blue; }"]);
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
test("sets the theme before the story's stylesheets, so they can override it", async () => {
|
|
248
|
+
const { preview } = setUp();
|
|
249
|
+
const js = await compileJs(script);
|
|
250
|
+
await preview.receive({ type: "load", story: { js, css: [":root { color-scheme: light; }"] } });
|
|
251
|
+
await preview.receive({ type: "theme", theme: "dark" });
|
|
252
|
+
const styles = () => [...document.head.querySelectorAll("style")].map(s => s.textContent);
|
|
253
|
+
expect(styles()).toEqual([":root { color-scheme: dark; }", ":root { color-scheme: light; }"]);
|
|
254
|
+
|
|
255
|
+
await preview.receive({ type: "theme", theme: "system" });
|
|
256
|
+
expect(styles()[0]).toBe("");
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
test("says whether the story's stylesheets set its color-scheme", async () => {
|
|
260
|
+
const { preview, messages } = setUp();
|
|
261
|
+
const js = await compileJs(script);
|
|
262
|
+
const fixed = () => messages.filter(m => m.type == "themeFixed");
|
|
263
|
+
|
|
264
|
+
await preview.receive({ type: "theme", theme: "dark" });
|
|
265
|
+
await preview.receive({ type: "load", story: { js, css: ["body { color: red; }"] } });
|
|
266
|
+
expect(fixed()).toEqual([{ type: "themeFixed", fixed: false }]);
|
|
267
|
+
|
|
268
|
+
messages.length = 0;
|
|
269
|
+
await preview.receive({ type: "load", story: { js, css: [":root { color-scheme: light; }"] } });
|
|
270
|
+
expect(fixed()).toEqual([{ type: "themeFixed", fixed: true }]);
|
|
271
|
+
// Finding out doesn't change the theme the host set
|
|
272
|
+
expect(document.head.querySelector("style")!.textContent).toBe(":root { color-scheme: dark; }");
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
test("shows the story's panels, and shows them again when they change", async () => {
|
|
276
|
+
const { preview, element } = setUp();
|
|
277
|
+
const panelScript = "[[]]:\n\n[status]:\nScore: {{score}}\n\n" + script;
|
|
278
|
+
await preview.receive({ type: "load", story: { js: await compileJs(panelScript, [{ passage: "status", label: "Status", position: "side" }]) } });
|
|
279
|
+
expect(element.querySelector(".squiffy-preview-header button.squiffy-panel-button")!.textContent).toBe("Status");
|
|
280
|
+
expect(element.querySelector(".squiffy-preview-body > aside section")!.innerHTML).toBe("<p>Score: 1</p>");
|
|
281
|
+
|
|
282
|
+
await preview.receive({ type: "update", js: await compileJs(panelScript, [{ passage: "status", label: "Score" }]) });
|
|
283
|
+
expect(element.querySelector("aside")).toBeNull();
|
|
284
|
+
expect([...element.querySelectorAll("button.squiffy-panel-button")].map(b => b.textContent)).toEqual(["Score"]);
|
|
285
|
+
|
|
286
|
+
// A new story has no panels left over
|
|
287
|
+
await preview.receive({ type: "load", story: { js: await compileJs(script) } });
|
|
288
|
+
expect(element.querySelector("button.squiffy-panel-button")).toBeNull();
|
|
289
|
+
expect(document.querySelectorAll("dialog.squiffy-panel-dialog")).toHaveLength(0);
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
test("shows a Settings button for a story with settings, which opens the Settings dialog", async () => {
|
|
293
|
+
const { preview, element } = setUp();
|
|
294
|
+
const settings: PlayerSetting[] = [{ name: "warnings", label: "Content warnings", type: "toggle" }];
|
|
295
|
+
const panels: Panel[] = [{ passage: "status", label: "Status" }];
|
|
296
|
+
const panelScript = "[[]]:\n\n[status]:\nScore: {{score}}\n\n" + script;
|
|
297
|
+
await preview.receive({ type: "load", story: { js: await compileJs(panelScript, panels, settings) } });
|
|
298
|
+
const buttons = () => [...element.querySelectorAll<HTMLButtonElement>(".squiffy-preview-header button")];
|
|
299
|
+
expect(buttons().map(b => b.title || b.textContent)).toEqual(["Status", "Settings"]);
|
|
300
|
+
buttons()[1].click();
|
|
301
|
+
expect(document.querySelector("dialog.squiffy-settings label")!.textContent).toBe("Content warnings");
|
|
302
|
+
|
|
303
|
+
// Still after the panels' buttons when they change
|
|
304
|
+
await preview.receive({ type: "update", js: await compileJs(panelScript, [{ passage: "status", label: "Score" }], settings) });
|
|
305
|
+
expect(buttons().map(b => b.title || b.textContent)).toEqual(["Score", "Settings"]);
|
|
306
|
+
|
|
307
|
+
await preview.receive({ type: "update", js: await compileJs(panelScript, panels) });
|
|
308
|
+
expect(buttons().map(b => b.title || b.textContent)).toEqual(["Status"]);
|
|
309
|
+
|
|
310
|
+
await preview.receive({ type: "load", story: { js: await compileJs(script) } });
|
|
311
|
+
expect(document.querySelectorAll("dialog.squiffy-settings")).toHaveLength(0);
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
test("puts the story's classes on #squiffy, as a published story does", async () => {
|
|
315
|
+
const { preview, element } = setUp();
|
|
316
|
+
const settings: PlayerSetting[] = [{ name: "warnings", label: "Content warnings", type: "toggle" }];
|
|
317
|
+
const classScript = "[[start]]:\n@class dark\nIn the [[cellar]].\n\n[[cellar]]:\nDown here.\n";
|
|
318
|
+
await preview.receive({ type: "load", story: { js: await compileJs(classScript, undefined, settings) } });
|
|
319
|
+
expect(element.id).toBe("squiffy");
|
|
320
|
+
expect(element.classList.contains("dark")).toBe(true);
|
|
321
|
+
expect(document.querySelector("div#squiffy.dark")).toBe(element);
|
|
322
|
+
|
|
323
|
+
// A setting's class, when the player changes it
|
|
324
|
+
element.querySelector<HTMLButtonElement>(".squiffy-settings-button")!.click();
|
|
325
|
+
const toggle = document.querySelector<HTMLInputElement>("dialog.squiffy-settings input[type=checkbox]")!;
|
|
326
|
+
toggle.checked = true;
|
|
327
|
+
toggle.dispatchEvent(new window.Event("change"));
|
|
328
|
+
await Promise.resolve();
|
|
329
|
+
expect(element.classList.contains("setting-warnings")).toBe(true);
|
|
330
|
+
|
|
331
|
+
// A section's class goes when the player leaves it
|
|
332
|
+
element.querySelector<HTMLAnchorElement>("a.squiffy-link")!.click();
|
|
333
|
+
await new Promise(resolve => setTimeout(resolve, 0));
|
|
334
|
+
expect(element.classList.contains("dark")).toBe(false);
|
|
335
|
+
expect(element.classList.contains("setting-warnings")).toBe(true);
|
|
336
|
+
|
|
337
|
+
// And a new story starts with none
|
|
338
|
+
await preview.receive({ type: "load", story: { js: await compileJs(script) } });
|
|
339
|
+
expect([...element.classList]).toEqual([]);
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
test("reports errors", async () => {
|
|
343
|
+
const messages: PreviewMessage[] = [];
|
|
344
|
+
const preview = createPreview({
|
|
345
|
+
element: document.getElementById("squiffy")!,
|
|
346
|
+
send: m => messages.push(m),
|
|
347
|
+
loadModule: async () => { throw new Error("Bad story"); },
|
|
348
|
+
});
|
|
349
|
+
await preview.receive({ type: "load", story: { js: "" } });
|
|
350
|
+
expect(messages).toMatchObject([{ type: "runtimeError", message: "Bad story" }]);
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
test("reloads the page when the host asks, as it does before the next story after one with scripts (#528)", async () => {
|
|
354
|
+
const reload = vi.fn();
|
|
355
|
+
const preview = createPreview({ element: document.getElementById("squiffy")!, send: () => {}, loadModule, reload });
|
|
356
|
+
await preview.receive({ type: "reload" });
|
|
357
|
+
expect(reload).toHaveBeenCalledOnce();
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
test("ignores messages for a story that hasn't loaded", async () => {
|
|
361
|
+
const { preview, messages } = setUp();
|
|
362
|
+
await preview.receive({ type: "restart" });
|
|
363
|
+
await preview.receive({ type: "back" });
|
|
364
|
+
await preview.receive({ type: "update", js: await compileJs(script) });
|
|
365
|
+
expect(messages).toEqual([]);
|
|
366
|
+
});
|
|
367
|
+
});
|