squiffy-runtime 7.0.0-beta.7 → 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/confirmDialog.d.ts +8 -0
- package/dist/events.d.ts +10 -0
- package/dist/every.d.ts +12 -0
- package/dist/every.test.d.ts +1 -0
- package/dist/playerSettings.d.ts +17 -0
- package/dist/preview-html.js +1 -1
- package/dist/settings.test.d.ts +1 -0
- package/dist/settingsDialog.d.ts +17 -0
- package/dist/squiffy.page.css +4 -1
- package/dist/squiffy.preview.html +673 -83
- package/dist/squiffy.runtime.css +531 -1
- package/dist/squiffy.runtime.d.ts +5 -1
- package/dist/squiffy.runtime.global.js +106 -85
- package/dist/squiffy.runtime.global.js.map +1 -1
- package/dist/squiffy.runtime.js +4743 -4157
- package/dist/squiffy.runtime.js.map +1 -1
- package/dist/state.d.ts +1 -0
- package/dist/style.test.d.ts +1 -0
- package/dist/textProcessor.d.ts +8 -0
- package/dist/transitions.test.d.ts +1 -0
- package/dist/types.d.ts +43 -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/animation.test.ts +44 -0
- package/src/animation.ts +7 -3
- package/src/confirmDialog.ts +66 -0
- package/src/events.ts +17 -0
- package/src/every.test.ts +25 -0
- package/src/every.ts +94 -0
- package/src/panels.test.ts +24 -0
- package/src/playerSettings.ts +128 -0
- package/src/plugins/animate.ts +9 -0
- package/src/plugins/live.ts +3 -1
- package/src/plugins/plugins.test.ts +17 -0
- package/src/preview.test.ts +53 -3
- package/src/preview.ts +60 -1
- package/src/settings.test.ts +233 -0
- package/src/settingsDialog.ts +160 -0
- package/src/squiffy.page.css +4 -1
- package/src/squiffy.runtime.css +531 -1
- package/src/squiffy.runtime.test.ts +613 -16
- package/src/squiffy.runtime.ts +372 -19
- package/src/state.ts +16 -1
- package/src/style.test.ts +89 -0
- package/src/textProcessor.ts +141 -6
- package/src/transitions.test.ts +302 -0
- package/src/types.ts +56 -0
- package/src/uiLinks.test.ts +156 -0
- package/src/utils.test.ts +60 -0
- package/src/utils.ts +44 -14
- package/dist/infixExpressions.d.ts +0 -7
- package/src/infixExpressions.test.ts +0 -516
- package/src/infixExpressions.ts +0 -672
- /package/dist/{infixExpressions.test.d.ts → animation.test.d.ts} +0 -0
|
@@ -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}`)
|
|
@@ -2202,7 +2214,7 @@ test("arithmetic in {{set}}, {{inc}}, {{dec}} and conditions", async () => {
|
|
|
2202
2214
|
});
|
|
2203
2215
|
|
|
2204
2216
|
test("Invalid arithmetic shows a template error", async () => {
|
|
2205
|
-
const { element } = await
|
|
2217
|
+
const { element } = await initBrokenText("{{set \"gold\" gold +}}");
|
|
2206
2218
|
expect(element.querySelector(".squiffy-error")?.textContent).toContain("Invalid expression");
|
|
2207
2219
|
});
|
|
2208
2220
|
|
|
@@ -3353,6 +3365,139 @@ Level: <select data-attribute="level" required>
|
|
|
3353
3365
|
expect(getSectionContent(element, "Continue")).toContain("Hero at level 1");
|
|
3354
3366
|
});
|
|
3355
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
|
+
|
|
3356
3501
|
// Array management helper tests
|
|
3357
3502
|
|
|
3358
3503
|
test("{{append}} helper adds element to end of array", async () => {
|
|
@@ -3659,12 +3804,9 @@ Done.
|
|
|
3659
3804
|
// Template error handling tests
|
|
3660
3805
|
|
|
3661
3806
|
test("Template error displays error message instead of failing silently", async () => {
|
|
3662
|
-
const script =
|
|
3663
|
-
[[start]]:
|
|
3664
|
-
{{invalid * syntax}}
|
|
3665
|
-
`;
|
|
3807
|
+
const script = "{{invalid * syntax}}";
|
|
3666
3808
|
|
|
3667
|
-
const { element } = await
|
|
3809
|
+
const { element } = await initBrokenText(script);
|
|
3668
3810
|
|
|
3669
3811
|
// Should display an error message
|
|
3670
3812
|
const errorDiv = element.querySelector(".squiffy-error");
|
|
@@ -3673,12 +3815,9 @@ test("Template error displays error message instead of failing silently", async
|
|
|
3673
3815
|
});
|
|
3674
3816
|
|
|
3675
3817
|
test("Template error preserves monospace styling", async () => {
|
|
3676
|
-
const script =
|
|
3677
|
-
[[start]]:
|
|
3678
|
-
{{broken (expression}}
|
|
3679
|
-
`;
|
|
3818
|
+
const script = "{{broken (expression}}";
|
|
3680
3819
|
|
|
3681
|
-
const { element } = await
|
|
3820
|
+
const { element } = await initBrokenText(script);
|
|
3682
3821
|
|
|
3683
3822
|
const errorDiv = element.querySelector(".squiffy-error") as HTMLElement;
|
|
3684
3823
|
expect(errorDiv).not.toBeNull();
|
|
@@ -3717,12 +3856,9 @@ test("Infix expressions combine with existing helpers", async () => {
|
|
|
3717
3856
|
});
|
|
3718
3857
|
|
|
3719
3858
|
test("Malformed infix expression shows a template error", async () => {
|
|
3720
|
-
const script =
|
|
3721
|
-
[[start]]:
|
|
3722
|
-
{{#if score >}}Yes{{/if}}
|
|
3723
|
-
`;
|
|
3859
|
+
const script = "{{#if score >}}Yes{{/if}}";
|
|
3724
3860
|
|
|
3725
|
-
const { element } = await
|
|
3861
|
+
const { element } = await initBrokenText(script);
|
|
3726
3862
|
const errorDiv = element.querySelector(".squiffy-error");
|
|
3727
3863
|
expect(errorDiv?.textContent).toContain("Invalid condition \"score >\": expected a value after '>'");
|
|
3728
3864
|
});
|
|
@@ -3937,6 +4073,40 @@ Darkness.
|
|
|
3937
4073
|
}
|
|
3938
4074
|
});
|
|
3939
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
|
+
|
|
3940
4110
|
test("Following a link cancels a {{goto ... after=}}", async () => {
|
|
3941
4111
|
vi.useFakeTimers();
|
|
3942
4112
|
try {
|
|
@@ -4520,3 +4690,430 @@ Lower: {{= (lower name)["length"]}}. Abs: {{= (abs (-gold))}}. Max: {{max gold 3
|
|
|
4520
4690
|
expect(element.textContent).toContain("Letters: 5. Items: 2. Rich.");
|
|
4521
4691
|
expect(element.textContent).toContain("Lower: 5. Abs: 10. Max: 10.");
|
|
4522
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
|
+
});
|