squiffy-runtime 7.0.0-beta.6 → 7.0.0-beta.8
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 +1 -0
- package/dist/classes.test.d.ts +1 -0
- package/dist/confirmDialog.d.ts +8 -0
- package/dist/events.d.ts +11 -0
- package/dist/every.d.ts +12 -0
- package/dist/every.test.d.ts +1 -0
- package/dist/paths.d.ts +15 -0
- package/dist/paths.test.d.ts +1 -0
- package/dist/playerSettings.d.ts +17 -0
- package/dist/preview-host.d.ts +2 -0
- package/dist/preview-host.js +10 -0
- package/dist/preview-host.js.map +2 -2
- package/dist/preview-html.js +1 -1
- package/dist/preview-protocol.d.ts +6 -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 +14 -3
- package/dist/squiffy.preview.html +680 -82
- package/dist/squiffy.runtime.css +531 -1
- package/dist/squiffy.runtime.d.ts +5 -1
- package/dist/squiffy.runtime.global.js +117 -96
- package/dist/squiffy.runtime.global.js.map +1 -1
- package/dist/squiffy.runtime.js +5728 -4520
- package/dist/squiffy.runtime.js.map +1 -1
- package/dist/state.d.ts +4 -1
- package/dist/style.test.d.ts +1 -0
- package/dist/textProcessor.d.ts +16 -0
- package/dist/transitions.test.d.ts +1 -0
- package/dist/types.d.ts +54 -0
- package/dist/uiLinks.test.d.ts +1 -0
- package/dist/utils.d.ts +1 -0
- package/dist/utils.test.d.ts +1 -0
- package/package.json +3 -3
- package/src/__snapshots__/squiffy.runtime.test.ts.snap +10 -10
- package/src/animation.test.ts +44 -0
- package/src/animation.ts +9 -5
- package/src/assets.test.ts +1 -3
- package/src/assets.ts +1 -1
- package/src/classes.test.ts +201 -0
- package/src/confirmDialog.ts +66 -0
- package/src/events.ts +18 -0
- package/src/every.test.ts +25 -0
- package/src/every.ts +94 -0
- package/src/panels.test.ts +24 -0
- package/src/paths.test.ts +66 -0
- package/src/paths.ts +155 -0
- package/src/playerSettings.ts +128 -0
- package/src/plugins/animate.ts +16 -3
- package/src/plugins/live.ts +10 -4
- package/src/plugins/plugins.test.ts +51 -6
- package/src/preview-host.test.ts +22 -0
- package/src/preview-host.ts +15 -0
- package/src/preview-protocol.ts +9 -1
- package/src/preview.test.ts +114 -3
- package/src/preview.ts +97 -3
- 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 +14 -3
- package/src/squiffy.runtime.css +531 -1
- package/src/squiffy.runtime.test.ts +1130 -17
- package/src/squiffy.runtime.ts +600 -38
- package/src/state.ts +45 -10
- package/src/style.test.ts +89 -0
- package/src/textProcessor.ts +484 -35
- package/src/transitions.test.ts +302 -0
- package/src/types.ts +79 -0
- package/src/uiLinks.test.ts +156 -0
- package/src/updater.ts +26 -8
- package/src/utils.test.ts +60 -0
- package/src/utils.ts +44 -14
- package/dist/infixExpressions.d.ts +0 -6
- package/src/infixExpressions.test.ts +0 -379
- package/src/infixExpressions.ts +0 -488
- /package/dist/{infixExpressions.test.d.ts → animation.test.d.ts} +0 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { expect, test, beforeEach, afterEach, beforeAll, vi } from "vitest";
|
|
1
|
+
import { expect, test, describe, beforeEach, afterEach, beforeAll, vi } from "vitest";
|
|
2
2
|
import fs from "fs/promises";
|
|
3
3
|
import globalJsdom from "global-jsdom";
|
|
4
4
|
import { init } from "./squiffy.runtime.js";
|
|
@@ -71,6 +71,18 @@ const initScript = async (script: string, expressions = false) => {
|
|
|
71
71
|
};
|
|
72
72
|
};
|
|
73
73
|
|
|
74
|
+
// Starts a story whose start section has this text, which the compiler would refuse, as it
|
|
75
|
+
// reports template errors itself, to test how the runtime shows them (in a story built by an
|
|
76
|
+
// older compiler, say)
|
|
77
|
+
const initBrokenText = async (text: string) => {
|
|
78
|
+
const element = document.getElementById("squiffy")!;
|
|
79
|
+
const { story } = await compile("[[start]]:\nPlaceholder.");
|
|
80
|
+
(story.sections as any).start.text = text;
|
|
81
|
+
const squiffyApi = await init({ element, story });
|
|
82
|
+
await squiffyApi.begin();
|
|
83
|
+
return { squiffyApi, element };
|
|
84
|
+
};
|
|
85
|
+
|
|
74
86
|
const findLink = (element: HTMLElement, linkType: string, linkText: string, onlyEnabled: boolean = false) => {
|
|
75
87
|
const links = onlyEnabled
|
|
76
88
|
? element.querySelectorAll(`.squiffy-output-section:last-child a.squiffy-link.link-${linkType}`)
|
|
@@ -418,6 +430,65 @@ Bye.
|
|
|
418
430
|
expect(guard.classList).toContain("disabled");
|
|
419
431
|
});
|
|
420
432
|
|
|
433
|
+
describe("passage links in an {{#each}} (#448)", () => {
|
|
434
|
+
const script = `
|
|
435
|
+
[[start]]:
|
|
436
|
+
@set inventory = ["lamp", "key", "rope"]
|
|
437
|
+
{{#each inventory}}
|
|
438
|
+
- [Examine the {{this}}](examine, item = "{{this}}")
|
|
439
|
+
{{/each}}
|
|
440
|
+
|
|
441
|
+
[examine]:
|
|
442
|
+
It's a {{item}}.
|
|
443
|
+
`;
|
|
444
|
+
const disabled = (element: HTMLElement) =>
|
|
445
|
+
[...element.querySelectorAll("a.link-passage")].map(l => l.classList.contains("disabled"));
|
|
446
|
+
|
|
447
|
+
test("each item's link works on its own", async () => {
|
|
448
|
+
const { squiffyApi, element } = await initScript(script, true);
|
|
449
|
+
await squiffyApi.clickLink(findLink(element, "passage", "Examine the lamp"));
|
|
450
|
+
expect(disabled(element)).toEqual([true, false, false]);
|
|
451
|
+
expect(await squiffyApi.clickLink(findLink(element, "passage", "Examine the key"))).toBe(true);
|
|
452
|
+
expect(element.textContent).toContain("It's a lamp.");
|
|
453
|
+
expect(element.textContent).toContain("It's a key.");
|
|
454
|
+
expect(disabled(element)).toEqual([true, true, false]);
|
|
455
|
+
});
|
|
456
|
+
|
|
457
|
+
test("going back enables only the link that was clicked", async () => {
|
|
458
|
+
const { squiffyApi, element } = await initScript(script, true);
|
|
459
|
+
await squiffyApi.clickLink(findLink(element, "passage", "Examine the lamp"));
|
|
460
|
+
await squiffyApi.clickLink(findLink(element, "passage", "Examine the key"));
|
|
461
|
+
squiffyApi.goBack();
|
|
462
|
+
expect(disabled(element)).toEqual([true, false, false]);
|
|
463
|
+
squiffyApi.goBack();
|
|
464
|
+
expect(disabled(element)).toEqual([false, false, false]);
|
|
465
|
+
expect(element.querySelector("[data-clicked]")).toBeNull();
|
|
466
|
+
});
|
|
467
|
+
|
|
468
|
+
test("updating the story keeps the clicked link disabled, and going back still works", async () => {
|
|
469
|
+
const { squiffyApi, element } = await initScript(script, true);
|
|
470
|
+
await squiffyApi.clickLink(findLink(element, "passage", "Examine the key"));
|
|
471
|
+
const updated = await compile(script.replace("It's a", "It is a"), undefined, true);
|
|
472
|
+
squiffyApi.update(updated.story);
|
|
473
|
+
expect(disabled(element)).toEqual([false, true, false]);
|
|
474
|
+
const changed = await compile(script.replace("- [Examine", "- [Look at"), undefined, true);
|
|
475
|
+
squiffyApi.update(changed.story);
|
|
476
|
+
expect(disabled(element)).toEqual([false, true, false]);
|
|
477
|
+
squiffyApi.goBack();
|
|
478
|
+
expect(disabled(element)).toEqual([false, false, false]);
|
|
479
|
+
});
|
|
480
|
+
|
|
481
|
+
test("a saved game remembers which link was clicked", async () => {
|
|
482
|
+
const first = await initScript(script, true);
|
|
483
|
+
await first.squiffyApi.clickLink(findLink(first.element, "passage", "Examine the rope"));
|
|
484
|
+
first.element.innerHTML = "";
|
|
485
|
+
const { squiffyApi, element } = await initScript(script, true);
|
|
486
|
+
expect(disabled(element)).toEqual([false, false, true]);
|
|
487
|
+
squiffyApi.goBack();
|
|
488
|
+
expect(disabled(element)).toEqual([false, false, false]);
|
|
489
|
+
});
|
|
490
|
+
});
|
|
491
|
+
|
|
421
492
|
test("{{deactivate}} disables links to other passages, and going back only enables the ones it disabled", async () => {
|
|
422
493
|
// https://github.com/textadventures/squiffy/issues/13
|
|
423
494
|
const script = `
|
|
@@ -970,6 +1041,159 @@ Text from a passage.
|
|
|
970
1041
|
expect(output).toBe("Here is some text from a passage: Text from a passage.");
|
|
971
1042
|
});
|
|
972
1043
|
|
|
1044
|
+
describe("Embed with parameters and wrapped text (#450)", () => {
|
|
1045
|
+
test("Parameters are shown in the embedded text", async () => {
|
|
1046
|
+
const script = `
|
|
1047
|
+
[[section1]]:
|
|
1048
|
+
{{embed "say" who="Ann" mood="happy"}}
|
|
1049
|
+
|
|
1050
|
+
[[say]]:
|
|
1051
|
+
{{who}} looks {{mood}}.
|
|
1052
|
+
`;
|
|
1053
|
+
const { element } = await initScript(script);
|
|
1054
|
+
expect(getSectionContent(element, "section1")).toBe("Ann looks happy.");
|
|
1055
|
+
});
|
|
1056
|
+
|
|
1057
|
+
test("A parameter's value can be an attribute", async () => {
|
|
1058
|
+
const script = `
|
|
1059
|
+
[[section1]]:
|
|
1060
|
+
@set name = Bob
|
|
1061
|
+
{{embed "say" who=name}}
|
|
1062
|
+
|
|
1063
|
+
[[say]]:
|
|
1064
|
+
Hello, {{who}}.
|
|
1065
|
+
`;
|
|
1066
|
+
const { element } = await initScript(script);
|
|
1067
|
+
expect(getSectionContent(element, "section1")).toBe("Hello, Bob.");
|
|
1068
|
+
});
|
|
1069
|
+
|
|
1070
|
+
test("A parameter's value can be an expression", async () => {
|
|
1071
|
+
const script = `
|
|
1072
|
+
[[section1]]:
|
|
1073
|
+
@set gold = 10
|
|
1074
|
+
{{embed "say" left=gold - 3 items=["a", "b"]}}
|
|
1075
|
+
|
|
1076
|
+
[[say]]:
|
|
1077
|
+
{{left}} gold, {{join items " and "}}.
|
|
1078
|
+
`;
|
|
1079
|
+
const { element } = await initScript(script, true);
|
|
1080
|
+
expect(getSectionContent(element, "section1")).toBe("7 gold, a and b.");
|
|
1081
|
+
});
|
|
1082
|
+
|
|
1083
|
+
test("A parameter hides an attribute with the same name, without changing it", async () => {
|
|
1084
|
+
const script = `
|
|
1085
|
+
[[section1]]:
|
|
1086
|
+
@set who = Bob
|
|
1087
|
+
{{embed "say" who="Ann"}} Then {{who}}.
|
|
1088
|
+
|
|
1089
|
+
[[say]]:
|
|
1090
|
+
{{who}}, and {{get "who"}}.
|
|
1091
|
+
`;
|
|
1092
|
+
const { element, squiffyApi } = await initScript(script);
|
|
1093
|
+
expect(getSectionContent(element, "section1")).toBe("Ann, and Ann. Then Bob.");
|
|
1094
|
+
expect(squiffyApi.get("who")).toBe("Bob");
|
|
1095
|
+
});
|
|
1096
|
+
|
|
1097
|
+
test("Setting a parameter changes it for the rest of the embed only", async () => {
|
|
1098
|
+
const script = `
|
|
1099
|
+
[[section1]]:
|
|
1100
|
+
@set who = Bob
|
|
1101
|
+
{{embed "say" who="Ann"}} Then {{who}}, {{mood}}.
|
|
1102
|
+
|
|
1103
|
+
[[say]]:
|
|
1104
|
+
{{set "who" "Cat"}}{{set "mood" "happy"}}{{who}}.
|
|
1105
|
+
`;
|
|
1106
|
+
const { element, squiffyApi } = await initScript(script);
|
|
1107
|
+
expect(getSectionContent(element, "section1")).toBe("Cat. Then Bob, happy.");
|
|
1108
|
+
expect(squiffyApi.get("who")).toBe("Bob");
|
|
1109
|
+
expect(squiffyApi.get("mood")).toBe("happy");
|
|
1110
|
+
});
|
|
1111
|
+
|
|
1112
|
+
test("A parameter works in a condition", async () => {
|
|
1113
|
+
const script = `
|
|
1114
|
+
[[section1]]:
|
|
1115
|
+
{{embed "say" who="Ann"}} {{embed "say" who="Bob"}}
|
|
1116
|
+
|
|
1117
|
+
[[say]]:
|
|
1118
|
+
{{#if who = "Ann"}}Hi Ann.{{else}}Who are you?{{/if}}
|
|
1119
|
+
`;
|
|
1120
|
+
const { element } = await initScript(script, true);
|
|
1121
|
+
expect(getSectionContent(element, "section1")).toBe("Hi Ann. Who are you?");
|
|
1122
|
+
});
|
|
1123
|
+
|
|
1124
|
+
test("An embed inside an embed doesn't see the outer one's parameters", async () => {
|
|
1125
|
+
const script = `
|
|
1126
|
+
[[section1]]:
|
|
1127
|
+
@set who = Bob
|
|
1128
|
+
{{embed "outer" who="Ann"}}
|
|
1129
|
+
|
|
1130
|
+
[[outer]]:
|
|
1131
|
+
{{who}} / {{embed "inner"}}
|
|
1132
|
+
|
|
1133
|
+
[[inner]]:
|
|
1134
|
+
{{who}}
|
|
1135
|
+
`;
|
|
1136
|
+
const { element } = await initScript(script);
|
|
1137
|
+
expect(getSectionContent(element, "section1")).toBe("Ann / Bob");
|
|
1138
|
+
});
|
|
1139
|
+
|
|
1140
|
+
test("Wrapped text is shown by {{content}}, with the caller's attributes", async () => {
|
|
1141
|
+
const script = `
|
|
1142
|
+
[[section1]]:
|
|
1143
|
+
@set name = Bob
|
|
1144
|
+
{{#embed "say" who="Ann" name="not this"}}Hello, {{name}}!{{/embed}}
|
|
1145
|
+
|
|
1146
|
+
[[say]]:
|
|
1147
|
+
{{who}}: "{{content}}"
|
|
1148
|
+
`;
|
|
1149
|
+
const { element } = await initScript(script);
|
|
1150
|
+
expect(getSectionContent(element, "section1")).toBe("Ann: \"Hello, Bob!\"");
|
|
1151
|
+
});
|
|
1152
|
+
|
|
1153
|
+
test("Wrapped text can use the caller's parameters and loop items", async () => {
|
|
1154
|
+
const script = `
|
|
1155
|
+
[[section1]]:
|
|
1156
|
+
{{embed "outer" who="Ann"}}
|
|
1157
|
+
|
|
1158
|
+
[[outer]]:
|
|
1159
|
+
{{#each (array "x" "y")}}{{#embed "box"}}{{who}}-{{this}}{{/embed}}{{/each}}
|
|
1160
|
+
|
|
1161
|
+
[[box]]:
|
|
1162
|
+
({{content}})
|
|
1163
|
+
`;
|
|
1164
|
+
const { element } = await initScript(script);
|
|
1165
|
+
expect(getSectionContent(element, "section1")).toBe("(Ann-x)(Ann-y)");
|
|
1166
|
+
});
|
|
1167
|
+
|
|
1168
|
+
test("Setting an attribute in wrapped text sets the caller's", async () => {
|
|
1169
|
+
const script = `
|
|
1170
|
+
[[section1]]:
|
|
1171
|
+
{{embed "outer" n=1}}
|
|
1172
|
+
|
|
1173
|
+
[[outer]]:
|
|
1174
|
+
{{#embed "box" n=10}}{{set "n" 2}}{{/embed}}{{n}}
|
|
1175
|
+
|
|
1176
|
+
[[box]]:
|
|
1177
|
+
{{content}}{{n}}
|
|
1178
|
+
`;
|
|
1179
|
+
const { element } = await initScript(script);
|
|
1180
|
+
expect(getSectionContent(element, "section1")).toBe("102");
|
|
1181
|
+
});
|
|
1182
|
+
|
|
1183
|
+
test("{{content}} with nothing wrapped shows a content attribute, if there is one", async () => {
|
|
1184
|
+
const script = `
|
|
1185
|
+
[[section1]]:
|
|
1186
|
+
@set content = stuff
|
|
1187
|
+
{{embed "box"}} {{content}}
|
|
1188
|
+
|
|
1189
|
+
[[box]]:
|
|
1190
|
+
({{content}})
|
|
1191
|
+
`;
|
|
1192
|
+
const { element } = await initScript(script);
|
|
1193
|
+
expect(getSectionContent(element, "section1")).toBe("(stuff) stuff");
|
|
1194
|
+
});
|
|
1195
|
+
});
|
|
1196
|
+
|
|
973
1197
|
test("Update section with embedded text", async () => {
|
|
974
1198
|
const script = `
|
|
975
1199
|
[[section1]]:
|
|
@@ -1990,7 +2214,7 @@ test("arithmetic in {{set}}, {{inc}}, {{dec}} and conditions", async () => {
|
|
|
1990
2214
|
});
|
|
1991
2215
|
|
|
1992
2216
|
test("Invalid arithmetic shows a template error", async () => {
|
|
1993
|
-
const { element } = await
|
|
2217
|
+
const { element } = await initBrokenText("{{set \"gold\" gold +}}");
|
|
1994
2218
|
expect(element.querySelector(".squiffy-error")?.textContent).toContain("Invalid expression");
|
|
1995
2219
|
});
|
|
1996
2220
|
|
|
@@ -2149,6 +2373,163 @@ test("helpers can be used in setter expressions", async () => {
|
|
|
2149
2373
|
expect(squiffyApi.get("here")).toBe(true);
|
|
2150
2374
|
});
|
|
2151
2375
|
|
|
2376
|
+
describe("objects (#442)", () => {
|
|
2377
|
+
test("@set can set an object, and values inside it", async () => {
|
|
2378
|
+
const script = `
|
|
2379
|
+
[[start]]:
|
|
2380
|
+
@set hero = { hp: 10, name: "Ann", "max hp": 12 }
|
|
2381
|
+
@set hero.hp += 5
|
|
2382
|
+
@set hero.level = 1
|
|
2383
|
+
@set hero.poisoned
|
|
2384
|
+
@set not hero.blessed
|
|
2385
|
+
@set hero.title := {{hero.name}} the Brave
|
|
2386
|
+
@set bag.gold = 3
|
|
2387
|
+
HP: {{hero.hp}}. {{#if hero.hp > 12}}Healthy.{{/if}} {{= hero.hp * 2}}
|
|
2388
|
+
`;
|
|
2389
|
+
const { squiffyApi, element } = await initScript(script, true);
|
|
2390
|
+
expect(squiffyApi.get("hero")).toEqual({ hp: 15, name: "Ann", "max hp": 12, level: 1, poisoned: true, blessed: false, title: "Ann the Brave" });
|
|
2391
|
+
expect(squiffyApi.get("bag")).toEqual({ gold: 3 });
|
|
2392
|
+
expect(element.textContent).toContain("HP: 15. Healthy. 30");
|
|
2393
|
+
});
|
|
2394
|
+
|
|
2395
|
+
test("lists of objects, with an index from another attribute", async () => {
|
|
2396
|
+
const script = `
|
|
2397
|
+
[[start]]:
|
|
2398
|
+
@set inventory = [{ name: "lamp", weight: 2 }, { name: "rope", weight: 1 }]
|
|
2399
|
+
@set i = 1
|
|
2400
|
+
@set inventory[i].weight = 5
|
|
2401
|
+
@set inventory[0].lit = true
|
|
2402
|
+
@set chosen = inventory[i]
|
|
2403
|
+
@set first = inventory[0].name
|
|
2404
|
+
{{#each inventory}}{{this.name}} ({{weight}}) {{/each}}
|
|
2405
|
+
{{#if inventory[i].weight > 4}}Heavy {{= inventory[i].name}}.{{/if}}
|
|
2406
|
+
`;
|
|
2407
|
+
const { squiffyApi, element } = await initScript(script, true);
|
|
2408
|
+
expect(squiffyApi.get("inventory")).toEqual([{ name: "lamp", weight: 2, lit: true }, { name: "rope", weight: 5 }]);
|
|
2409
|
+
expect(squiffyApi.get("chosen")).toEqual({ name: "rope", weight: 5 });
|
|
2410
|
+
expect(squiffyApi.get("first")).toBe("lamp");
|
|
2411
|
+
expect(element.textContent).toContain("lamp (2) rope (5)");
|
|
2412
|
+
expect(element.textContent).toContain("Heavy rope.");
|
|
2413
|
+
});
|
|
2414
|
+
|
|
2415
|
+
test("copying an object copies it, rather than sharing it", async () => {
|
|
2416
|
+
const script = `
|
|
2417
|
+
[[start]]:
|
|
2418
|
+
@set hero = { hp: 10 }
|
|
2419
|
+
@set saved = hero
|
|
2420
|
+
@set hero.hp = 1
|
|
2421
|
+
`;
|
|
2422
|
+
const { squiffyApi } = await initScript(script, true);
|
|
2423
|
+
expect(squiffyApi.get("saved")).toEqual({ hp: 10 });
|
|
2424
|
+
expect(squiffyApi.get("hero.hp")).toBe(1);
|
|
2425
|
+
});
|
|
2426
|
+
|
|
2427
|
+
test("link setters can set values inside objects", async () => {
|
|
2428
|
+
const script = `
|
|
2429
|
+
[[start]]:
|
|
2430
|
+
@set hero = { hp: 10, name: "Ann" }
|
|
2431
|
+
[[Heal]](inn, hero.hp += 5, hero.mood = { happy: true, reason: "rest, at last" })
|
|
2432
|
+
|
|
2433
|
+
[[inn]]:
|
|
2434
|
+
HP {{hero.hp}}.
|
|
2435
|
+
`;
|
|
2436
|
+
const { squiffyApi, element } = await initScript(script, true);
|
|
2437
|
+
await squiffyApi.clickLink(findLink(element, "section", "Heal"));
|
|
2438
|
+
expect(squiffyApi.get("hero")).toEqual({ hp: 15, name: "Ann", mood: { happy: true, reason: "rest, at last" } });
|
|
2439
|
+
expect(element.textContent).toContain("HP 15.");
|
|
2440
|
+
});
|
|
2441
|
+
|
|
2442
|
+
test("a link's setters can use square brackets, which aren't passage links", async () => {
|
|
2443
|
+
const script = `
|
|
2444
|
+
[[start]]:
|
|
2445
|
+
@set party = [{ name: "Ann", hp: 10 }, { name: "Bo", hp: 4 }]
|
|
2446
|
+
@set leader = 0
|
|
2447
|
+
[[Make Bo the leader]](next, leader = 1, party[1].hp += 2, list = ["a", "b"])
|
|
2448
|
+
|
|
2449
|
+
[[next]]:
|
|
2450
|
+
{{= party[leader].name}} has {{= party[leader].hp}} HP.
|
|
2451
|
+
`;
|
|
2452
|
+
const { squiffyApi, element } = await initScript(script, true);
|
|
2453
|
+
await squiffyApi.clickLink(findLink(element, "section", "Make Bo the leader"));
|
|
2454
|
+
expect(element.textContent).toContain("Bo has 6 HP.");
|
|
2455
|
+
expect(squiffyApi.get("list")).toEqual(["a", "b"]);
|
|
2456
|
+
});
|
|
2457
|
+
|
|
2458
|
+
test("going back undoes a change inside an object", async () => {
|
|
2459
|
+
const script = `
|
|
2460
|
+
[[start]]:
|
|
2461
|
+
@set hero = { hp: 10 }
|
|
2462
|
+
[[next]]
|
|
2463
|
+
|
|
2464
|
+
[[next]]:
|
|
2465
|
+
@set hero.hp = 3
|
|
2466
|
+
`;
|
|
2467
|
+
const { squiffyApi, element } = await initScript(script, true);
|
|
2468
|
+
await squiffyApi.clickLink(findLink(element, "section", "next"));
|
|
2469
|
+
expect(squiffyApi.get("hero.hp")).toBe(3);
|
|
2470
|
+
squiffyApi.goBack();
|
|
2471
|
+
expect(squiffyApi.get("hero")).toEqual({ hp: 10 });
|
|
2472
|
+
});
|
|
2473
|
+
|
|
2474
|
+
test("saves keep objects", async () => {
|
|
2475
|
+
const script = `
|
|
2476
|
+
[[start]]:
|
|
2477
|
+
@set hero = { hp: 10, items: ["lamp"] }
|
|
2478
|
+
`;
|
|
2479
|
+
const { squiffyApi } = await initScript(script, true);
|
|
2480
|
+
const save = squiffyApi.getSave();
|
|
2481
|
+
squiffyApi.set("hero.hp", 1);
|
|
2482
|
+
await squiffyApi.loadSave(save);
|
|
2483
|
+
expect(squiffyApi.get("hero")).toEqual({ hp: 10, items: ["lamp"] });
|
|
2484
|
+
});
|
|
2485
|
+
|
|
2486
|
+
test("squiffy.get and set, and {{set}}, {{inc}} and {{append}}, take paths", async () => {
|
|
2487
|
+
const script = `
|
|
2488
|
+
[[start]]:
|
|
2489
|
+
{{set "hero" { hp: 10 }}}{{set "hero.name" "Ann"}}{{inc "hero.hp" 2}}{{append "hero.items" "lamp"}}
|
|
2490
|
+
`;
|
|
2491
|
+
const { squiffyApi } = await initScript(script, true);
|
|
2492
|
+
expect(squiffyApi.get("hero")).toEqual({ hp: 12, name: "Ann", items: ["lamp"] });
|
|
2493
|
+
squiffyApi.set("hero.items.0", "rope");
|
|
2494
|
+
squiffyApi.set("hero[\"max hp\"]", 20);
|
|
2495
|
+
expect(squiffyApi.get("hero.items[0]")).toBe("rope");
|
|
2496
|
+
expect(squiffyApi.get("hero.max hp")).toBe(20);
|
|
2497
|
+
expect(squiffyApi.get("hero.missing.deeper")).toBeNull();
|
|
2498
|
+
expect(squiffyApi.get("nothing.here")).toBeNull();
|
|
2499
|
+
});
|
|
2500
|
+
|
|
2501
|
+
test("setting inside something that isn't an object is an error", async () => {
|
|
2502
|
+
const script = `
|
|
2503
|
+
[[start]]:
|
|
2504
|
+
@set gold = 5
|
|
2505
|
+
@set gold.amount = 1
|
|
2506
|
+
|
|
2507
|
+
[[next]]
|
|
2508
|
+
`;
|
|
2509
|
+
const { squiffyApi, element } = await initScript(script, true);
|
|
2510
|
+
await squiffyApi.clickLink(findLink(element, "section", "next"));
|
|
2511
|
+
expect(squiffyApi.get("gold")).toBe(5);
|
|
2512
|
+
expect(element.querySelector(".squiffy-error")?.textContent).toContain("gold isn't an object, so it has no amount");
|
|
2513
|
+
});
|
|
2514
|
+
|
|
2515
|
+
test("an object can't reach the prototype", async () => {
|
|
2516
|
+
const script = `
|
|
2517
|
+
[[start]]:
|
|
2518
|
+
@set hero = {}
|
|
2519
|
+
@set hero.__proto__.polluted = true
|
|
2520
|
+
@set key = "constructor"
|
|
2521
|
+
@set hero[key] = 1
|
|
2522
|
+
|
|
2523
|
+
[[next]]
|
|
2524
|
+
`;
|
|
2525
|
+
const { squiffyApi, element } = await initScript(script, true);
|
|
2526
|
+
await squiffyApi.clickLink(findLink(element, "section", "next"));
|
|
2527
|
+
expect(({} as Record<string, unknown>).polluted).toBeUndefined();
|
|
2528
|
+
expect(squiffyApi.get("hero")).toEqual({});
|
|
2529
|
+
expect(element.querySelectorAll(".squiffy-error").length).toBe(2);
|
|
2530
|
+
});
|
|
2531
|
+
});
|
|
2532
|
+
|
|
2152
2533
|
test("link setters take expressions, and can have commas in quotes", async () => {
|
|
2153
2534
|
const script = `
|
|
2154
2535
|
[[start]]:
|
|
@@ -2984,6 +3365,139 @@ Level: <select data-attribute="level" required>
|
|
|
2984
3365
|
expect(getSectionContent(element, "Continue")).toContain("Hero at level 1");
|
|
2985
3366
|
});
|
|
2986
3367
|
|
|
3368
|
+
// Presses Enter in a text box, and waits for the story to go on
|
|
3369
|
+
const pressEnter = async (input: HTMLElement) => {
|
|
3370
|
+
input.dispatchEvent(new window.KeyboardEvent("keypress", { key: "Enter", bubbles: true, cancelable: true }));
|
|
3371
|
+
await new Promise(resolve => setTimeout(resolve, 0));
|
|
3372
|
+
};
|
|
3373
|
+
|
|
3374
|
+
test("A text box with data-section goes to the section when Enter is pressed", async () => {
|
|
3375
|
+
// https://github.com/textadventures/squiffy/issues/454
|
|
3376
|
+
const script = `
|
|
3377
|
+
[[start]]:
|
|
3378
|
+
What's your name? <input type="text" data-attribute="name" data-section="greet">
|
|
3379
|
+
|
|
3380
|
+
[[greet]]:
|
|
3381
|
+
Hello, {{name}}!
|
|
3382
|
+
`;
|
|
3383
|
+
const { squiffyApi, element } = await initScript(script);
|
|
3384
|
+
const input = element.querySelector("input") as HTMLInputElement;
|
|
3385
|
+
input.value = "Ann";
|
|
3386
|
+
await pressEnter(input);
|
|
3387
|
+
|
|
3388
|
+
expect(squiffyApi.get("_section")).toBe("greet");
|
|
3389
|
+
expect(getSectionContent(element, "greet")).toContain("Hello, Ann!");
|
|
3390
|
+
expect(squiffyApi.get("turns")).toBe(1);
|
|
3391
|
+
expect(input.disabled).toBe(true);
|
|
3392
|
+
|
|
3393
|
+
squiffyApi.goBack();
|
|
3394
|
+
expect(squiffyApi.get("_section")).toBe("start");
|
|
3395
|
+
expect(squiffyApi.get("name")).toBe(null);
|
|
3396
|
+
expect(input.disabled).toBe(false);
|
|
3397
|
+
});
|
|
3398
|
+
|
|
3399
|
+
test("A text box with data-passage shows the passage when Enter is pressed, once", async () => {
|
|
3400
|
+
// https://github.com/textadventures/squiffy/issues/454
|
|
3401
|
+
const script = `
|
|
3402
|
+
[[start]]:
|
|
3403
|
+
What's your name? <input type="text" data-attribute="name" data-passage="greet">
|
|
3404
|
+
|
|
3405
|
+
[greet]:
|
|
3406
|
+
Hello, {{name}}!
|
|
3407
|
+
`;
|
|
3408
|
+
const { squiffyApi, element } = await initScript(script);
|
|
3409
|
+
const input = element.querySelector("input") as HTMLInputElement;
|
|
3410
|
+
input.value = "Ann";
|
|
3411
|
+
await pressEnter(input);
|
|
3412
|
+
|
|
3413
|
+
expect(getPassageContent(element, "start", "greet")).toContain("Hello, Ann!");
|
|
3414
|
+
expect(input.disabled).toBe(true);
|
|
3415
|
+
await pressEnter(input);
|
|
3416
|
+
expect(element.querySelectorAll(".squiffy-output-passage").length).toBe(1);
|
|
3417
|
+
|
|
3418
|
+
squiffyApi.goBack();
|
|
3419
|
+
expect(element.querySelectorAll(".squiffy-output-passage").length).toBe(0);
|
|
3420
|
+
expect(input.disabled).toBe(false);
|
|
3421
|
+
expect(squiffyApi.get("name")).toBe(null);
|
|
3422
|
+
});
|
|
3423
|
+
|
|
3424
|
+
test("A text box going to a @repeat passage can be used again", async () => {
|
|
3425
|
+
// https://github.com/textadventures/squiffy/issues/454
|
|
3426
|
+
const script = `
|
|
3427
|
+
[[start]]:
|
|
3428
|
+
Guess: <input type="text" data-attribute="guess" data-passage="check">
|
|
3429
|
+
|
|
3430
|
+
[check]:
|
|
3431
|
+
@repeat
|
|
3432
|
+
You guessed {{guess}}.
|
|
3433
|
+
`;
|
|
3434
|
+
const { element } = await initScript(script);
|
|
3435
|
+
const input = element.querySelector("input") as HTMLInputElement;
|
|
3436
|
+
input.value = "1";
|
|
3437
|
+
await pressEnter(input);
|
|
3438
|
+
input.value = "2";
|
|
3439
|
+
await pressEnter(input);
|
|
3440
|
+
|
|
3441
|
+
expect(input.disabled).toBe(false);
|
|
3442
|
+
expect(element.textContent).toContain("You guessed 1.");
|
|
3443
|
+
expect(element.textContent).toContain("You guessed 2.");
|
|
3444
|
+
});
|
|
3445
|
+
|
|
3446
|
+
test("Enter does nothing until the text boxes are filled in properly", async () => {
|
|
3447
|
+
// https://github.com/textadventures/squiffy/issues/454
|
|
3448
|
+
const script = `
|
|
3449
|
+
[[start]]:
|
|
3450
|
+
<input type="text" data-attribute="name" data-section="greet" required>
|
|
3451
|
+
|
|
3452
|
+
[[greet]]:
|
|
3453
|
+
Hello, {{name}}!
|
|
3454
|
+
`;
|
|
3455
|
+
const { squiffyApi, element } = await initScript(script);
|
|
3456
|
+
const input = element.querySelector("input") as HTMLInputElement;
|
|
3457
|
+
await pressEnter(input);
|
|
3458
|
+
expect(squiffyApi.get("_section")).toBe("start");
|
|
3459
|
+
expect(input.classList.contains("squiffy-invalid")).toBe(true);
|
|
3460
|
+
|
|
3461
|
+
input.value = "Ann";
|
|
3462
|
+
await pressEnter(input);
|
|
3463
|
+
expect(squiffyApi.get("_section")).toBe("greet");
|
|
3464
|
+
});
|
|
3465
|
+
|
|
3466
|
+
test("Enter in a text box without data-section or data-passage does nothing", async () => {
|
|
3467
|
+
const script = `
|
|
3468
|
+
[[start]]:
|
|
3469
|
+
<input type="text" data-attribute="name">
|
|
3470
|
+
|
|
3471
|
+
[[greet]]:
|
|
3472
|
+
Hello.
|
|
3473
|
+
`;
|
|
3474
|
+
const { squiffyApi, element } = await initScript(script);
|
|
3475
|
+
await pressEnter(element.querySelector("input") as HTMLElement);
|
|
3476
|
+
expect(squiffyApi.get("_section")).toBe("start");
|
|
3477
|
+
});
|
|
3478
|
+
|
|
3479
|
+
test("A text box can set a value inside an object", async () => {
|
|
3480
|
+
// https://github.com/textadventures/squiffy/issues/454
|
|
3481
|
+
const script = `
|
|
3482
|
+
[[start]]:
|
|
3483
|
+
@set hero = { name: "", hp: 10 }
|
|
3484
|
+
<input type="text" data-attribute="hero.name" data-section="greet">
|
|
3485
|
+
|
|
3486
|
+
[[greet]]:
|
|
3487
|
+
Hello, {{hero.name}}! You have {{hero.hp}} HP.
|
|
3488
|
+
`;
|
|
3489
|
+
const { squiffyApi, element } = await initScript(script, true);
|
|
3490
|
+
const input = element.querySelector("input") as HTMLInputElement;
|
|
3491
|
+
input.value = "Ann";
|
|
3492
|
+
await pressEnter(input);
|
|
3493
|
+
|
|
3494
|
+
expect(squiffyApi.get("hero")).toEqual({ name: "Ann", hp: 10 });
|
|
3495
|
+
expect(getSectionContent(element, "greet")).toContain("Hello, Ann! You have 10 HP.");
|
|
3496
|
+
|
|
3497
|
+
squiffyApi.goBack();
|
|
3498
|
+
expect(squiffyApi.get("hero")).toEqual({ name: "", hp: 10 });
|
|
3499
|
+
});
|
|
3500
|
+
|
|
2987
3501
|
// Array management helper tests
|
|
2988
3502
|
|
|
2989
3503
|
test("{{append}} helper adds element to end of array", async () => {
|
|
@@ -3290,12 +3804,9 @@ Done.
|
|
|
3290
3804
|
// Template error handling tests
|
|
3291
3805
|
|
|
3292
3806
|
test("Template error displays error message instead of failing silently", async () => {
|
|
3293
|
-
const script =
|
|
3294
|
-
[[start]]:
|
|
3295
|
-
{{invalid * syntax}}
|
|
3296
|
-
`;
|
|
3807
|
+
const script = "{{invalid * syntax}}";
|
|
3297
3808
|
|
|
3298
|
-
const { element } = await
|
|
3809
|
+
const { element } = await initBrokenText(script);
|
|
3299
3810
|
|
|
3300
3811
|
// Should display an error message
|
|
3301
3812
|
const errorDiv = element.querySelector(".squiffy-error");
|
|
@@ -3304,12 +3815,9 @@ test("Template error displays error message instead of failing silently", async
|
|
|
3304
3815
|
});
|
|
3305
3816
|
|
|
3306
3817
|
test("Template error preserves monospace styling", async () => {
|
|
3307
|
-
const script =
|
|
3308
|
-
[[start]]:
|
|
3309
|
-
{{broken (expression}}
|
|
3310
|
-
`;
|
|
3818
|
+
const script = "{{broken (expression}}";
|
|
3311
3819
|
|
|
3312
|
-
const { element } = await
|
|
3820
|
+
const { element } = await initBrokenText(script);
|
|
3313
3821
|
|
|
3314
3822
|
const errorDiv = element.querySelector(".squiffy-error") as HTMLElement;
|
|
3315
3823
|
expect(errorDiv).not.toBeNull();
|
|
@@ -3348,12 +3856,9 @@ test("Infix expressions combine with existing helpers", async () => {
|
|
|
3348
3856
|
});
|
|
3349
3857
|
|
|
3350
3858
|
test("Malformed infix expression shows a template error", async () => {
|
|
3351
|
-
const script =
|
|
3352
|
-
[[start]]:
|
|
3353
|
-
{{#if score >}}Yes{{/if}}
|
|
3354
|
-
`;
|
|
3859
|
+
const script = "{{#if score >}}Yes{{/if}}";
|
|
3355
3860
|
|
|
3356
|
-
const { element } = await
|
|
3861
|
+
const { element } = await initBrokenText(script);
|
|
3357
3862
|
const errorDiv = element.querySelector(".squiffy-error");
|
|
3358
3863
|
expect(errorDiv?.textContent).toContain("Invalid condition \"score >\": expected a value after '>'");
|
|
3359
3864
|
});
|
|
@@ -3568,6 +4073,40 @@ Darkness.
|
|
|
3568
4073
|
}
|
|
3569
4074
|
});
|
|
3570
4075
|
|
|
4076
|
+
test.each([
|
|
4077
|
+
["after=(delay * 1000)", 2000],
|
|
4078
|
+
["after=delay * 1000 + 500", 2500],
|
|
4079
|
+
['after="1.5s"', 1500],
|
|
4080
|
+
['after="250ms"', 250],
|
|
4081
|
+
["after=time", 3000],
|
|
4082
|
+
])("{{goto ... %s}} waits for a delay worked out as the story runs (#451)", async (after, ms) => {
|
|
4083
|
+
vi.useFakeTimers();
|
|
4084
|
+
try {
|
|
4085
|
+
const script = `
|
|
4086
|
+
[[start]]:
|
|
4087
|
+
@set delay = 2
|
|
4088
|
+
@set time = 3s
|
|
4089
|
+
Waiting.{{goto "later" ${after}}}
|
|
4090
|
+
|
|
4091
|
+
[[later]]:
|
|
4092
|
+
Later.
|
|
4093
|
+
`;
|
|
4094
|
+
const { squiffyApi } = await initScript(script);
|
|
4095
|
+
await vi.advanceTimersByTimeAsync(ms - 1);
|
|
4096
|
+
expect(squiffyApi.get("_section")).toBe("start");
|
|
4097
|
+
await vi.advanceTimersByTimeAsync(1);
|
|
4098
|
+
expect(squiffyApi.get("_section")).toBe("later");
|
|
4099
|
+
}
|
|
4100
|
+
finally {
|
|
4101
|
+
vi.useRealTimers();
|
|
4102
|
+
}
|
|
4103
|
+
});
|
|
4104
|
+
|
|
4105
|
+
test("{{goto ... after=}} with something that isn't a delay is an error", async () => {
|
|
4106
|
+
const { element } = await initScript("{{goto \"later\" after=\"soon\"}}\n\n[[later]]:\nLater.");
|
|
4107
|
+
expect(element.querySelector(".squiffy-error")?.textContent).toContain('after="3s"');
|
|
4108
|
+
});
|
|
4109
|
+
|
|
3571
4110
|
test("Following a link cancels a {{goto ... after=}}", async () => {
|
|
3572
4111
|
vi.useFakeTimers();
|
|
3573
4112
|
try {
|
|
@@ -4004,3 +4543,577 @@ Other.
|
|
|
4004
4543
|
expect(squiffyApi.get("x")).toBe(1);
|
|
4005
4544
|
expect(visibleText(element)).toBe("Start. next");
|
|
4006
4545
|
});
|
|
4546
|
+
|
|
4547
|
+
// Number, text and conditional helpers (#446)
|
|
4548
|
+
test.each([
|
|
4549
|
+
["{{set \"x\" (round 2.5)}}", 3],
|
|
4550
|
+
["{{set \"x\" (round -2.5)}}", -2],
|
|
4551
|
+
["{{set \"x\" (round 1.005 2)}}", 1.01],
|
|
4552
|
+
["{{set \"x\" (round 1234 -2)}}", 1200],
|
|
4553
|
+
["{{set \"x\" (round \"2.4\")}}", 2],
|
|
4554
|
+
["{{set \"x\" (floor 2.7)}}", 2],
|
|
4555
|
+
["{{set \"x\" (ceil 2.1)}}", 3],
|
|
4556
|
+
["{{set \"x\" (abs -4)}}", 4],
|
|
4557
|
+
["{{set \"x\" (min 3 1 2)}}", 1],
|
|
4558
|
+
["{{set \"x\" (max 3 1 2)}}", 3],
|
|
4559
|
+
["{{set \"x\" (max (array 4 9 2))}}", 9],
|
|
4560
|
+
["{{set \"x\" (min 5)}}", 5],
|
|
4561
|
+
["{{set \"x\" (upper \"Ann\")}}", "ANN"],
|
|
4562
|
+
["{{set \"x\" (lower \"Ann\")}}", "ann"],
|
|
4563
|
+
["{{set \"x\" (capitalise \"ann lee\")}}", "Ann lee"],
|
|
4564
|
+
["{{set \"x\" (capitalize \"ann\")}}", "Ann"],
|
|
4565
|
+
["{{set \"x\" (upper unsetattr)}}", ""],
|
|
4566
|
+
["{{set \"x\" (upper 5)}}", "5"],
|
|
4567
|
+
["{{set \"x\" (cond true \"a\" \"b\")}}", "a"],
|
|
4568
|
+
["{{set \"x\" (cond 0 \"a\" \"b\")}}", "b"],
|
|
4569
|
+
])("helper: %s", async (text, expected) => {
|
|
4570
|
+
const { squiffyApi } = await initScript(`[[start]]:\n${text}`);
|
|
4571
|
+
expect(squiffyApi.get("x")).toBe(expected);
|
|
4572
|
+
});
|
|
4573
|
+
|
|
4574
|
+
test.each([
|
|
4575
|
+
["{{= (round \"a\")}}", "\"round\" needs numbers, but was given \"a\""],
|
|
4576
|
+
["{{= (round 1 2 3)}}", "\"round\" needs one or two values, but was given 3"],
|
|
4577
|
+
["{{= (floor 1 2)}}", "\"floor\" needs one value, but was given 2"],
|
|
4578
|
+
["{{= (max)}}", "\"max\" needs at least one value"],
|
|
4579
|
+
["{{= (upper (array 1))}}", "\"upper\" needs text, but was given a list"],
|
|
4580
|
+
["{{= (cond true 1)}}", "\"cond\" needs three values, but was given 2"],
|
|
4581
|
+
])("helper error: %s", async (text, message) => {
|
|
4582
|
+
const { element } = await initScript(`[[start]]:\n${text}`);
|
|
4583
|
+
expect(element.querySelector(".squiffy-error")?.textContent).toContain(message);
|
|
4584
|
+
});
|
|
4585
|
+
|
|
4586
|
+
// List helpers (#447)
|
|
4587
|
+
test.each([
|
|
4588
|
+
["{{set \"x\" (join (array \"a\" \"b\" \"c\"))}}", "a, b, c"],
|
|
4589
|
+
["{{set \"x\" (join (array \"a\" \"b\" \"c\") \" / \")}}", "a / b / c"],
|
|
4590
|
+
["{{set \"x\" (join (array \"a\" \"b\" \"c\") \", \" \" and \")}}", "a, b and c"],
|
|
4591
|
+
["{{set \"x\" (join (array \"a\" \"b\") \", \" \" and \")}}", "a and b"],
|
|
4592
|
+
["{{set \"x\" (join (array \"a\") \", \" \" and \")}}", "a"],
|
|
4593
|
+
["{{set \"x\" (join unsetattr)}}", ""],
|
|
4594
|
+
["{{set \"x\" (join (array 1 2) \"\")}}", "12"],
|
|
4595
|
+
["{{set \"x\" (sort (array \"pear\" \"Apple\" \"item 10\" \"item 2\" 10 2))}}", [2, 10, "Apple", "item 2", "item 10", "pear"]],
|
|
4596
|
+
["{{set \"x\" (slice (array 1 2 3 4) 1 3)}}", [2, 3]],
|
|
4597
|
+
["{{set \"x\" (slice (array 1 2 3 4) 1)}}", [2, 3, 4]],
|
|
4598
|
+
["{{set \"x\" (slice (array 1 2 3 4) -2)}}", [3, 4]],
|
|
4599
|
+
["{{set \"x\" (slice \"hello\" 1 3)}}", "el"],
|
|
4600
|
+
["{{set \"x\" (first (array 1 2 3))}}", 1],
|
|
4601
|
+
["{{set \"x\" (last (array 1 2 3))}}", 3],
|
|
4602
|
+
["{{set \"x\" (first (array))}}", ""],
|
|
4603
|
+
["{{set \"x\" (indexof (array \"a\" \"b\") \"b\")}}", 1],
|
|
4604
|
+
["{{set \"x\" (indexof (array \"a\" \"b\") \"c\")}}", -1],
|
|
4605
|
+
["{{set \"x\" (indexof \"hello\" \"l\")}}", 2],
|
|
4606
|
+
["{{set \"x\" (count (array \"a\" \"b\" \"a\") \"a\")}}", 2],
|
|
4607
|
+
["{{set \"x\" (count \"banana\" \"an\")}}", 2],
|
|
4608
|
+
["{{set \"x\" (range 1 5)}}", [1, 2, 3, 4, 5]],
|
|
4609
|
+
["{{set \"x\" (range 3 1)}}", [3, 2, 1]],
|
|
4610
|
+
["{{set \"x\" (range 0 0)}}", [0]],
|
|
4611
|
+
])("list helper: %s", async (text, expected) => {
|
|
4612
|
+
const { squiffyApi } = await initScript(`[[start]]:\n${text}`);
|
|
4613
|
+
expect(squiffyApi.get("x")).toEqual(expected);
|
|
4614
|
+
});
|
|
4615
|
+
|
|
4616
|
+
test.each([
|
|
4617
|
+
["{{= (sort \"bag\")}}", "\"sort\" needs a list, but was given the text \"bag\". To use an attribute's list, leave out the quotes"],
|
|
4618
|
+
["{{= (sort 5)}}", "\"sort\" needs a list, but was given 5"],
|
|
4619
|
+
["{{= (slice (array 1))}}", "\"slice\" needs 2 to 3 values, but was given 1"],
|
|
4620
|
+
["{{= (range 1 100000)}}", "\"range\" can make a list of up to 10,000 numbers, but was asked for 100000"],
|
|
4621
|
+
["{{= (join (array (array 1)))}}", "\"join\" needs text, but was given a list"],
|
|
4622
|
+
])("list helper error: %s", async (text, message) => {
|
|
4623
|
+
const { element } = await initScript(`[[start]]:\n${text}`);
|
|
4624
|
+
expect(element.querySelector(".squiffy-error")?.textContent).toContain(message);
|
|
4625
|
+
});
|
|
4626
|
+
|
|
4627
|
+
test("list helpers with attributes, in expressions and loops (#447)", async () => {
|
|
4628
|
+
const script = `
|
|
4629
|
+
[[start]]:
|
|
4630
|
+
@set bag = ["rope", "lamp", "key"]
|
|
4631
|
+
@set i = 1
|
|
4632
|
+
@set n = 3
|
|
4633
|
+
@set bag = (sort bag)
|
|
4634
|
+
@set mixed = (shuffle bag)
|
|
4635
|
+
You have {{join bag ", " " and "}}. First: {{first bag}}. Last: {{last bag}}. Item {{= i}}: {{= bag[i]}}.
|
|
4636
|
+
{{#each (range 1 3)}}n{{this}} {{/each}}
|
|
4637
|
+
{{#if (indexof bag "lamp") = 1}}Lamp second.{{/if}}
|
|
4638
|
+
{{#if (count bag "rope") > 0}}Has rope.{{/if}}
|
|
4639
|
+
Two: {{join (slice bag 0 2)}}.
|
|
4640
|
+
Down: {{#each (range 0 (n - 1))}}n{{this}} {{/each}}. Second last: {{= bag[bag.length - 2]}}.
|
|
4641
|
+
`;
|
|
4642
|
+
const { squiffyApi, element } = await initScript(script, true);
|
|
4643
|
+
expect(squiffyApi.get("bag")).toEqual(["key", "lamp", "rope"]);
|
|
4644
|
+
expect([...squiffyApi.get("mixed")].sort()).toEqual(["key", "lamp", "rope"]);
|
|
4645
|
+
expect(element.textContent).toContain("You have key, lamp and rope. First: key. Last: rope. Item 1: lamp.");
|
|
4646
|
+
expect(element.textContent).toContain("n1 n2 n3");
|
|
4647
|
+
expect(element.textContent).toContain("Lamp second.");
|
|
4648
|
+
expect(element.textContent).toContain("Has rope.");
|
|
4649
|
+
expect(element.textContent).toContain("Two: key, lamp.");
|
|
4650
|
+
expect(element.textContent).toContain("Down: n0 n1 n2 . Second last: lamp.");
|
|
4651
|
+
});
|
|
4652
|
+
|
|
4653
|
+
// An attribute can have a helper's name: {{count}} on its own shows it, rather than calling
|
|
4654
|
+
// the helper with no values (#447)
|
|
4655
|
+
test.each(["count", "first", "min", "round", "upper", "cond"])("an attribute called %s", async (name) => {
|
|
4656
|
+
const { element } = await initScript(`[[start]]:\n@set ${name} = 3\nIt's {{${name}}}, then {{= ${name} + 1}}.`, true);
|
|
4657
|
+
expect(element.textContent).toContain("It's 3, then 4.");
|
|
4658
|
+
});
|
|
4659
|
+
|
|
4660
|
+
test("contains, ? : and the new helpers in expressions (#446)", async () => {
|
|
4661
|
+
const script = `
|
|
4662
|
+
[[start]]:
|
|
4663
|
+
@set name = "Robin"
|
|
4664
|
+
@set hp = 0
|
|
4665
|
+
@set gold = 10
|
|
4666
|
+
@set bag = ["lamp", "key"]
|
|
4667
|
+
@set hero = { hp: 3 }
|
|
4668
|
+
{{set "status" hp > 0 ? "alive" : "dead"}}
|
|
4669
|
+
{{set "share" (round (gold / 3) 2)}}
|
|
4670
|
+
{{set "shout" (upper name) + "!"}}
|
|
4671
|
+
{{#if bag contains "key"}}Key.{{/if}}
|
|
4672
|
+
{{#if not bag contains "rope"}}No rope.{{/if}}
|
|
4673
|
+
{{#if name contains "ob"}}Ob.{{/if}}
|
|
4674
|
+
{{#if hero contains "hp"}}Has hp.{{/if}}
|
|
4675
|
+
{{#if unsetattr contains "x"}}Wrong.{{/if}}
|
|
4676
|
+
{{#if (contains "bag" "lamp")}}Lamp.{{/if}}
|
|
4677
|
+
Letters: {{= name.length}}. Items: {{= bag.length}}. {{= gold > 5 ? "Rich" : "Poor"}}.
|
|
4678
|
+
Lower: {{= (lower name)["length"]}}. Abs: {{= (abs (-gold))}}. Max: {{max gold 3}}.
|
|
4679
|
+
`;
|
|
4680
|
+
const { squiffyApi, element } = await initScript(script, true);
|
|
4681
|
+
expect(squiffyApi.get("status")).toBe("dead");
|
|
4682
|
+
expect(squiffyApi.get("share")).toBe(3.33);
|
|
4683
|
+
expect(squiffyApi.get("shout")).toBe("ROBIN!");
|
|
4684
|
+
expect(element.textContent).toContain("Key.");
|
|
4685
|
+
expect(element.textContent).toContain("No rope.");
|
|
4686
|
+
expect(element.textContent).toContain("Ob.");
|
|
4687
|
+
expect(element.textContent).toContain("Has hp.");
|
|
4688
|
+
expect(element.textContent).not.toContain("Wrong.");
|
|
4689
|
+
expect(element.textContent).toContain("Lamp.");
|
|
4690
|
+
expect(element.textContent).toContain("Letters: 5. Items: 2. Rich.");
|
|
4691
|
+
expect(element.textContent).toContain("Lower: 5. Abs: 10. Max: 10.");
|
|
4692
|
+
});
|
|
4693
|
+
|
|
4694
|
+
// {{#every}} (#451)
|
|
4695
|
+
describe("{{#every}}", () => {
|
|
4696
|
+
beforeEach(() => {
|
|
4697
|
+
vi.useFakeTimers();
|
|
4698
|
+
});
|
|
4699
|
+
afterEach(() => {
|
|
4700
|
+
vi.useRealTimers();
|
|
4701
|
+
});
|
|
4702
|
+
|
|
4703
|
+
const everyText = (element: HTMLElement) =>
|
|
4704
|
+
[...element.querySelectorAll(".squiffy-every")].map(el => el.textContent);
|
|
4705
|
+
|
|
4706
|
+
test("shows its text again every so often, replacing what it showed before", async () => {
|
|
4707
|
+
const { squiffyApi, element } = await initScript("@set n = 0\nTicks: {{#every 1000}}{{inc \"n\"}}{{n}}{{/every}}", true);
|
|
4708
|
+
expect(everyText(element)).toEqual([""]);
|
|
4709
|
+
await vi.advanceTimersByTimeAsync(999);
|
|
4710
|
+
expect(everyText(element)).toEqual([""]);
|
|
4711
|
+
await vi.advanceTimersByTimeAsync(1);
|
|
4712
|
+
expect(everyText(element)).toEqual(["1"]);
|
|
4713
|
+
await vi.advanceTimersByTimeAsync(2000);
|
|
4714
|
+
expect(everyText(element)).toEqual(["3"]);
|
|
4715
|
+
expect(squiffyApi.get("n")).toBe(3);
|
|
4716
|
+
});
|
|
4717
|
+
|
|
4718
|
+
test("scrolls down to the new text once its first tick has filled it in", async () => {
|
|
4719
|
+
// The page is too short, when the section is written, to scroll down to where its text
|
|
4720
|
+
// starts, as the {{#every}} is empty until it ticks. Then it's taller.
|
|
4721
|
+
const { squiffyApi, element } = await initScript("[[start]]:\nStart. [[next]]\n\n[[next]]:\n{{#every 100 times=1}}\n\nA big box.\n\n{{/every}}", true);
|
|
4722
|
+
let pageHeight = 900;
|
|
4723
|
+
Object.defineProperty(window.HTMLElement.prototype, "scrollHeight", { configurable: true, get: () => 1000 });
|
|
4724
|
+
Object.defineProperty(document.documentElement, "scrollHeight", { configurable: true, get: () => pageHeight });
|
|
4725
|
+
Object.defineProperty(document.documentElement, "scrollTop", { configurable: true, get: () => 0 });
|
|
4726
|
+
const scrollTo = vi.fn();
|
|
4727
|
+
window.scrollTo = scrollTo;
|
|
4728
|
+
|
|
4729
|
+
try {
|
|
4730
|
+
await squiffyApi.clickLink(findLink(element, "section", "next"));
|
|
4731
|
+
expect(scrollTo).toHaveBeenLastCalledWith({ top: 900 - window.innerHeight, behavior: "smooth" });
|
|
4732
|
+
|
|
4733
|
+
pageHeight = 2000;
|
|
4734
|
+
await vi.advanceTimersByTimeAsync(100);
|
|
4735
|
+
expect(scrollTo).toHaveBeenLastCalledWith({ top: 1000, behavior: "smooth" });
|
|
4736
|
+
}
|
|
4737
|
+
finally {
|
|
4738
|
+
delete (window.HTMLElement.prototype as any).scrollHeight;
|
|
4739
|
+
}
|
|
4740
|
+
});
|
|
4741
|
+
|
|
4742
|
+
test("append=true adds to what it showed, and times= stops it", async () => {
|
|
4743
|
+
const { element } = await initScript("{{#every 500 append=true times=3}}Drip. {{/every}}", true);
|
|
4744
|
+
await vi.advanceTimersByTimeAsync(5000);
|
|
4745
|
+
expect(everyText(element)).toEqual(["Drip. Drip. Drip. "]);
|
|
4746
|
+
expect(element.querySelector(".squiffy-every")!.hasAttribute("data-every-stopped")).toBe(true);
|
|
4747
|
+
});
|
|
4748
|
+
|
|
4749
|
+
test("{{stop}} and {{goto}} in its text", async () => {
|
|
4750
|
+
const script = `
|
|
4751
|
+
[[start]]:
|
|
4752
|
+
@set fuse = 3
|
|
4753
|
+
{{#every 1000}}{{dec "fuse"}}{{fuse}}...{{#if fuse <= 0}}{{stop}}{{goto "boom"}}{{/if}}{{/every}}
|
|
4754
|
+
|
|
4755
|
+
[[boom]]:
|
|
4756
|
+
BOOM.
|
|
4757
|
+
`;
|
|
4758
|
+
const { squiffyApi, element } = await initScript(script, true);
|
|
4759
|
+
await vi.advanceTimersByTimeAsync(2000);
|
|
4760
|
+
expect(squiffyApi.get("_section")).toBe("start");
|
|
4761
|
+
expect(everyText(element)).toEqual(["1..."]);
|
|
4762
|
+
await vi.advanceTimersByTimeAsync(1000);
|
|
4763
|
+
expect(squiffyApi.get("_section")).toBe("boom");
|
|
4764
|
+
expect(visibleText(element)).toBe("0... BOOM.");
|
|
4765
|
+
await vi.advanceTimersByTimeAsync(5000);
|
|
4766
|
+
expect(squiffyApi.get("fuse")).toBe(0);
|
|
4767
|
+
});
|
|
4768
|
+
|
|
4769
|
+
test.each([
|
|
4770
|
+
["delay * 100", 1000],
|
|
4771
|
+
["\"1.5s\"", 1500],
|
|
4772
|
+
["(delay * 100)", 1000],
|
|
4773
|
+
])("{{#every %s}} works out its delay as the story runs", async (delay, ms) => {
|
|
4774
|
+
const { element } = await initScript(`@set delay = 10\n{{#every ${delay}}}Tick.{{/every}}`, true);
|
|
4775
|
+
await vi.advanceTimersByTimeAsync(ms - 1);
|
|
4776
|
+
expect(everyText(element)).toEqual([""]);
|
|
4777
|
+
await vi.advanceTimersByTimeAsync(1);
|
|
4778
|
+
expect(everyText(element)).toEqual(["Tick."]);
|
|
4779
|
+
});
|
|
4780
|
+
|
|
4781
|
+
test("text with paragraphs is a block", async () => {
|
|
4782
|
+
const { element } = await initScript("{{#every 1000 times=1}}\n\nOne.\n\nTwo.\n\n{{/every}}", true);
|
|
4783
|
+
await vi.advanceTimersByTimeAsync(1000);
|
|
4784
|
+
const every = element.querySelector(".squiffy-every")!;
|
|
4785
|
+
expect(every.tagName).toBe("DIV");
|
|
4786
|
+
expect(every.querySelectorAll("p").length).toBe(2);
|
|
4787
|
+
});
|
|
4788
|
+
|
|
4789
|
+
test("nested {{#every}}s each keep their own text", async () => {
|
|
4790
|
+
const { element } = await initScript("{{#every 1000 times=1}}Outer {{#every 500 times=2}}inner{{/every}}{{/every}}", true);
|
|
4791
|
+
await vi.advanceTimersByTimeAsync(1000);
|
|
4792
|
+
expect(everyText(element)).toEqual(["Outer ", ""]);
|
|
4793
|
+
await vi.advanceTimersByTimeAsync(500);
|
|
4794
|
+
expect(everyText(element)).toEqual(["Outer inner", "inner"]);
|
|
4795
|
+
});
|
|
4796
|
+
|
|
4797
|
+
test("keeps going when a passage is shown, and stops when the player leaves the section", async () => {
|
|
4798
|
+
const script = `
|
|
4799
|
+
[[start]]:
|
|
4800
|
+
@set n = 0
|
|
4801
|
+
{{#every 1000}}{{inc "n"}}{{/every}} [Look] [[Leave]](away)
|
|
4802
|
+
|
|
4803
|
+
[Look]:
|
|
4804
|
+
You look.
|
|
4805
|
+
|
|
4806
|
+
[[away]]:
|
|
4807
|
+
Away.
|
|
4808
|
+
`;
|
|
4809
|
+
const { squiffyApi, element } = await initScript(script, true);
|
|
4810
|
+
await vi.advanceTimersByTimeAsync(1500);
|
|
4811
|
+
await squiffyApi.clickLink(findLink(element, "passage", "Look"));
|
|
4812
|
+
await vi.advanceTimersByTimeAsync(1500);
|
|
4813
|
+
expect(squiffyApi.get("n")).toBe(3);
|
|
4814
|
+
await squiffyApi.clickLink(findLink(element, "section", "Leave"));
|
|
4815
|
+
await vi.advanceTimersByTimeAsync(5000);
|
|
4816
|
+
expect(squiffyApi.get("n")).toBe(3);
|
|
4817
|
+
});
|
|
4818
|
+
|
|
4819
|
+
test("going back undoes what its text changed", async () => {
|
|
4820
|
+
const script = `
|
|
4821
|
+
[[start]]:
|
|
4822
|
+
@set n = 0
|
|
4823
|
+
[[Next]](next)
|
|
4824
|
+
|
|
4825
|
+
[[next]]:
|
|
4826
|
+
{{#every 1000}}{{inc "n"}}{{n}}{{/every}}
|
|
4827
|
+
`;
|
|
4828
|
+
const { squiffyApi, element } = await initScript(script, true);
|
|
4829
|
+
await squiffyApi.clickLink(findLink(element, "section", "Next"));
|
|
4830
|
+
await vi.advanceTimersByTimeAsync(3000);
|
|
4831
|
+
expect(squiffyApi.get("n")).toBe(3);
|
|
4832
|
+
squiffyApi.goBack();
|
|
4833
|
+
expect(squiffyApi.get("n")).toBe(0);
|
|
4834
|
+
await vi.advanceTimersByTimeAsync(3000);
|
|
4835
|
+
expect(squiffyApi.get("n")).toBe(0);
|
|
4836
|
+
});
|
|
4837
|
+
|
|
4838
|
+
test("going back from a passage undoes the ticks since, and carries on", async () => {
|
|
4839
|
+
const script = `
|
|
4840
|
+
[[start]]:
|
|
4841
|
+
@set n = 0
|
|
4842
|
+
{{#every 1000}}{{inc "n"}}{{/every}} [Look]
|
|
4843
|
+
|
|
4844
|
+
[Look]:
|
|
4845
|
+
You look.
|
|
4846
|
+
`;
|
|
4847
|
+
const { squiffyApi, element } = await initScript(script, true);
|
|
4848
|
+
await vi.advanceTimersByTimeAsync(2000);
|
|
4849
|
+
await squiffyApi.clickLink(findLink(element, "passage", "Look"));
|
|
4850
|
+
await vi.advanceTimersByTimeAsync(3000);
|
|
4851
|
+
expect(squiffyApi.get("n")).toBe(5);
|
|
4852
|
+
squiffyApi.goBack();
|
|
4853
|
+
expect(squiffyApi.get("n")).toBe(2);
|
|
4854
|
+
await vi.advanceTimersByTimeAsync(1000);
|
|
4855
|
+
expect(squiffyApi.get("n")).toBe(3);
|
|
4856
|
+
});
|
|
4857
|
+
|
|
4858
|
+
test("a saved game carries on", async () => {
|
|
4859
|
+
const script = "@set n = 0\n{{#every 1000}}{{inc \"n\"}}{{n}}{{/every}}";
|
|
4860
|
+
const element = document.getElementById("squiffy")!;
|
|
4861
|
+
const { story } = await compile(script, undefined, true);
|
|
4862
|
+
const first = await init({ element, story });
|
|
4863
|
+
await first.begin();
|
|
4864
|
+
await vi.advanceTimersByTimeAsync(2000);
|
|
4865
|
+
first.dispose();
|
|
4866
|
+
|
|
4867
|
+
element.innerHTML = "";
|
|
4868
|
+
const second = await init({ element, story });
|
|
4869
|
+
await second.begin();
|
|
4870
|
+
expect(everyText(element)).toEqual(["2"]);
|
|
4871
|
+
await vi.advanceTimersByTimeAsync(1000);
|
|
4872
|
+
expect(everyText(element)).toEqual(["3"]);
|
|
4873
|
+
expect(element.querySelector(".squiffy-every")!.getAttribute("data-every-count")).toBe("3");
|
|
4874
|
+
});
|
|
4875
|
+
|
|
4876
|
+
test("keeps going after the story is edited, and stops when it's disposed", async () => {
|
|
4877
|
+
const script = "@set n = 0\nTicks: {{#every 1000}}{{inc \"n\"}}{{/every}}";
|
|
4878
|
+
const { squiffyApi } = await initScript(script, true);
|
|
4879
|
+
await vi.advanceTimersByTimeAsync(1000);
|
|
4880
|
+
const updated = await compile(script.replace("Ticks", "Tocks"), undefined, true);
|
|
4881
|
+
squiffyApi.update(updated.story);
|
|
4882
|
+
await vi.advanceTimersByTimeAsync(1000);
|
|
4883
|
+
expect(squiffyApi.get("n")).toBe(2);
|
|
4884
|
+
squiffyApi.dispose();
|
|
4885
|
+
await vi.advanceTimersByTimeAsync(3000);
|
|
4886
|
+
expect(squiffyApi.get("n")).toBe(2);
|
|
4887
|
+
});
|
|
4888
|
+
|
|
4889
|
+
test.each([
|
|
4890
|
+
["{{#every}}x{{/every}}", "how often"],
|
|
4891
|
+
["{{#every 0}}x{{/every}}", "how often"],
|
|
4892
|
+
["{{#every \"soon\"}}x{{/every}}", "not \"soon\""],
|
|
4893
|
+
["{{#every 1000 times=0}}x{{/every}}", "times="],
|
|
4894
|
+
])("%s is an error", async (script, message) => {
|
|
4895
|
+
const { element } = await initScript(script, true);
|
|
4896
|
+
expect(element.querySelector(".squiffy-error")?.textContent).toContain(message);
|
|
4897
|
+
});
|
|
4898
|
+
});
|
|
4899
|
+
|
|
4900
|
+
describe('scroll: "element"', () => {
|
|
4901
|
+
// A story in a box 100px tall, whose text is 500px, as jsdom has no layout
|
|
4902
|
+
const boxedStory = (element: HTMLElement) => {
|
|
4903
|
+
element.style.overflowY = "auto";
|
|
4904
|
+
Object.defineProperty(element, "clientHeight", { value: 100 });
|
|
4905
|
+
Object.defineProperty(element, "scrollHeight", { value: 500 });
|
|
4906
|
+
const scrolls: ScrollToOptions[] = [];
|
|
4907
|
+
element.scrollTo = ((options: ScrollToOptions) => { scrolls.push(options); }) as typeof element.scrollTo;
|
|
4908
|
+
return scrolls;
|
|
4909
|
+
};
|
|
4910
|
+
|
|
4911
|
+
test("scrolls the story's own box, and not the page around it", async () => {
|
|
4912
|
+
const element = document.getElementById("squiffy")!;
|
|
4913
|
+
const scrolls = boxedStory(element);
|
|
4914
|
+
const scrollIntoView = vi.fn();
|
|
4915
|
+
window.Element.prototype.scrollIntoView = scrollIntoView;
|
|
4916
|
+
const windowScroll = vi.spyOn(window, "scrollTo").mockImplementation(() => {});
|
|
4917
|
+
|
|
4918
|
+
const { story } = await compile("[[start]]:\n[[Go on]](next)\n\n[[next]]:\nYou go on.\n\n[[Clear]](clear)\n\n[[clear]]:\n@clear\nA fresh page.");
|
|
4919
|
+
const squiffyApi = await init({ element, story, scroll: "element" });
|
|
4920
|
+
await squiffyApi.begin();
|
|
4921
|
+
scrolls.length = 0;
|
|
4922
|
+
|
|
4923
|
+
await squiffyApi.clickLink(findLink(element, "section", "Go on"));
|
|
4924
|
+
expect(scrolls.length).toBeGreaterThan(0);
|
|
4925
|
+
|
|
4926
|
+
scrolls.length = 0;
|
|
4927
|
+
await squiffyApi.clickLink(findLink(element, "section", "Clear"));
|
|
4928
|
+
expect(scrolls.length).toBeGreaterThan(0);
|
|
4929
|
+
|
|
4930
|
+
expect(scrollIntoView).not.toHaveBeenCalled();
|
|
4931
|
+
expect(windowScroll).not.toHaveBeenCalled();
|
|
4932
|
+
});
|
|
4933
|
+
});
|
|
4934
|
+
|
|
4935
|
+
// The CLI's --play carries on from a saved game without beginning the story first, and the
|
|
4936
|
+
// editor's preview begins at the section being edited
|
|
4937
|
+
const helperStory = `
|
|
4938
|
+
@ui
|
|
4939
|
+
window.uiRuns = (window.uiRuns || 0) + 1;
|
|
4940
|
+
squiffy.registerHelper("shout", (text) => text.toUpperCase() + "!");
|
|
4941
|
+
---
|
|
4942
|
+
|
|
4943
|
+
{{shout "one"}} [[b]]
|
|
4944
|
+
|
|
4945
|
+
[[b]]:
|
|
4946
|
+
{{shout "two"}} [[c]] [more]
|
|
4947
|
+
|
|
4948
|
+
[more]:
|
|
4949
|
+
{{shout "more"}}
|
|
4950
|
+
|
|
4951
|
+
[[c]]:
|
|
4952
|
+
{{shout "three"}}
|
|
4953
|
+
`;
|
|
4954
|
+
|
|
4955
|
+
const initHelperStory = async () => {
|
|
4956
|
+
const compiled = await compile(helperStory);
|
|
4957
|
+
const element = document.getElementById("squiffy")!;
|
|
4958
|
+
element.innerHTML = "";
|
|
4959
|
+
const api = await init({ element, story: compiled.story, scroll: "element" });
|
|
4960
|
+
return { api, element };
|
|
4961
|
+
};
|
|
4962
|
+
|
|
4963
|
+
test("@ui has run when the story begins at another section", async () => {
|
|
4964
|
+
const { api, element } = await initHelperStory();
|
|
4965
|
+
await api.begin({ section: "b", passage: "more" });
|
|
4966
|
+
expect(element.textContent).toContain("TWO!");
|
|
4967
|
+
expect(element.textContent).toContain("MORE!");
|
|
4968
|
+
expect((window as any).uiRuns).toBe(1);
|
|
4969
|
+
});
|
|
4970
|
+
|
|
4971
|
+
test("@ui has run when a saved game is loaded before the story begins, and runs once", async () => {
|
|
4972
|
+
const first = await initHelperStory();
|
|
4973
|
+
await first.api.begin({ section: "b" });
|
|
4974
|
+
const save = JSON.parse(JSON.stringify(first.api.getSave()));
|
|
4975
|
+
expect((window as any).uiRuns).toBe(1);
|
|
4976
|
+
|
|
4977
|
+
const { api, element } = await initHelperStory();
|
|
4978
|
+
expect(await api.loadSave(save)).toEqual({ success: true });
|
|
4979
|
+
expect((window as any).uiRuns).toBe(2);
|
|
4980
|
+
await api.clickLink(element.querySelector<HTMLElement>("a.squiffy-link[data-section='c']")!);
|
|
4981
|
+
expect(element.textContent).toContain("THREE!");
|
|
4982
|
+
expect(element.querySelector(".squiffy-error")).toBeNull();
|
|
4983
|
+
|
|
4984
|
+
// Loading again doesn't run it again, but restarting does, as that resets the attributes
|
|
4985
|
+
await api.loadSave(save);
|
|
4986
|
+
expect((window as any).uiRuns).toBe(2);
|
|
4987
|
+
api.restart();
|
|
4988
|
+
await vi.waitFor(() => expect(element.textContent).toContain("ONE!"));
|
|
4989
|
+
expect((window as any).uiRuns).toBe(3);
|
|
4990
|
+
});
|
|
4991
|
+
|
|
4992
|
+
describe("{{#once}} and {{#firsttime}}", () => {
|
|
4993
|
+
const lastSection = (element: HTMLElement) =>
|
|
4994
|
+
element.querySelector(".squiffy-output-section:last-child")!.textContent!.trim().replace(/\s+/g, " ");
|
|
4995
|
+
|
|
4996
|
+
test("Shows its text the first time, and the {{else}} text after that", async () => {
|
|
4997
|
+
const script = `
|
|
4998
|
+
[[room]]:
|
|
4999
|
+
{{#once}}A bat flies out.{{else}}The cave is quiet.{{/once}} [[again]](room)
|
|
5000
|
+
`;
|
|
5001
|
+
const { squiffyApi, element } = await initScript(script);
|
|
5002
|
+
expect(lastSection(element)).toBe("A bat flies out. again");
|
|
5003
|
+
await squiffyApi.clickLink(findLink(element, "section", "again", true));
|
|
5004
|
+
expect(lastSection(element)).toBe("The cave is quiet. again");
|
|
5005
|
+
await squiffyApi.clickLink(findLink(element, "section", "again", true));
|
|
5006
|
+
expect(lastSection(element)).toBe("The cave is quiet. again");
|
|
5007
|
+
});
|
|
5008
|
+
|
|
5009
|
+
test("Shows nothing after the first time when there's no {{else}}", async () => {
|
|
5010
|
+
const script = `
|
|
5011
|
+
[[room]]:
|
|
5012
|
+
Dark.{{#once}} A bat flies out.{{/once}} [[again]](room)
|
|
5013
|
+
`;
|
|
5014
|
+
const { squiffyApi, element } = await initScript(script);
|
|
5015
|
+
expect(lastSection(element)).toBe("Dark. A bat flies out. again");
|
|
5016
|
+
await squiffyApi.clickLink(findLink(element, "section", "again", true));
|
|
5017
|
+
expect(lastSection(element)).toBe("Dark. again");
|
|
5018
|
+
});
|
|
5019
|
+
|
|
5020
|
+
test("Each one remembers for itself", async () => {
|
|
5021
|
+
const script = `
|
|
5022
|
+
[[room]]:
|
|
5023
|
+
{{#if lit}}{{#once}}Light!{{else}}Lit.{{/once}}{{else}}{{#once}}Dark!{{else}}Unlit.{{/once}}{{/if}} [[again]](room) [[light]](room, lit = true)
|
|
5024
|
+
`;
|
|
5025
|
+
const { squiffyApi, element } = await initScript(script);
|
|
5026
|
+
expect(lastSection(element)).toBe("Dark! again light");
|
|
5027
|
+
await squiffyApi.clickLink(findLink(element, "section", "again", true));
|
|
5028
|
+
expect(lastSection(element)).toBe("Unlit. again light");
|
|
5029
|
+
await squiffyApi.clickLink(findLink(element, "section", "light", true));
|
|
5030
|
+
expect(lastSection(element)).toBe("Light! again light");
|
|
5031
|
+
await squiffyApi.clickLink(findLink(element, "section", "again", true));
|
|
5032
|
+
expect(lastSection(element)).toBe("Lit. again light");
|
|
5033
|
+
});
|
|
5034
|
+
|
|
5035
|
+
test("Remembers in embedded text, wherever it's embedded", async () => {
|
|
5036
|
+
const script = `
|
|
5037
|
+
[[one]]:
|
|
5038
|
+
{{embed "bell"}} [[two]]
|
|
5039
|
+
|
|
5040
|
+
[[two]]:
|
|
5041
|
+
{{embed "bell"}} [[one]]
|
|
5042
|
+
|
|
5043
|
+
[[bell]]:
|
|
5044
|
+
A bell rings.{{#once}} You've never heard it before.{{/once}}
|
|
5045
|
+
`;
|
|
5046
|
+
const { squiffyApi, element } = await initScript(script);
|
|
5047
|
+
expect(lastSection(element)).toBe("A bell rings. You've never heard it before. two");
|
|
5048
|
+
await squiffyApi.clickLink(findLink(element, "section", "two", true));
|
|
5049
|
+
expect(lastSection(element)).toBe("A bell rings. one");
|
|
5050
|
+
});
|
|
5051
|
+
|
|
5052
|
+
test("The same text in two passages is remembered separately", async () => {
|
|
5053
|
+
const script = `
|
|
5054
|
+
[[room]]:
|
|
5055
|
+
[a] [b]
|
|
5056
|
+
|
|
5057
|
+
[a]:
|
|
5058
|
+
A: {{#once}}new{{else}}old{{/once}}
|
|
5059
|
+
|
|
5060
|
+
[b]:
|
|
5061
|
+
B: {{#once}}new{{else}}old{{/once}}
|
|
5062
|
+
`;
|
|
5063
|
+
const { squiffyApi, element } = await initScript(script);
|
|
5064
|
+
await squiffyApi.clickLink(findLink(element, "passage", "a", true));
|
|
5065
|
+
await squiffyApi.clickLink(findLink(element, "passage", "b", true));
|
|
5066
|
+
expect(lastSection(element)).toBe("a bA: newB: new");
|
|
5067
|
+
});
|
|
5068
|
+
|
|
5069
|
+
test("Ones with the same name share their memory", async () => {
|
|
5070
|
+
const script = `
|
|
5071
|
+
[[one]]:
|
|
5072
|
+
{{#once "warning"}}Mind the gap.{{/once}} [[two]]
|
|
5073
|
+
|
|
5074
|
+
[[two]]:
|
|
5075
|
+
{{#once "warning"}}Mind the gap.{{else}}You mind it.{{/once}}
|
|
5076
|
+
`;
|
|
5077
|
+
const { squiffyApi, element } = await initScript(script);
|
|
5078
|
+
expect(lastSection(element)).toBe("Mind the gap. two");
|
|
5079
|
+
await squiffyApi.clickLink(findLink(element, "section", "two", true));
|
|
5080
|
+
expect(lastSection(element)).toBe("You mind it.");
|
|
5081
|
+
});
|
|
5082
|
+
|
|
5083
|
+
test("Going back undoes it", async () => {
|
|
5084
|
+
const script = `
|
|
5085
|
+
[[start]]:
|
|
5086
|
+
[[room]]
|
|
5087
|
+
|
|
5088
|
+
[[room]]:
|
|
5089
|
+
{{#once}}A bat flies out.{{else}}The cave is quiet.{{/once}}
|
|
5090
|
+
`;
|
|
5091
|
+
const { squiffyApi, element } = await initScript(script);
|
|
5092
|
+
await squiffyApi.clickLink(findLink(element, "section", "room", true));
|
|
5093
|
+
expect(lastSection(element)).toBe("A bat flies out.");
|
|
5094
|
+
squiffyApi.goBack();
|
|
5095
|
+
await squiffyApi.clickLink(findLink(element, "section", "room", true));
|
|
5096
|
+
expect(lastSection(element)).toBe("A bat flies out.");
|
|
5097
|
+
});
|
|
5098
|
+
|
|
5099
|
+
test("Helpers inside it only run the first time", async () => {
|
|
5100
|
+
const script = `
|
|
5101
|
+
[[room]]:
|
|
5102
|
+
{{#once}}{{inc "bats"}}{{/once}}Bats: {{bats}} [[again]](room)
|
|
5103
|
+
`;
|
|
5104
|
+
const { squiffyApi, element } = await initScript(script);
|
|
5105
|
+
await squiffyApi.clickLink(findLink(element, "section", "again", true));
|
|
5106
|
+
expect(lastSection(element)).toBe("Bats: 1 again");
|
|
5107
|
+
});
|
|
5108
|
+
|
|
5109
|
+
test("firsttime is another name for it", async () => {
|
|
5110
|
+
const script = `
|
|
5111
|
+
[[room]]:
|
|
5112
|
+
{{#firsttime}}A bat flies out.{{else}}The cave is quiet.{{/firsttime}} [[again]](room)
|
|
5113
|
+
`;
|
|
5114
|
+
const { squiffyApi, element } = await initScript(script);
|
|
5115
|
+
expect(lastSection(element)).toBe("A bat flies out. again");
|
|
5116
|
+
await squiffyApi.clickLink(findLink(element, "section", "again", true));
|
|
5117
|
+
expect(lastSection(element)).toBe("The cave is quiet. again");
|
|
5118
|
+
});
|
|
5119
|
+
});
|