squiffy-runtime 7.0.0-beta.4 → 7.0.0-beta.6

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.
Files changed (47) hide show
  1. package/dist/events.d.ts +1 -0
  2. package/dist/panels.d.ts +14 -0
  3. package/dist/panels.test.d.ts +1 -0
  4. package/dist/preview-host.d.ts +2 -1
  5. package/dist/preview-host.js +4 -1
  6. package/dist/preview-host.js.map +2 -2
  7. package/dist/preview-html.js +1 -1
  8. package/dist/preview-protocol.d.ts +4 -0
  9. package/dist/saveSlots.d.ts +16 -0
  10. package/dist/saves.test.d.ts +1 -0
  11. package/dist/savesDialog.d.ts +43 -0
  12. package/dist/savesDialog.test.d.ts +1 -0
  13. package/dist/squiffy.page.css +46 -22
  14. package/dist/squiffy.preview.html +612 -109
  15. package/dist/squiffy.runtime.css +476 -9
  16. package/dist/squiffy.runtime.d.ts +9 -1
  17. package/dist/squiffy.runtime.global.js +113 -72
  18. package/dist/squiffy.runtime.global.js.map +1 -1
  19. package/dist/squiffy.runtime.js +4146 -3460
  20. package/dist/squiffy.runtime.js.map +1 -1
  21. package/dist/state.d.ts +4 -0
  22. package/dist/textProcessor.d.ts +5 -0
  23. package/dist/types.d.ts +32 -0
  24. package/package.json +3 -3
  25. package/src/__snapshots__/squiffy.runtime.test.ts.snap +21 -21
  26. package/src/events.ts +1 -0
  27. package/src/panels.test.ts +201 -0
  28. package/src/panels.ts +187 -0
  29. package/src/plugins/plugins.test.ts +42 -0
  30. package/src/plugins/replaceLabel.ts +6 -2
  31. package/src/preview-host.test.ts +5 -1
  32. package/src/preview-host.ts +9 -3
  33. package/src/preview-protocol.ts +6 -2
  34. package/src/preview.test.ts +71 -3
  35. package/src/preview.ts +74 -13
  36. package/src/saveSlots.ts +159 -0
  37. package/src/saves.test.ts +199 -0
  38. package/src/savesDialog.test.ts +290 -0
  39. package/src/savesDialog.ts +499 -0
  40. package/src/squiffy.page.css +46 -22
  41. package/src/squiffy.runtime.css +476 -9
  42. package/src/squiffy.runtime.test.ts +890 -4
  43. package/src/squiffy.runtime.ts +322 -17
  44. package/src/state.ts +34 -4
  45. package/src/textProcessor.ts +76 -9
  46. package/src/types.ts +57 -1
  47. package/vite.preview.config.ts +7 -0
@@ -147,7 +147,7 @@ Checked.`;
147
147
  });
148
148
 
149
149
  test("Click a section link", async () => {
150
- const script = await fs.readFile("../examples/test/example.squiffy", "utf-8");
150
+ const script = await fs.readFile("../compiler/test/fixtures/test/example.squiffy", "utf-8");
151
151
  const { squiffyApi, element } = await initScript(script);
152
152
  expect(element.innerHTML).toMatchSnapshot();
153
153
 
@@ -181,7 +181,7 @@ test("Click a section link", async () => {
181
181
  });
182
182
 
183
183
  test("Click a section link and go back", async () => {
184
- const script = await fs.readFile("../examples/test/example.squiffy", "utf-8");
184
+ const script = await fs.readFile("../compiler/test/fixtures/test/example.squiffy", "utf-8");
185
185
  const { squiffyApi, element } = await initScript(script);
186
186
 
187
187
  const linkToPassage = findLink(element, "passage", "a link to a passage");
@@ -202,8 +202,50 @@ test("Click a section link and go back", async () => {
202
202
  expect(await squiffyApi.clickLink(linkToPassage)).toBe(true);
203
203
  });
204
204
 
205
+ test("canGoBack says whether there's anything to go back to", async () => {
206
+ const { squiffyApi, element } = await initScript("[[a]]:\nGo to [[b]].\n\n[[b]]:\nThe end.");
207
+ expect(squiffyApi.canGoBack()).toBe(false);
208
+
209
+ await squiffyApi.clickLink(findLink(element, "section", "b"));
210
+ expect(squiffyApi.canGoBack()).toBe(true);
211
+
212
+ squiffyApi.goBack();
213
+ expect(squiffyApi.canGoBack()).toBe(false);
214
+ });
215
+
216
+ test("canGoBack is true for a saved game that has somewhere to go back to", async () => {
217
+ const element = document.getElementById("squiffy")!;
218
+ const compileResult = await compile("[[a]]:\nGo to [[b]].\n\n[[b]]:\nThe end.");
219
+ const first = await init({ element, story: compileResult.story, persist: true });
220
+ await first.begin();
221
+ await first.clickLink(findLink(element, "section", "b"));
222
+
223
+ // A new page loads the saved game, which doesn't fire canGoBackChanged
224
+ element.innerHTML = "";
225
+ const second = await init({ element, story: compileResult.story, persist: true });
226
+ await second.begin();
227
+ expect(second.canGoBack()).toBe(true);
228
+ });
229
+
230
+ test("Going back is saved, so a saved game doesn't bring back what was undone", async () => {
231
+ const element = document.getElementById("squiffy")!;
232
+ const compileResult = await compile("[[a]]:\nGo to [[b]].\n\n[[b]]:\nLook at the [view].\n\n[view]:\nIt's lovely.");
233
+ const first = await init({ element, story: compileResult.story, persist: true });
234
+ await first.begin();
235
+ await first.clickLink(findLink(element, "section", "b"));
236
+ await first.clickLink(findLink(element, "passage", "view"));
237
+ first.goBack();
238
+ expect(element.innerHTML).not.toContain("lovely");
239
+
240
+ element.innerHTML = "";
241
+ const second = await init({ element, story: compileResult.story, persist: true });
242
+ await second.begin();
243
+ expect(element.innerHTML).toContain("Look at the");
244
+ expect(element.innerHTML).not.toContain("lovely");
245
+ });
246
+
205
247
  test("Click a passage link", async () => {
206
- const script = await fs.readFile("../examples/test/example.squiffy", "utf-8");
248
+ const script = await fs.readFile("../compiler/test/fixtures/test/example.squiffy", "utf-8");
207
249
  const { squiffyApi, element } = await initScript(script);
208
250
  expect(element.innerHTML).toMatchSnapshot();
209
251
 
@@ -238,7 +280,7 @@ test("Click a passage link", async () => {
238
280
  });
239
281
 
240
282
  test("Click a passage link and go back", async () => {
241
- const script = await fs.readFile("../examples/test/example.squiffy", "utf-8");
283
+ const script = await fs.readFile("../compiler/test/fixtures/test/example.squiffy", "utf-8");
242
284
  const { squiffyApi, element } = await initScript(script);
243
285
 
244
286
  const linkToPassage = findLink(element, "passage", "a link to a passage");
@@ -295,6 +337,132 @@ Last turn.
295
337
  expectSingleBlock("c", ["You chose c.", "Last turn."]);
296
338
  });
297
339
 
340
+ test("A passage with @repeat can be clicked again, and doesn't count towards @1 or @last", async () => {
341
+ // https://github.com/textadventures/squiffy/issues/104
342
+ const script = `
343
+ [[start]]:
344
+ Talk to the [guard], or look at the [a] or the [b].
345
+
346
+ [guard]:
347
+ @repeat
348
+ @inc talks
349
+ Talk {{talks}}.
350
+
351
+ [a]:
352
+ You look at a.
353
+
354
+ [b]:
355
+ You look at b.
356
+
357
+ [@1]:
358
+ Turn one.
359
+
360
+ [@last]:
361
+ Last turn.
362
+ `;
363
+ const { squiffyApi, element } = await initScript(script);
364
+ const passageTexts = () => Array.from(element.querySelectorAll(".squiffy-output-passage"))
365
+ .map(p => p.textContent!.trim());
366
+
367
+ const guard = findLink(element, "passage", "guard");
368
+ expect(await squiffyApi.clickLink(guard)).toBe(true);
369
+ expect(await squiffyApi.clickLink(guard)).toBe(true);
370
+ expect(guard.classList).not.toContain("disabled");
371
+ expect(squiffyApi.get("talks")).toBe(2);
372
+ // Neither click was a turn in the section, so @1 hasn't run
373
+ expect(passageTexts()).toEqual(["Talk 1.", "Talk 2."]);
374
+
375
+ await squiffyApi.clickLink(findLink(element, "passage", "a"));
376
+ expect(passageTexts().at(-1)).toBe("You look at a.Turn one.");
377
+
378
+ // @1 doesn't run again after another click on the guard
379
+ await squiffyApi.clickLink(guard);
380
+ expect(passageTexts().at(-1)).toBe("Talk 3.");
381
+
382
+ // @last doesn't wait for the guard, and doesn't run again for it
383
+ await squiffyApi.clickLink(findLink(element, "passage", "b"));
384
+ expect(passageTexts().at(-1)).toBe("You look at b.Last turn.");
385
+ await squiffyApi.clickLink(guard);
386
+ expect(passageTexts().at(-1)).toBe("Talk 4.");
387
+ expect(squiffyApi.get("turns")).toBe(6);
388
+ });
389
+
390
+ test("{{deactivate}} with no name stops a passage with @repeat, and going back undoes it", async () => {
391
+ // https://github.com/textadventures/squiffy/issues/13
392
+ const script = `
393
+ [[start]]:
394
+ Talk to the [guard]. Or [[leave]].
395
+
396
+ [guard]:
397
+ @repeat
398
+ @inc talks
399
+ {{#if talks < 2}}Move along.{{else}}The password is swordfish.{{deactivate}}{{/if}}
400
+
401
+ [[leave]]:
402
+ Bye.
403
+ `;
404
+ const { squiffyApi, element } = await initScript(script);
405
+ const guard = findLink(element, "passage", "guard");
406
+
407
+ await squiffyApi.clickLink(guard);
408
+ expect(guard.classList).not.toContain("disabled");
409
+ await squiffyApi.clickLink(guard);
410
+ expect(element.textContent).toContain("The password is swordfish.");
411
+ expect(guard.classList).toContain("disabled");
412
+ expect(await squiffyApi.clickLink(guard)).toBe(false);
413
+
414
+ squiffyApi.goBack();
415
+ expect(guard.classList).not.toContain("disabled");
416
+ expect(guard.hasAttribute("data-deactivated-by")).toBe(false);
417
+ expect(await squiffyApi.clickLink(guard)).toBe(true);
418
+ expect(guard.classList).toContain("disabled");
419
+ });
420
+
421
+ test("{{deactivate}} disables links to other passages, and going back only enables the ones it disabled", async () => {
422
+ // https://github.com/textadventures/squiffy/issues/13
423
+ const script = `
424
+ [[start]]:
425
+ You can see [a], [b], [c] and [d].
426
+
427
+ [a]:
428
+ {{deactivate "b" "c"}}A rules out B and C.
429
+
430
+ [b]:
431
+ B.
432
+
433
+ [c]:
434
+ C.
435
+
436
+ [d]:
437
+ D.
438
+ `;
439
+ const { squiffyApi, element } = await initScript(script);
440
+ const link = (name: string) => findLink(element, "passage", name);
441
+
442
+ // C is clicked first, so it's disabled already
443
+ await squiffyApi.clickLink(link("c"));
444
+ await squiffyApi.clickLink(link("a"));
445
+ expect(["a", "b", "c", "d"].map(n => link(n).classList.contains("disabled"))).toEqual([true, true, true, false]);
446
+
447
+ squiffyApi.goBack();
448
+ // A was undone, so B is back, but C stays clicked
449
+ expect(["a", "b", "c", "d"].map(n => link(n).classList.contains("disabled"))).toEqual([false, false, true, false]);
450
+ });
451
+
452
+ test("{{deactivate}} shows an error for a passage that isn't in the section, or with no name in a section", async () => {
453
+ const script = `
454
+ [[start]]:
455
+ {{deactivate}}See [a].
456
+
457
+ [a]:
458
+ {{deactivate "nowhere"}}A.
459
+ `;
460
+ const { squiffyApi, element } = await initScript(script);
461
+ expect(element.textContent).toContain("{{deactivate}} with no passage name only works in a passage");
462
+ await squiffyApi.clickLink(findLink(element, "passage", "a"));
463
+ expect(element.textContent).toContain('there is no passage called "nowhere" in this section');
464
+ });
465
+
298
466
  test("Going back to a section re-enables its inputs", async () => {
299
467
  // https://github.com/textadventures/squiffy/issues/123
300
468
  const script = `
@@ -3001,6 +3169,94 @@ Length is {{length (array "x" "y")}}.
3001
3169
  expect(element.textContent).toContain("Length is 2.");
3002
3170
  });
3003
3171
 
3172
+ // {{#each}} loops
3173
+
3174
+ test("{{#each}} lists an array, with @index, @first and {{else}}", async () => {
3175
+ const script = `
3176
+ [[start]]:
3177
+ @set gold = 3
3178
+ {{set "inventory" (array "sword" "key" "lamp")}}
3179
+ {{set "empty" (array)}}
3180
+ A: {{#each inventory}}{{#unless @first}}, {{/unless}}{{@index}} {{this}}{{/each}}.
3181
+ B: {{#each empty}}{{this}}{{else}}nothing{{/each}}.
3182
+ `;
3183
+
3184
+ const { element } = await initScript(script);
3185
+ expect(element.textContent).toContain("A: 0 sword, 1 key, 2 lamp.");
3186
+ expect(element.textContent).toContain("B: nothing.");
3187
+ });
3188
+
3189
+ test("Attributes can be read inside {{#each}}", async () => {
3190
+ const script = `
3191
+ [[start]]:
3192
+ @set gold = 3
3193
+ {{set "inventory" (array "sword" "key")}}
3194
+ A: {{#each inventory}}{{this}} {{gold}}; {{/each}}
3195
+ B: {{#each inventory}}{{../gold}}{{/each}}
3196
+ C: {{#each inventory}}{{#if gold > 2 and this = "key"}}yes{{/if}}{{/each}}
3197
+ D: {{#each inventory}}{{inc "gold"}}{{/each}}{{gold}}
3198
+ `;
3199
+
3200
+ const { element } = await initScript(script, true);
3201
+ expect(element.textContent).toContain("A: sword 3; key 3;");
3202
+ expect(element.textContent).toContain("B: 33");
3203
+ expect(element.textContent).toContain("C: yes");
3204
+ expect(element.textContent).toContain("D: 5");
3205
+ });
3206
+
3207
+ test("Links inside {{#each}} can use the item in their text and setters", async () => {
3208
+ const script = `
3209
+ [[start]]:
3210
+ @set gold = 3
3211
+ {{set "inventory" (array "sword" "key")}}
3212
+ {{#each inventory}}
3213
+ - [[Drop the {{this}}]](drop, item = "{{this}}", gold += 1)
3214
+ - [Look at the {{this}}](look, looking = "{{this}}")
3215
+ {{/each}}
3216
+
3217
+ [look]:
3218
+ You look at the {{looking}}.
3219
+
3220
+ [[drop]]:
3221
+ You dropped the {{item}}, and have {{gold}} gold.
3222
+ `;
3223
+
3224
+ const { squiffyApi, element } = await initScript(script, true);
3225
+ expect(findLink(element, "section", "Drop the sword")).toBeDefined();
3226
+
3227
+ await squiffyApi.clickLink(findLink(element, "passage", "Look at the sword"));
3228
+ expect(element.textContent).toContain("You look at the sword.");
3229
+
3230
+ await squiffyApi.clickLink(findLink(element, "section", "Drop the key"));
3231
+ expect(squiffyApi.get("item")).toBe("key");
3232
+ expect(element.textContent).toContain("You dropped the key, and have 4 gold.");
3233
+ });
3234
+
3235
+ test("A link's text can show an attribute", async () => {
3236
+ const script = `
3237
+ [[start]]:
3238
+ @set gold = 3
3239
+ [[Spend {{gold}} gold]](shop)
3240
+
3241
+ [[shop]]:
3242
+ Spent.
3243
+ `;
3244
+
3245
+ const { element } = await initScript(script, true);
3246
+ expect(findLink(element, "section", "Spend 3 gold")).toBeDefined();
3247
+ });
3248
+
3249
+ test("Names every object has, like valueOf, aren't attributes", async () => {
3250
+ const script = `
3251
+ [[start]]:
3252
+ {{#if constructor}}yes{{else}}no{{/if}}
3253
+ `;
3254
+
3255
+ const { squiffyApi, element } = await initScript(script);
3256
+ expect(element.textContent).toContain("no");
3257
+ expect(squiffyApi.get("valueOf")).toBeNull();
3258
+ });
3259
+
3004
3260
  test("Array helpers work together for game round tracking", async () => {
3005
3261
  const script = `
3006
3262
  [[start]]:
@@ -3118,3 +3374,633 @@ test("Custom helpers can be registered in @ui", async () => {
3118
3374
  expect(section?.querySelector("b")?.textContent).toBe("world");
3119
3375
  expect(section?.querySelector("i")).toBeNull();
3120
3376
  });
3377
+
3378
+ // {{goto}} (#375)
3379
+
3380
+ test("{{goto}} goes to another section after showing this one", async () => {
3381
+ const script = `
3382
+ [[start]]:
3383
+ @set health = 1
3384
+ Start. [[fight]]
3385
+
3386
+ [[fight]]:
3387
+ @set health = 0
3388
+ You fight.
3389
+ {{#if health <= 0}}{{goto "death"}}{{else}}You win. [[win]]{{/if}}
3390
+
3391
+ [[death]]:
3392
+ You died.
3393
+
3394
+ [[win]]:
3395
+ Won.
3396
+ `;
3397
+ const { squiffyApi, element } = await initScript(script, true);
3398
+ await squiffyApi.clickLink(findLink(element, "section", "fight"));
3399
+
3400
+ expect(squiffyApi.get("_section")).toBe("death");
3401
+ expect(visibleText(element)).toBe("Start. fight You fight. You died.");
3402
+ expect(element.innerHTML).not.toContain("You win.");
3403
+ });
3404
+
3405
+ test("{{goto}} isn't followed when its {{#if}} is false", async () => {
3406
+ const script = `
3407
+ [[start]]:
3408
+ @set health = 5
3409
+ {{#if health <= 0}}{{goto "death"}}{{/if}}Alive.
3410
+
3411
+ [[death]]:
3412
+ You died.
3413
+ `;
3414
+ const { squiffyApi, element } = await initScript(script, true);
3415
+ expect(squiffyApi.get("_section")).toBe("start");
3416
+ expect(visibleText(element)).toBe("Alive.");
3417
+ });
3418
+
3419
+ test("{{goto}} in the start section", async () => {
3420
+ const { squiffyApi, element } = await initScript(`Intro.{{goto "next"}}
3421
+
3422
+ [[next]]:
3423
+ Next.`);
3424
+ expect(squiffyApi.get("_section")).toBe("next");
3425
+ expect(visibleText(element)).toBe("Intro. Next.");
3426
+ });
3427
+
3428
+ test("{{goto}} with an expression, e.g. to go somewhere at random", async () => {
3429
+ const script = `
3430
+ {{goto (random (array "a" "b"))}}
3431
+
3432
+ [[a]]:
3433
+ A.
3434
+
3435
+ [[b]]:
3436
+ B.
3437
+ `;
3438
+ const { squiffyApi } = await initScript(script);
3439
+ expect(["a", "b"]).toContain(squiffyApi.get("_section"));
3440
+ });
3441
+
3442
+ test("{{goto}} in a passage", async () => {
3443
+ const script = `
3444
+ [[start]]:
3445
+ Start. [open]
3446
+
3447
+ [open]:
3448
+ @set opened
3449
+ The door opens.{{goto "outside"}}
3450
+
3451
+ [[outside]]:
3452
+ Outside.
3453
+ `;
3454
+ const { squiffyApi, element } = await initScript(script);
3455
+ await squiffyApi.clickLink(findLink(element, "passage", "open"));
3456
+ expect(squiffyApi.get("_section")).toBe("outside");
3457
+ expect(visibleText(element)).toContain("The door opens. Outside.");
3458
+ expect(squiffyApi.get("opened")).toBe(true);
3459
+ });
3460
+
3461
+ test("Only the first {{goto}} counts", async () => {
3462
+ const script = `
3463
+ {{goto "a"}}{{goto "b"}}
3464
+
3465
+ [[a]]:
3466
+ A.
3467
+
3468
+ [[b]]:
3469
+ B.
3470
+ `;
3471
+ const { squiffyApi } = await initScript(script);
3472
+ expect(squiffyApi.get("_section")).toBe("a");
3473
+ });
3474
+
3475
+ test("{{goto}} to a section that doesn't exist shows an error", async () => {
3476
+ const { squiffyApi, element } = await initScript(`@set where = nowhere
3477
+ Start.{{goto where}}`);
3478
+ expect(squiffyApi.get("_section")).toBe("_default");
3479
+ expect(element.querySelector(".squiffy-error")?.textContent).toContain("there is no section called \"nowhere\"");
3480
+ });
3481
+
3482
+ test("A {{goto}} loop is stopped and reported", async () => {
3483
+ const errors = vi.spyOn(console, "error").mockImplementation(() => {});
3484
+ const script = `
3485
+ [[a]]:
3486
+ A.{{goto "b"}}
3487
+
3488
+ [[b]]:
3489
+ B.{{goto "a"}}
3490
+ `;
3491
+ const { element } = await initScript(script);
3492
+ expect(element.querySelector(".squiffy-error")?.textContent).toContain("Is there a loop?");
3493
+ expect(errors).toHaveBeenCalled();
3494
+ errors.mockRestore();
3495
+ });
3496
+
3497
+ test("Going back skips sections and passages that went somewhere else with {{goto}}", async () => {
3498
+ const script = `
3499
+ [[start]]:
3500
+ @set x = 1
3501
+ Start. [[jump]] [open]
3502
+
3503
+ [open]:
3504
+ @set x = 3
3505
+ Open.{{goto "end"}}
3506
+
3507
+ [[jump]]:
3508
+ @set x = 2
3509
+ Jump.{{goto "end"}}
3510
+
3511
+ [[end]]:
3512
+ End.
3513
+ `;
3514
+ const { squiffyApi, element } = await initScript(script);
3515
+ let canGoBack: boolean | undefined;
3516
+ squiffyApi.on("canGoBackChanged", e => canGoBack = e.canGoBack);
3517
+
3518
+ await squiffyApi.clickLink(findLink(element, "section", "jump"));
3519
+ expect(squiffyApi.get("_section")).toBe("end");
3520
+ expect(canGoBack).toBe(true);
3521
+
3522
+ squiffyApi.goBack();
3523
+ expect(squiffyApi.get("_section")).toBe("start");
3524
+ expect(squiffyApi.get("x")).toBe(1);
3525
+ expect(visibleText(element)).toBe("Start. jump open");
3526
+ expect(canGoBack).toBe(false);
3527
+
3528
+ await squiffyApi.clickLink(findLink(element, "passage", "open"));
3529
+ expect(squiffyApi.get("_section")).toBe("end");
3530
+ squiffyApi.goBack();
3531
+ expect(squiffyApi.get("_section")).toBe("start");
3532
+ expect(squiffyApi.get("x")).toBe(1);
3533
+ expect(findLink(element, "passage", "open", true)).toBeDefined();
3534
+ });
3535
+
3536
+ test("Can't go back if the only earlier section went somewhere else with {{goto}}", async () => {
3537
+ const { squiffyApi } = await initScript(`Intro.{{goto "next"}}
3538
+
3539
+ [[next]]:
3540
+ Next.`);
3541
+ let canGoBack = true;
3542
+ squiffyApi.on("canGoBackChanged", e => canGoBack = e.canGoBack);
3543
+ squiffyApi.goBack();
3544
+ expect(squiffyApi.get("_section")).toBe("next");
3545
+ expect(canGoBack).toBe(true); // unchanged: no event
3546
+ });
3547
+
3548
+ test("{{goto ... after=}} waits", async () => {
3549
+ vi.useFakeTimers();
3550
+ try {
3551
+ const script = `
3552
+ [[start]]:
3553
+ The lights go out.{{goto "darkness" after=3000}}
3554
+
3555
+ [[darkness]]:
3556
+ Darkness.
3557
+ `;
3558
+ const { squiffyApi, element } = await initScript(script);
3559
+ expect(squiffyApi.get("_section")).toBe("start");
3560
+ await vi.advanceTimersByTimeAsync(2999);
3561
+ expect(squiffyApi.get("_section")).toBe("start");
3562
+ await vi.advanceTimersByTimeAsync(1);
3563
+ expect(squiffyApi.get("_section")).toBe("darkness");
3564
+ expect(visibleText(element)).toBe("The lights go out. Darkness.");
3565
+ }
3566
+ finally {
3567
+ vi.useRealTimers();
3568
+ }
3569
+ });
3570
+
3571
+ test("Following a link cancels a {{goto ... after=}}", async () => {
3572
+ vi.useFakeTimers();
3573
+ try {
3574
+ const script = `
3575
+ [[start]]:
3576
+ Quick! [[run]]{{goto "caught" after=3000}}
3577
+
3578
+ [[run]]:
3579
+ You got away.
3580
+
3581
+ [[caught]]:
3582
+ Caught.
3583
+ `;
3584
+ const { squiffyApi, element } = await initScript(script);
3585
+ await squiffyApi.clickLink(findLink(element, "section", "run"));
3586
+ await vi.advanceTimersByTimeAsync(5000);
3587
+ expect(squiffyApi.get("_section")).toBe("run");
3588
+ }
3589
+ finally {
3590
+ vi.useRealTimers();
3591
+ }
3592
+ });
3593
+
3594
+ test("Going back cancels a {{goto ... after=}}", async () => {
3595
+ vi.useFakeTimers();
3596
+ try {
3597
+ const script = `
3598
+ [[start]]:
3599
+ Start. [[wait]]
3600
+
3601
+ [[wait]]:
3602
+ Waiting.{{goto "later" after=3000}}
3603
+
3604
+ [[later]]:
3605
+ Later.
3606
+ `;
3607
+ const { squiffyApi, element } = await initScript(script);
3608
+ await squiffyApi.clickLink(findLink(element, "section", "wait"));
3609
+ squiffyApi.goBack();
3610
+ expect(squiffyApi.get("_section")).toBe("start");
3611
+ await vi.advanceTimersByTimeAsync(5000);
3612
+ expect(squiffyApi.get("_section")).toBe("start");
3613
+ }
3614
+ finally {
3615
+ vi.useRealTimers();
3616
+ }
3617
+ });
3618
+
3619
+ test("Loading a story saved during a {{goto ... after=}} carries on waiting", async () => {
3620
+ vi.useFakeTimers();
3621
+ try {
3622
+ const script = `
3623
+ [[start]]:
3624
+ Waiting.{{goto "later" after=3000}}
3625
+
3626
+ [[later]]:
3627
+ Later.
3628
+ `;
3629
+ const element = document.getElementById("squiffy")!;
3630
+ const { story } = await compile(script);
3631
+ const first = await init({ element, story });
3632
+ await first.begin();
3633
+ expect(first.get("_section")).toBe("start");
3634
+
3635
+ element.innerHTML = "";
3636
+ const second = await init({ element, story });
3637
+ await second.begin();
3638
+ expect(second.get("_section")).toBe("start");
3639
+ await vi.advanceTimersByTimeAsync(3000);
3640
+ expect(second.get("_section")).toBe("later");
3641
+ }
3642
+ finally {
3643
+ vi.useRealTimers();
3644
+ }
3645
+ });
3646
+
3647
+ test("{{goto}} in the master section, leaving out the section it goes to", async () => {
3648
+ const script = `
3649
+ [[]]:
3650
+ {{#if health <= 0 and not (at "death")}}{{goto "death"}}{{/if}}
3651
+
3652
+ [[start]]:
3653
+ @set health = 1
3654
+ Start. [[Fight]](fight, health -= 1)
3655
+
3656
+ [[fight]]:
3657
+ Ouch.
3658
+
3659
+ [[death]]:
3660
+ You died.
3661
+ `;
3662
+ const { squiffyApi, element } = await initScript(script, true);
3663
+ await squiffyApi.clickLink(findLink(element, "section", "Fight"));
3664
+ expect(squiffyApi.get("_section")).toBe("death");
3665
+ expect(element.querySelector(".squiffy-error")).toBeNull();
3666
+ });
3667
+
3668
+ // Visit counts and the story-wide turn count (#377)
3669
+
3670
+ test("{{visits}} counts visits to sections and passages", async () => {
3671
+ const script = `
3672
+ [[start]]:
3673
+ Start. [look] [[cave]]
3674
+
3675
+ [look]:
3676
+ You look. Looked {{visits "look"}} times.
3677
+
3678
+ [[cave]]:
3679
+ Cave visit {{visits "cave"}}. Start {{visits "start"}}, nowhere {{visits "nowhere"}}. [[start]]
3680
+ `;
3681
+ const { squiffyApi, element } = await initScript(script);
3682
+ expect(squiffyApi.get("_visits")).toEqual({ start: 1 });
3683
+
3684
+ await squiffyApi.clickLink(findLink(element, "passage", "look"));
3685
+ expect(visibleText(element)).toContain("Looked 1 times.");
3686
+
3687
+ await squiffyApi.clickLink(findLink(element, "section", "cave"));
3688
+ expect(visibleText(element)).toContain("Cave visit 1. Start 1, nowhere 0.");
3689
+
3690
+ await squiffyApi.clickLink(findLink(element, "section", "start", true));
3691
+ await squiffyApi.clickLink(findLink(element, "passage", "look", true));
3692
+ expect(visibleText(element)).toContain("Looked 2 times.");
3693
+ await squiffyApi.clickLink(findLink(element, "section", "cave", true));
3694
+ expect(visibleText(element)).toContain("Cave visit 2. Start 2, nowhere 0.");
3695
+ });
3696
+
3697
+ for (const expressions of [false, true]) {
3698
+ test(`{{visits}} works in conditions${expressions ? " in a story with a squiffy.json" : ""}`, async () => {
3699
+ const script = `
3700
+ [[start]]:
3701
+ [[cave]]
3702
+
3703
+ [[cave]]:
3704
+ {{#if (visits "cave") > 1}}Back again.{{else}}A new cave.{{/if}} [[start]]
3705
+ `;
3706
+ const { squiffyApi, element } = await initScript(script, expressions);
3707
+ await squiffyApi.clickLink(findLink(element, "section", "cave"));
3708
+ expect(visibleText(element)).toContain("A new cave.");
3709
+ await squiffyApi.clickLink(findLink(element, "section", "start", true));
3710
+ await squiffyApi.clickLink(findLink(element, "section", "cave", true));
3711
+ expect(visibleText(element)).toContain("Back again.");
3712
+ });
3713
+ }
3714
+
3715
+ test("squiffy.story.visits() in JS", async () => {
3716
+ // Note: the 4-space indent on the JS lines is the squiffy JS syntax - don't trim it!
3717
+ const script = `Click [a] then [check].
3718
+
3719
+ [a]:
3720
+ Passage a.
3721
+
3722
+ [check]:
3723
+ squiffy.set("visits_a", squiffy.story.visits("a"));
3724
+ squiffy.set("visits_b", squiffy.story.visits("b"));
3725
+ Checked.`;
3726
+ const { squiffyApi, element } = await initScript(script);
3727
+ await squiffyApi.clickLink(findLink(element, "passage", "a"));
3728
+ await squiffyApi.clickLink(findLink(element, "passage", "check"));
3729
+ expect(squiffyApi.get("visits_a")).toBe(1);
3730
+ expect(squiffyApi.get("visits_b")).toBe(0);
3731
+ });
3732
+
3733
+ test("turns counts every section and passage link clicked, across sections", async () => {
3734
+ const script = `
3735
+ [[start]]:
3736
+ Turn {{turns}}. [a] [b] [[next]]
3737
+
3738
+ [a]:
3739
+ A at turn {{turns}}, section turn {{_turncount}}.
3740
+
3741
+ [b]:
3742
+ B.
3743
+
3744
+ [[next]]:
3745
+ Next at turn {{turns}}. [c]
3746
+
3747
+ [c]:
3748
+ C at turn {{turns}}, section turn {{_turncount}}.
3749
+ `;
3750
+ const { squiffyApi, element } = await initScript(script);
3751
+ expect(visibleText(element)).toContain("Turn 0.");
3752
+ await squiffyApi.clickLink(findLink(element, "passage", "a"));
3753
+ expect(visibleText(element)).toContain("A at turn 1, section turn 1.");
3754
+ await squiffyApi.clickLink(findLink(element, "passage", "b"));
3755
+ await squiffyApi.clickLink(findLink(element, "section", "next"));
3756
+ expect(visibleText(element)).toContain("Next at turn 3.");
3757
+ await squiffyApi.clickLink(findLink(element, "passage", "c"));
3758
+ expect(visibleText(element)).toContain("C at turn 4, section turn 1.");
3759
+ expect(squiffyApi.get("turns")).toBe(4);
3760
+ });
3761
+
3762
+ test("turns works in conditions", async () => {
3763
+ const script = `
3764
+ [[start]]:
3765
+ [a] [b] [c]
3766
+
3767
+ [a]:
3768
+ A.
3769
+
3770
+ [b]:
3771
+ B.
3772
+
3773
+ [c]:
3774
+ {{#if turns > 2}}Late.{{else}}Early.{{/if}}
3775
+ `;
3776
+ const { squiffyApi, element } = await initScript(script, true);
3777
+ await squiffyApi.clickLink(findLink(element, "passage", "a"));
3778
+ await squiffyApi.clickLink(findLink(element, "passage", "b"));
3779
+ await squiffyApi.clickLink(findLink(element, "passage", "c"));
3780
+ expect(visibleText(element)).toContain("Late.");
3781
+ });
3782
+
3783
+ test("{{goto}} counts as a visit but not a turn", async () => {
3784
+ const script = `
3785
+ [[start]]:
3786
+ [[fight]]
3787
+
3788
+ [[fight]]:
3789
+ {{goto "death"}}
3790
+
3791
+ [[death]]:
3792
+ Died at turn {{turns}}, visit {{visits "death"}}.
3793
+ `;
3794
+ const { squiffyApi, element } = await initScript(script);
3795
+ await squiffyApi.clickLink(findLink(element, "section", "fight"));
3796
+ expect(squiffyApi.get("_section")).toBe("death");
3797
+ expect(visibleText(element)).toContain("Died at turn 1, visit 1.");
3798
+ expect(squiffyApi.get("_visits")).toEqual({ start: 1, fight: 1, death: 1 });
3799
+ });
3800
+
3801
+ test("Going back undoes visit counts and turns", async () => {
3802
+ const script = `
3803
+ [[start]]:
3804
+ [a] [[next]]
3805
+
3806
+ [a]:
3807
+ A.
3808
+
3809
+ [[next]]:
3810
+ Next.
3811
+ `;
3812
+ const { squiffyApi, element } = await initScript(script);
3813
+ await squiffyApi.clickLink(findLink(element, "passage", "a"));
3814
+ await squiffyApi.clickLink(findLink(element, "section", "next"));
3815
+ expect(squiffyApi.get("turns")).toBe(2);
3816
+ expect(squiffyApi.get("_visits")).toEqual({ start: 1, a: 1, next: 1 });
3817
+
3818
+ squiffyApi.goBack();
3819
+ expect(squiffyApi.get("turns")).toBe(1);
3820
+ expect(squiffyApi.get("_visits")).toEqual({ start: 1, a: 1 });
3821
+
3822
+ squiffyApi.goBack();
3823
+ expect(squiffyApi.get("turns")).toBe(0);
3824
+ expect(squiffyApi.get("_visits")).toEqual({ start: 1 });
3825
+
3826
+ await squiffyApi.clickLink(findLink(element, "passage", "a", true));
3827
+ expect(squiffyApi.get("_visits")).toEqual({ start: 1, a: 1 });
3828
+ });
3829
+
3830
+ test("A save from before visit counts treats each seen section or passage as visited once", async () => {
3831
+ const script = `
3832
+ [[start]]:
3833
+ Start {{visits "start"}}, cave {{visits "cave"}}, lake {{visits "lake"}}, turns {{turns}}.
3834
+ `;
3835
+ const storyId = "00000000-0000-0000-0000-000000000000";
3836
+ localStorage[`${storyId}-_seen_sections`] = JSON.stringify(["start", "cave"]);
3837
+
3838
+ const element = document.getElementById("squiffy")!;
3839
+ const compileResult = await compile(script, "old-save.squiffy");
3840
+ const squiffyApi = await init({ element, story: compileResult.story, persist: true, storyId });
3841
+ await squiffyApi.begin();
3842
+
3843
+ // Going to start makes it the second visit
3844
+ expect(visibleText(element)).toContain("Start 2, cave 1, lake 0, turns 0.");
3845
+ expect(squiffyApi.get("_visits")).toEqual({ start: 2 });
3846
+
3847
+ localStorage.clear();
3848
+ });
3849
+
3850
+
3851
+ test("previous and previous_section say where the player was before (#56)", async () => {
3852
+ const script = `
3853
+ [[start]]:
3854
+ Start, previous "{{previous}}". [a] [b] [[cave]]
3855
+
3856
+ [a]:
3857
+ A after {{previous}}.
3858
+
3859
+ [b]:
3860
+ B after {{previous}}.
3861
+
3862
+ [[cave]]:
3863
+ Cave after {{previous}}, from section {{previous_section}}. [c]
3864
+
3865
+ [c]:
3866
+ C after {{previous}}, from section {{previous_section}}.
3867
+ `;
3868
+ const { squiffyApi, element } = await initScript(script);
3869
+ expect(visibleText(element)).toContain("Start, previous \"\".");
3870
+ expect(squiffyApi.get("previous")).toBeNull();
3871
+ await squiffyApi.clickLink(findLink(element, "passage", "a"));
3872
+ expect(visibleText(element)).toContain("A after start.");
3873
+ await squiffyApi.clickLink(findLink(element, "passage", "b"));
3874
+ expect(visibleText(element)).toContain("B after a.");
3875
+ await squiffyApi.clickLink(findLink(element, "section", "cave"));
3876
+ expect(visibleText(element)).toContain("Cave after b, from section start.");
3877
+ await squiffyApi.clickLink(findLink(element, "passage", "c"));
3878
+ expect(visibleText(element)).toContain("C after cave, from section start.");
3879
+ });
3880
+
3881
+ test("previous works in conditions", async () => {
3882
+ const script = `
3883
+ [[start]]:
3884
+ [[river]] [[forest]]
3885
+
3886
+ [[river]]:
3887
+ [[cave]]
3888
+
3889
+ [[forest]]:
3890
+ [[cave]]
3891
+
3892
+ [[cave]]:
3893
+ {{#if previous_section = "river"}}You squeeze out of the water.{{else}}You step in from the trees.{{/if}}
3894
+ `;
3895
+ const { squiffyApi, element } = await initScript(script, true);
3896
+ await squiffyApi.clickLink(findLink(element, "section", "river"));
3897
+ await squiffyApi.clickLink(findLink(element, "section", "cave"));
3898
+ expect(visibleText(element)).toContain("You squeeze out of the water.");
3899
+ });
3900
+
3901
+ test("A section reached with {{goto}} has the section before it as previous", async () => {
3902
+ const script = `
3903
+ [[start]]:
3904
+ [[fight]]
3905
+
3906
+ [[fight]]:
3907
+ {{goto "death"}}
3908
+
3909
+ [[death]]:
3910
+ Died after {{previous}}.
3911
+ `;
3912
+ const { squiffyApi, element } = await initScript(script);
3913
+ await squiffyApi.clickLink(findLink(element, "section", "fight"));
3914
+ expect(visibleText(element)).toContain("Died after fight.");
3915
+ expect(squiffyApi.get("previous_section")).toBe("fight");
3916
+ });
3917
+
3918
+ test("Going back undoes previous and previous_section", async () => {
3919
+ const script = `
3920
+ [[start]]:
3921
+ [a] [[next]]
3922
+
3923
+ [a]:
3924
+ A.
3925
+
3926
+ [[next]]:
3927
+ Next.
3928
+ `;
3929
+ const { squiffyApi, element } = await initScript(script);
3930
+ await squiffyApi.clickLink(findLink(element, "passage", "a"));
3931
+ await squiffyApi.clickLink(findLink(element, "section", "next"));
3932
+ expect(squiffyApi.get("previous")).toBe("a");
3933
+ expect(squiffyApi.get("previous_section")).toBe("start");
3934
+
3935
+ squiffyApi.goBack();
3936
+ expect(squiffyApi.get("previous")).toBe("start");
3937
+ expect(squiffyApi.get("previous_section")).toBeNull();
3938
+
3939
+ squiffyApi.goBack();
3940
+ expect(squiffyApi.get("previous")).toBeNull();
3941
+ });
3942
+
3943
+ test("squiffy.ui.afterWrite runs once the text and its {{...}} are on the page", async () => {
3944
+ // https://github.com/textadventures/squiffy/issues/106
3945
+ // Note: the 4-space indent on the JS lines is the squiffy JS syntax - don't trim it!
3946
+ const script = `
3947
+ [[start]]:
3948
+ @set n = 5
3949
+ set("order", "before");
3950
+ squiffy.ui.afterWrite(() => {
3951
+ set("shown", document.getElementById("count").textContent);
3952
+ set("order", get("order") + ",after");
3953
+ });
3954
+ set("order", get("order") + ",js");
3955
+ Count: <span id="count">{{n}}</span> [a] [[next]]
3956
+
3957
+ [a]:
3958
+ squiffy.ui.afterWrite(() => set("passage_shown", document.getElementById("passage").textContent));
3959
+ <span id="passage">{{n}} again</span>
3960
+
3961
+ [[next]]:
3962
+ Next.
3963
+ `;
3964
+ const { squiffyApi, element } = await initScript(script);
3965
+ expect(squiffyApi.get("shown")).toBe("5");
3966
+ expect(squiffyApi.get("order")).toBe("before,js,after");
3967
+
3968
+ await squiffyApi.clickLink(findLink(element, "passage", "a"));
3969
+ expect(squiffyApi.get("passage_shown")).toBe("5 again");
3970
+
3971
+ // What an afterWrite function changes is part of the turn, so going back undoes it
3972
+ squiffyApi.goBack();
3973
+ expect(squiffyApi.get("passage_shown")).toBeNull();
3974
+ expect(squiffyApi.get("shown")).toBe("5");
3975
+ });
3976
+
3977
+ test("squiffy.story.go() in squiffy.ui.afterWrite goes there once the section has finished", async () => {
3978
+ // https://github.com/textadventures/squiffy/issues/106
3979
+ // Note: the 4-space indent on the JS lines is the squiffy JS syntax - don't trim it!
3980
+ const script = `
3981
+ [[start]]:
3982
+ @set x = 1
3983
+ Start. [[next]]
3984
+
3985
+ [[next]]:
3986
+ @set x = 2
3987
+ squiffy.ui.afterWrite(() => squiffy.story.go("other"));
3988
+ squiffy.ui.afterWrite(() => set("ran", true));
3989
+ Next.
3990
+
3991
+ [[other]]:
3992
+ @set x = 3
3993
+ Other.
3994
+ `;
3995
+ const { squiffyApi, element } = await initScript(script);
3996
+ await squiffyApi.clickLink(findLink(element, "section", "next"));
3997
+ expect(squiffyApi.get("_section")).toBe("other");
3998
+ expect(squiffyApi.get("ran")).toBe(true);
3999
+ expect(visibleText(element)).toBe("Start. next Next. Other.");
4000
+
4001
+ // Going back steps back through both sections, as with {{goto}}
4002
+ squiffyApi.goBack();
4003
+ expect(squiffyApi.get("_section")).toBe("start");
4004
+ expect(squiffyApi.get("x")).toBe(1);
4005
+ expect(visibleText(element)).toBe("Start. next");
4006
+ });