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

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.
@@ -202,6 +202,48 @@ 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
248
  const script = await fs.readFile("../examples/test/example.squiffy", "utf-8");
207
249
  const { squiffyApi, element } = await initScript(script);
@@ -3118,3 +3160,476 @@ test("Custom helpers can be registered in @ui", async () => {
3118
3160
  expect(section?.querySelector("b")?.textContent).toBe("world");
3119
3161
  expect(section?.querySelector("i")).toBeNull();
3120
3162
  });
3163
+
3164
+ // {{goto}} (#375)
3165
+
3166
+ test("{{goto}} goes to another section after showing this one", async () => {
3167
+ const script = `
3168
+ [[start]]:
3169
+ @set health = 1
3170
+ Start. [[fight]]
3171
+
3172
+ [[fight]]:
3173
+ @set health = 0
3174
+ You fight.
3175
+ {{#if health <= 0}}{{goto "death"}}{{else}}You win. [[win]]{{/if}}
3176
+
3177
+ [[death]]:
3178
+ You died.
3179
+
3180
+ [[win]]:
3181
+ Won.
3182
+ `;
3183
+ const { squiffyApi, element } = await initScript(script, true);
3184
+ await squiffyApi.clickLink(findLink(element, "section", "fight"));
3185
+
3186
+ expect(squiffyApi.get("_section")).toBe("death");
3187
+ expect(visibleText(element)).toBe("Start. fight You fight. You died.");
3188
+ expect(element.innerHTML).not.toContain("You win.");
3189
+ });
3190
+
3191
+ test("{{goto}} isn't followed when its {{#if}} is false", async () => {
3192
+ const script = `
3193
+ [[start]]:
3194
+ @set health = 5
3195
+ {{#if health <= 0}}{{goto "death"}}{{/if}}Alive.
3196
+
3197
+ [[death]]:
3198
+ You died.
3199
+ `;
3200
+ const { squiffyApi, element } = await initScript(script, true);
3201
+ expect(squiffyApi.get("_section")).toBe("start");
3202
+ expect(visibleText(element)).toBe("Alive.");
3203
+ });
3204
+
3205
+ test("{{goto}} in the start section", async () => {
3206
+ const { squiffyApi, element } = await initScript(`Intro.{{goto "next"}}
3207
+
3208
+ [[next]]:
3209
+ Next.`);
3210
+ expect(squiffyApi.get("_section")).toBe("next");
3211
+ expect(visibleText(element)).toBe("Intro. Next.");
3212
+ });
3213
+
3214
+ test("{{goto}} with an expression, e.g. to go somewhere at random", async () => {
3215
+ const script = `
3216
+ {{goto (random (array "a" "b"))}}
3217
+
3218
+ [[a]]:
3219
+ A.
3220
+
3221
+ [[b]]:
3222
+ B.
3223
+ `;
3224
+ const { squiffyApi } = await initScript(script);
3225
+ expect(["a", "b"]).toContain(squiffyApi.get("_section"));
3226
+ });
3227
+
3228
+ test("{{goto}} in a passage", async () => {
3229
+ const script = `
3230
+ [[start]]:
3231
+ Start. [open]
3232
+
3233
+ [open]:
3234
+ @set opened
3235
+ The door opens.{{goto "outside"}}
3236
+
3237
+ [[outside]]:
3238
+ Outside.
3239
+ `;
3240
+ const { squiffyApi, element } = await initScript(script);
3241
+ await squiffyApi.clickLink(findLink(element, "passage", "open"));
3242
+ expect(squiffyApi.get("_section")).toBe("outside");
3243
+ expect(visibleText(element)).toContain("The door opens. Outside.");
3244
+ expect(squiffyApi.get("opened")).toBe(true);
3245
+ });
3246
+
3247
+ test("Only the first {{goto}} counts", async () => {
3248
+ const script = `
3249
+ {{goto "a"}}{{goto "b"}}
3250
+
3251
+ [[a]]:
3252
+ A.
3253
+
3254
+ [[b]]:
3255
+ B.
3256
+ `;
3257
+ const { squiffyApi } = await initScript(script);
3258
+ expect(squiffyApi.get("_section")).toBe("a");
3259
+ });
3260
+
3261
+ test("{{goto}} to a section that doesn't exist shows an error", async () => {
3262
+ const { squiffyApi, element } = await initScript(`@set where = nowhere
3263
+ Start.{{goto where}}`);
3264
+ expect(squiffyApi.get("_section")).toBe("_default");
3265
+ expect(element.querySelector(".squiffy-error")?.textContent).toContain("there is no section called \"nowhere\"");
3266
+ });
3267
+
3268
+ test("A {{goto}} loop is stopped and reported", async () => {
3269
+ const errors = vi.spyOn(console, "error").mockImplementation(() => {});
3270
+ const script = `
3271
+ [[a]]:
3272
+ A.{{goto "b"}}
3273
+
3274
+ [[b]]:
3275
+ B.{{goto "a"}}
3276
+ `;
3277
+ const { element } = await initScript(script);
3278
+ expect(element.querySelector(".squiffy-error")?.textContent).toContain("Is there a loop?");
3279
+ expect(errors).toHaveBeenCalled();
3280
+ errors.mockRestore();
3281
+ });
3282
+
3283
+ test("Going back skips sections and passages that went somewhere else with {{goto}}", async () => {
3284
+ const script = `
3285
+ [[start]]:
3286
+ @set x = 1
3287
+ Start. [[jump]] [open]
3288
+
3289
+ [open]:
3290
+ @set x = 3
3291
+ Open.{{goto "end"}}
3292
+
3293
+ [[jump]]:
3294
+ @set x = 2
3295
+ Jump.{{goto "end"}}
3296
+
3297
+ [[end]]:
3298
+ End.
3299
+ `;
3300
+ const { squiffyApi, element } = await initScript(script);
3301
+ let canGoBack: boolean | undefined;
3302
+ squiffyApi.on("canGoBackChanged", e => canGoBack = e.canGoBack);
3303
+
3304
+ await squiffyApi.clickLink(findLink(element, "section", "jump"));
3305
+ expect(squiffyApi.get("_section")).toBe("end");
3306
+ expect(canGoBack).toBe(true);
3307
+
3308
+ squiffyApi.goBack();
3309
+ expect(squiffyApi.get("_section")).toBe("start");
3310
+ expect(squiffyApi.get("x")).toBe(1);
3311
+ expect(visibleText(element)).toBe("Start. jump open");
3312
+ expect(canGoBack).toBe(false);
3313
+
3314
+ await squiffyApi.clickLink(findLink(element, "passage", "open"));
3315
+ expect(squiffyApi.get("_section")).toBe("end");
3316
+ squiffyApi.goBack();
3317
+ expect(squiffyApi.get("_section")).toBe("start");
3318
+ expect(squiffyApi.get("x")).toBe(1);
3319
+ expect(findLink(element, "passage", "open", true)).toBeDefined();
3320
+ });
3321
+
3322
+ test("Can't go back if the only earlier section went somewhere else with {{goto}}", async () => {
3323
+ const { squiffyApi } = await initScript(`Intro.{{goto "next"}}
3324
+
3325
+ [[next]]:
3326
+ Next.`);
3327
+ let canGoBack = true;
3328
+ squiffyApi.on("canGoBackChanged", e => canGoBack = e.canGoBack);
3329
+ squiffyApi.goBack();
3330
+ expect(squiffyApi.get("_section")).toBe("next");
3331
+ expect(canGoBack).toBe(true); // unchanged: no event
3332
+ });
3333
+
3334
+ test("{{goto ... after=}} waits", async () => {
3335
+ vi.useFakeTimers();
3336
+ try {
3337
+ const script = `
3338
+ [[start]]:
3339
+ The lights go out.{{goto "darkness" after=3000}}
3340
+
3341
+ [[darkness]]:
3342
+ Darkness.
3343
+ `;
3344
+ const { squiffyApi, element } = await initScript(script);
3345
+ expect(squiffyApi.get("_section")).toBe("start");
3346
+ await vi.advanceTimersByTimeAsync(2999);
3347
+ expect(squiffyApi.get("_section")).toBe("start");
3348
+ await vi.advanceTimersByTimeAsync(1);
3349
+ expect(squiffyApi.get("_section")).toBe("darkness");
3350
+ expect(visibleText(element)).toBe("The lights go out. Darkness.");
3351
+ }
3352
+ finally {
3353
+ vi.useRealTimers();
3354
+ }
3355
+ });
3356
+
3357
+ test("Following a link cancels a {{goto ... after=}}", async () => {
3358
+ vi.useFakeTimers();
3359
+ try {
3360
+ const script = `
3361
+ [[start]]:
3362
+ Quick! [[run]]{{goto "caught" after=3000}}
3363
+
3364
+ [[run]]:
3365
+ You got away.
3366
+
3367
+ [[caught]]:
3368
+ Caught.
3369
+ `;
3370
+ const { squiffyApi, element } = await initScript(script);
3371
+ await squiffyApi.clickLink(findLink(element, "section", "run"));
3372
+ await vi.advanceTimersByTimeAsync(5000);
3373
+ expect(squiffyApi.get("_section")).toBe("run");
3374
+ }
3375
+ finally {
3376
+ vi.useRealTimers();
3377
+ }
3378
+ });
3379
+
3380
+ test("Going back cancels a {{goto ... after=}}", async () => {
3381
+ vi.useFakeTimers();
3382
+ try {
3383
+ const script = `
3384
+ [[start]]:
3385
+ Start. [[wait]]
3386
+
3387
+ [[wait]]:
3388
+ Waiting.{{goto "later" after=3000}}
3389
+
3390
+ [[later]]:
3391
+ Later.
3392
+ `;
3393
+ const { squiffyApi, element } = await initScript(script);
3394
+ await squiffyApi.clickLink(findLink(element, "section", "wait"));
3395
+ squiffyApi.goBack();
3396
+ expect(squiffyApi.get("_section")).toBe("start");
3397
+ await vi.advanceTimersByTimeAsync(5000);
3398
+ expect(squiffyApi.get("_section")).toBe("start");
3399
+ }
3400
+ finally {
3401
+ vi.useRealTimers();
3402
+ }
3403
+ });
3404
+
3405
+ test("Loading a story saved during a {{goto ... after=}} carries on waiting", async () => {
3406
+ vi.useFakeTimers();
3407
+ try {
3408
+ const script = `
3409
+ [[start]]:
3410
+ Waiting.{{goto "later" after=3000}}
3411
+
3412
+ [[later]]:
3413
+ Later.
3414
+ `;
3415
+ const element = document.getElementById("squiffy")!;
3416
+ const { story } = await compile(script);
3417
+ const first = await init({ element, story });
3418
+ await first.begin();
3419
+ expect(first.get("_section")).toBe("start");
3420
+
3421
+ element.innerHTML = "";
3422
+ const second = await init({ element, story });
3423
+ await second.begin();
3424
+ expect(second.get("_section")).toBe("start");
3425
+ await vi.advanceTimersByTimeAsync(3000);
3426
+ expect(second.get("_section")).toBe("later");
3427
+ }
3428
+ finally {
3429
+ vi.useRealTimers();
3430
+ }
3431
+ });
3432
+
3433
+ test("{{goto}} in the master section, leaving out the section it goes to", async () => {
3434
+ const script = `
3435
+ [[]]:
3436
+ {{#if health <= 0 and not (at "death")}}{{goto "death"}}{{/if}}
3437
+
3438
+ [[start]]:
3439
+ @set health = 1
3440
+ Start. [[Fight]](fight, health -= 1)
3441
+
3442
+ [[fight]]:
3443
+ Ouch.
3444
+
3445
+ [[death]]:
3446
+ You died.
3447
+ `;
3448
+ const { squiffyApi, element } = await initScript(script, true);
3449
+ await squiffyApi.clickLink(findLink(element, "section", "Fight"));
3450
+ expect(squiffyApi.get("_section")).toBe("death");
3451
+ expect(element.querySelector(".squiffy-error")).toBeNull();
3452
+ });
3453
+
3454
+ // Visit counts and the story-wide turn count (#377)
3455
+
3456
+ test("{{visits}} counts visits to sections and passages", async () => {
3457
+ const script = `
3458
+ [[start]]:
3459
+ Start. [look] [[cave]]
3460
+
3461
+ [look]:
3462
+ You look. Looked {{visits "look"}} times.
3463
+
3464
+ [[cave]]:
3465
+ Cave visit {{visits "cave"}}. Start {{visits "start"}}, nowhere {{visits "nowhere"}}. [[start]]
3466
+ `;
3467
+ const { squiffyApi, element } = await initScript(script);
3468
+ expect(squiffyApi.get("_visits")).toEqual({ start: 1 });
3469
+
3470
+ await squiffyApi.clickLink(findLink(element, "passage", "look"));
3471
+ expect(visibleText(element)).toContain("Looked 1 times.");
3472
+
3473
+ await squiffyApi.clickLink(findLink(element, "section", "cave"));
3474
+ expect(visibleText(element)).toContain("Cave visit 1. Start 1, nowhere 0.");
3475
+
3476
+ await squiffyApi.clickLink(findLink(element, "section", "start", true));
3477
+ await squiffyApi.clickLink(findLink(element, "passage", "look", true));
3478
+ expect(visibleText(element)).toContain("Looked 2 times.");
3479
+ await squiffyApi.clickLink(findLink(element, "section", "cave", true));
3480
+ expect(visibleText(element)).toContain("Cave visit 2. Start 2, nowhere 0.");
3481
+ });
3482
+
3483
+ for (const expressions of [false, true]) {
3484
+ test(`{{visits}} works in conditions${expressions ? " in a story with a squiffy.json" : ""}`, async () => {
3485
+ const script = `
3486
+ [[start]]:
3487
+ [[cave]]
3488
+
3489
+ [[cave]]:
3490
+ {{#if (visits "cave") > 1}}Back again.{{else}}A new cave.{{/if}} [[start]]
3491
+ `;
3492
+ const { squiffyApi, element } = await initScript(script, expressions);
3493
+ await squiffyApi.clickLink(findLink(element, "section", "cave"));
3494
+ expect(visibleText(element)).toContain("A new cave.");
3495
+ await squiffyApi.clickLink(findLink(element, "section", "start", true));
3496
+ await squiffyApi.clickLink(findLink(element, "section", "cave", true));
3497
+ expect(visibleText(element)).toContain("Back again.");
3498
+ });
3499
+ }
3500
+
3501
+ test("squiffy.story.visits() in JS", async () => {
3502
+ // Note: the 4-space indent on the JS lines is the squiffy JS syntax - don't trim it!
3503
+ const script = `Click [a] then [check].
3504
+
3505
+ [a]:
3506
+ Passage a.
3507
+
3508
+ [check]:
3509
+ squiffy.set("visits_a", squiffy.story.visits("a"));
3510
+ squiffy.set("visits_b", squiffy.story.visits("b"));
3511
+ Checked.`;
3512
+ const { squiffyApi, element } = await initScript(script);
3513
+ await squiffyApi.clickLink(findLink(element, "passage", "a"));
3514
+ await squiffyApi.clickLink(findLink(element, "passage", "check"));
3515
+ expect(squiffyApi.get("visits_a")).toBe(1);
3516
+ expect(squiffyApi.get("visits_b")).toBe(0);
3517
+ });
3518
+
3519
+ test("turns counts every section and passage link clicked, across sections", async () => {
3520
+ const script = `
3521
+ [[start]]:
3522
+ Turn {{turns}}. [a] [b] [[next]]
3523
+
3524
+ [a]:
3525
+ A at turn {{turns}}, section turn {{_turncount}}.
3526
+
3527
+ [b]:
3528
+ B.
3529
+
3530
+ [[next]]:
3531
+ Next at turn {{turns}}. [c]
3532
+
3533
+ [c]:
3534
+ C at turn {{turns}}, section turn {{_turncount}}.
3535
+ `;
3536
+ const { squiffyApi, element } = await initScript(script);
3537
+ expect(visibleText(element)).toContain("Turn 0.");
3538
+ await squiffyApi.clickLink(findLink(element, "passage", "a"));
3539
+ expect(visibleText(element)).toContain("A at turn 1, section turn 1.");
3540
+ await squiffyApi.clickLink(findLink(element, "passage", "b"));
3541
+ await squiffyApi.clickLink(findLink(element, "section", "next"));
3542
+ expect(visibleText(element)).toContain("Next at turn 3.");
3543
+ await squiffyApi.clickLink(findLink(element, "passage", "c"));
3544
+ expect(visibleText(element)).toContain("C at turn 4, section turn 1.");
3545
+ expect(squiffyApi.get("turns")).toBe(4);
3546
+ });
3547
+
3548
+ test("turns works in conditions", async () => {
3549
+ const script = `
3550
+ [[start]]:
3551
+ [a] [b] [c]
3552
+
3553
+ [a]:
3554
+ A.
3555
+
3556
+ [b]:
3557
+ B.
3558
+
3559
+ [c]:
3560
+ {{#if turns > 2}}Late.{{else}}Early.{{/if}}
3561
+ `;
3562
+ const { squiffyApi, element } = await initScript(script, true);
3563
+ await squiffyApi.clickLink(findLink(element, "passage", "a"));
3564
+ await squiffyApi.clickLink(findLink(element, "passage", "b"));
3565
+ await squiffyApi.clickLink(findLink(element, "passage", "c"));
3566
+ expect(visibleText(element)).toContain("Late.");
3567
+ });
3568
+
3569
+ test("{{goto}} counts as a visit but not a turn", async () => {
3570
+ const script = `
3571
+ [[start]]:
3572
+ [[fight]]
3573
+
3574
+ [[fight]]:
3575
+ {{goto "death"}}
3576
+
3577
+ [[death]]:
3578
+ Died at turn {{turns}}, visit {{visits "death"}}.
3579
+ `;
3580
+ const { squiffyApi, element } = await initScript(script);
3581
+ await squiffyApi.clickLink(findLink(element, "section", "fight"));
3582
+ expect(squiffyApi.get("_section")).toBe("death");
3583
+ expect(visibleText(element)).toContain("Died at turn 1, visit 1.");
3584
+ expect(squiffyApi.get("_visits")).toEqual({ start: 1, fight: 1, death: 1 });
3585
+ });
3586
+
3587
+ test("Going back undoes visit counts and turns", async () => {
3588
+ const script = `
3589
+ [[start]]:
3590
+ [a] [[next]]
3591
+
3592
+ [a]:
3593
+ A.
3594
+
3595
+ [[next]]:
3596
+ Next.
3597
+ `;
3598
+ const { squiffyApi, element } = await initScript(script);
3599
+ await squiffyApi.clickLink(findLink(element, "passage", "a"));
3600
+ await squiffyApi.clickLink(findLink(element, "section", "next"));
3601
+ expect(squiffyApi.get("turns")).toBe(2);
3602
+ expect(squiffyApi.get("_visits")).toEqual({ start: 1, a: 1, next: 1 });
3603
+
3604
+ squiffyApi.goBack();
3605
+ expect(squiffyApi.get("turns")).toBe(1);
3606
+ expect(squiffyApi.get("_visits")).toEqual({ start: 1, a: 1 });
3607
+
3608
+ squiffyApi.goBack();
3609
+ expect(squiffyApi.get("turns")).toBe(0);
3610
+ expect(squiffyApi.get("_visits")).toEqual({ start: 1 });
3611
+
3612
+ await squiffyApi.clickLink(findLink(element, "passage", "a", true));
3613
+ expect(squiffyApi.get("_visits")).toEqual({ start: 1, a: 1 });
3614
+ });
3615
+
3616
+ test("A save from before visit counts treats each seen section or passage as visited once", async () => {
3617
+ const script = `
3618
+ [[start]]:
3619
+ Start {{visits "start"}}, cave {{visits "cave"}}, lake {{visits "lake"}}, turns {{turns}}.
3620
+ `;
3621
+ const storyId = "00000000-0000-0000-0000-000000000000";
3622
+ localStorage[`${storyId}-_seen_sections`] = JSON.stringify(["start", "cave"]);
3623
+
3624
+ const element = document.getElementById("squiffy")!;
3625
+ const compileResult = await compile(script, "old-save.squiffy");
3626
+ const squiffyApi = await init({ element, story: compileResult.story, persist: true, storyId });
3627
+ await squiffyApi.begin();
3628
+
3629
+ // Going to start makes it the second visit
3630
+ expect(visibleText(element)).toContain("Start 2, cave 1, lake 0, turns 0.");
3631
+ expect(squiffyApi.get("_visits")).toEqual({ start: 2 });
3632
+
3633
+ localStorage.clear();
3634
+ });
3635
+