squiffy-runtime 6.0.0-beta.3 → 6.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.
@@ -121,6 +121,29 @@ Destination section text.`;
121
121
  expect(element.querySelector(".squiffy-output-section")?.getAttribute("data-section")).toBe("destination");
122
122
  });
123
123
 
124
+ test("squiffy.story.seen() in JS reports which sections and passages have been seen", async () => {
125
+ // Note: the 4-space indent on the JS lines is the squiffy JS syntax - don't trim it!
126
+ const script = `Click [a] then [check].
127
+
128
+ [a]:
129
+ Passage a.
130
+
131
+ [check]:
132
+ squiffy.set("seen_a", squiffy.story.seen("a"));
133
+ squiffy.set("seen_b", squiffy.story.seen("b"));
134
+ squiffy.set("seen_start", squiffy.story.seen("_default"));
135
+ Checked.`;
136
+
137
+ const { squiffyApi, element } = await initScript(script);
138
+
139
+ await squiffyApi.clickLink(findLink(element, "passage", "a"));
140
+ await squiffyApi.clickLink(findLink(element, "passage", "check"));
141
+
142
+ expect(squiffyApi.get("seen_a")).toBe(true);
143
+ expect(squiffyApi.get("seen_b")).toBe(false);
144
+ expect(squiffyApi.get("seen_start")).toBe(true);
145
+ });
146
+
124
147
  test("Click a section link", async () => {
125
148
  const script = await fs.readFile("../examples/test/example.squiffy", "utf-8");
126
149
  const { squiffyApi, element } = await initScript(script);
@@ -232,6 +255,221 @@ test("Click a passage link and go back", async () => {
232
255
  expect(await squiffyApi.clickLink(linkToPassage)).toBe(true);
233
256
  });
234
257
 
258
+ test("Turn-count passages share an output block with the passage they follow", async () => {
259
+ // https://github.com/textadventures/squiffy/issues/123
260
+ const script = `
261
+ [[start]]:
262
+ Choose [a], [b] or [c]
263
+
264
+ [a]:
265
+ You chose a.
266
+
267
+ [b]:
268
+ You chose b.
269
+
270
+ [c]:
271
+ You chose c.
272
+
273
+ [@1]:
274
+ Turn one.
275
+
276
+ [@last]:
277
+ Last turn.
278
+ `;
279
+ const { squiffyApi, element } = await initScript(script);
280
+
281
+ const expectSingleBlock = (passageName: string, expectedText: string[]) => {
282
+ const passageElement = element.querySelector(`.squiffy-output-passage[data-passage="${passageName}"]`);
283
+ const blocks = passageElement!.querySelectorAll(".squiffy-output-block");
284
+ expect(blocks.length).toBe(1);
285
+ expect(Array.from(blocks[0].children).map(c => c.textContent)).toEqual(expectedText);
286
+ };
287
+
288
+ await squiffyApi.clickLink(findLink(element, "passage", "a"));
289
+ expectSingleBlock("a", ["You chose a.", "Turn one."]);
290
+
291
+ await squiffyApi.clickLink(findLink(element, "passage", "b"));
292
+ await squiffyApi.clickLink(findLink(element, "passage", "c"));
293
+ expectSingleBlock("c", ["You chose c.", "Last turn."]);
294
+ });
295
+
296
+ test("Going back to a section re-enables its inputs", async () => {
297
+ // https://github.com/textadventures/squiffy/issues/123
298
+ const script = `
299
+ [[start]]:
300
+ <input type="text" data-attribute="name">
301
+ <textarea data-attribute="bio"></textarea>
302
+ <select data-attribute="colour"><option>red</option><option>blue</option></select>
303
+ <div contenteditable="true" data-attribute="notes"></div>
304
+ <input type="text" data-attribute="fixed" disabled>
305
+ [[next]]
306
+
307
+ [[next]]:
308
+ Hello {{name}}.
309
+ `;
310
+ const { squiffyApi, element } = await initScript(script);
311
+
312
+ const input = element.querySelector("input[data-attribute='name']") as HTMLInputElement;
313
+ const textarea = element.querySelector("textarea") as HTMLTextAreaElement;
314
+ const select = element.querySelector("select") as HTMLSelectElement;
315
+ const editable = element.querySelector("[contenteditable]") as HTMLElement;
316
+ const authorDisabled = element.querySelector("input[data-attribute='fixed']") as HTMLInputElement;
317
+
318
+ input.value = "Bob";
319
+ await squiffyApi.clickLink(findLink(element, "section", "next"));
320
+
321
+ expect(squiffyApi.get("name")).toBe("Bob");
322
+ expect(input.disabled).toBe(true);
323
+ expect(textarea.disabled).toBe(true);
324
+ expect(select.disabled).toBe(true);
325
+ expect(editable.getAttribute("contenteditable")).toBe("false");
326
+
327
+ squiffyApi.goBack();
328
+
329
+ expect(squiffyApi.get("name")).toBe(null);
330
+ expect(input.disabled).toBe(false);
331
+ expect(textarea.disabled).toBe(false);
332
+ expect(select.disabled).toBe(false);
333
+ expect(editable.getAttribute("contenteditable")).toBe("true");
334
+ expect(element.querySelectorAll("[data-squiffy-disabled]").length).toBe(0);
335
+
336
+ // An input the author disabled stays disabled
337
+ expect(authorDisabled.disabled).toBe(true);
338
+
339
+ // The edited value is picked up when moving on again
340
+ input.value = "Alice";
341
+ await squiffyApi.clickLink(findLink(element, "section", "next"));
342
+ expect(squiffyApi.get("name")).toBe("Alice");
343
+ });
344
+
345
+ test("Going back is ignored while transitions are running", async () => {
346
+ // https://github.com/textadventures/squiffy/issues/123
347
+ let release: () => void = () => {};
348
+ (window as any).transitionGate = new Promise<void>(resolve => { release = resolve; });
349
+
350
+ // Note: the 4-space indent on the transition line is the squiffy JS syntax - don't trim it!
351
+ const script = `
352
+ [[start]]:
353
+ @set x = 1
354
+ Start. [[slow]]
355
+
356
+ [[slow]]:
357
+ @set x = 2
358
+ squiffy.ui.transition(() => window.transitionGate);
359
+ Slow section. [[after]]
360
+
361
+ [[after]]:
362
+ After.
363
+ `;
364
+ const { squiffyApi, element } = await initScript(script);
365
+
366
+ const clickPromise = squiffyApi.clickLink(findLink(element, "section", "slow"));
367
+ await new Promise(resolve => setTimeout(resolve, 0));
368
+
369
+ // Mid-transition: going back does nothing
370
+ squiffyApi.goBack();
371
+ expect(element.querySelector(".squiffy-output-section[data-section='slow']")).not.toBeNull();
372
+
373
+ release();
374
+ await clickPromise;
375
+ expect(squiffyApi.get("_section")).toBe("slow");
376
+ expect(squiffyApi.get("x")).toBe(2);
377
+
378
+ // Once the transition is done, going back fully restores the previous section
379
+ squiffyApi.goBack();
380
+ expect(element.querySelector(".squiffy-output-section[data-section='slow']")).toBeNull();
381
+ expect(squiffyApi.get("_section")).toBe("start");
382
+ expect(squiffyApi.get("x")).toBe(1);
383
+ expect(squiffyApi.get("_seen_sections")).toEqual(["start"]);
384
+ });
385
+
386
+ const visibleText = (element: HTMLElement) =>
387
+ Array.from(element.querySelector("div")!.children)
388
+ .filter(child => !child.classList.contains("squiffy-clear-stack"))
389
+ .map(child => child.textContent)
390
+ .join(" ")
391
+ .replace(/\s+/g, " ")
392
+ .trim();
393
+
394
+ test("Going back through a cleared section reached via squiffy.story.go()", async () => {
395
+ // https://github.com/textadventures/squiffy/issues/123
396
+ // Note: the 4-space indent on the squiffy.story.go line is the squiffy JS syntax - don't trim it!
397
+ const script = `
398
+ [[start]]:
399
+ @set x = 1
400
+ Start. [[jump]]
401
+
402
+ [[jump]]:
403
+ @set x = 2
404
+ squiffy.story.go("cleared");
405
+ Never shown.
406
+
407
+ [[cleared]]:
408
+ @clear
409
+ @set x = 3
410
+ Cleared. [[next]]
411
+
412
+ [[next]]:
413
+ @clear
414
+ Next.
415
+ `;
416
+ const { squiffyApi, element } = await initScript(script);
417
+
418
+ await squiffyApi.clickLink(findLink(element, "section", "jump"));
419
+ expect(visibleText(element)).toBe("Cleared. next");
420
+ await squiffyApi.clickLink(findLink(element, "section", "next"));
421
+ expect(visibleText(element)).toBe("Next.");
422
+
423
+ squiffyApi.goBack();
424
+ expect(visibleText(element)).toBe("Cleared. next");
425
+ expect(squiffyApi.get("_section")).toBe("cleared");
426
+
427
+ squiffyApi.goBack();
428
+ expect(visibleText(element)).toBe("Start. jump");
429
+ expect(squiffyApi.get("_section")).toBe("start");
430
+ expect(squiffyApi.get("x")).toBe(1);
431
+ expect(squiffyApi.get("_seen_sections")).toEqual(["start"]);
432
+ expect(findLink(element, "section", "jump", true)).toBeDefined();
433
+ });
434
+
435
+ test("Going back through screens cleared by the master section and a passage", async () => {
436
+ // https://github.com/textadventures/squiffy/issues/123
437
+ const script = `
438
+ [[start]]:
439
+ Start. [[two]]
440
+
441
+ [[]]:
442
+ @clear
443
+
444
+ [[two]]:
445
+ Two. [a] [[three]]
446
+
447
+ [a]:
448
+ @clear
449
+ Passage a.
450
+
451
+ [[three]]:
452
+ Three.
453
+ `;
454
+ const { squiffyApi, element } = await initScript(script);
455
+
456
+ await squiffyApi.clickLink(findLink(element, "section", "two"));
457
+ expect(visibleText(element)).toBe("Two. a three");
458
+ await squiffyApi.clickLink(findLink(element, "passage", "a"));
459
+ expect(visibleText(element)).toBe("Passage a.");
460
+
461
+ squiffyApi.goBack();
462
+ expect(visibleText(element)).toBe("Two. a three");
463
+ expect(findLink(element, "passage", "a", true)).toBeDefined();
464
+
465
+ squiffyApi.goBack();
466
+ expect(visibleText(element)).toBe("Start. two");
467
+ expect(squiffyApi.get("_seen_sections")).toEqual(["start"]);
468
+
469
+ await squiffyApi.clickLink(findLink(element, "section", "two"));
470
+ expect(visibleText(element)).toBe("Two. a three");
471
+ });
472
+
235
473
  test("Run JavaScript functions", async () => {
236
474
  const script = `
237
475
  document.getElementById('test').innerText = 'Initial JavaScript';
@@ -488,6 +726,20 @@ Passage in section B.`);
488
726
  expect(await squiffyApi.clickLink(linkB1)).toBe(true);
489
727
  });
490
728
 
729
+ test("Print an array element by index", async () => {
730
+ const script = `
731
+ [[section1]]:
732
+ squiffy.set("fruits", ["apple", "banana", "cherry"]);
733
+
734
+ The second fruit is {{fruits.[1]}}.
735
+ `;
736
+
737
+ const { element } = await initScript(script);
738
+
739
+ const output = getSectionContent(element, "section1");
740
+ expect(output).toBe("The second fruit is banana.");
741
+ });
742
+
491
743
  test("Embed text from a section", async () => {
492
744
  const script = `
493
745
  [[section1]]:
@@ -544,9 +796,10 @@ Updated text from next section.
544
796
  squiffyApi.update(updated.story);
545
797
  output = getSectionContent(element, "section1");
546
798
 
547
- // NOTE: The embedded text does not currently get updated. We would need to track where the embedded
548
- // text has been written.
549
- expect(output).toBe("Here is an updated script with text from the next section: Text from next section.");
799
+ // section1 changed, so it is re-rendered using the new section2 text.
800
+ // NOTE: If only section2 changed, the embedded text would not currently get updated. We would need to
801
+ // track where the embedded text has been written.
802
+ expect(output).toBe("Here is an updated script with text from the next section: Updated text from next section.");
550
803
  });
551
804
 
552
805
  test("Update passage with embedded text", async () => {
@@ -575,9 +828,140 @@ Updated text from a passage.
575
828
  squiffyApi.update(updated.story);
576
829
  output = getSectionContent(element, "section1");
577
830
 
578
- // NOTE: The embedded text does not currently get updated. We would need to track where the embedded
579
- // text has been written.
580
- expect(output).toBe("Here is an updated script with text from a passage: Text from a passage.");
831
+ // section1 changed, so it is re-rendered using the new passage text.
832
+ // NOTE: If only the passage changed, the embedded text would not currently get updated. We would need to
833
+ // track where the embedded text has been written.
834
+ expect(output).toBe("Here is an updated script with text from a passage: Updated text from a passage.");
835
+ });
836
+
837
+ test("Update section to embed a newly added passage", async () => {
838
+ const script = `
839
+ [[section1]]:
840
+ Here is some text from a passage: {{embed "passage"}}
841
+ `;
842
+
843
+ const { squiffyApi, element } = await initScript(script);
844
+
845
+ let output = getSectionContent(element, "section1");
846
+ expect(output).toBe("Here is some text from a passage: ");
847
+
848
+ const script2 = `
849
+ [[section1]]:
850
+ Here is updated text from a passage: {{embed "passage"}}
851
+
852
+ [passage]:
853
+ Text from a passage.
854
+ `;
855
+
856
+ const updated = await compile(script2);
857
+ squiffyApi.update(updated.story);
858
+ output = getSectionContent(element, "section1");
859
+
860
+ expect(output).toBe("Here is updated text from a passage: Text from a passage.");
861
+ });
862
+
863
+ test("Update passage to embed a newly added passage", async () => {
864
+ const script = `
865
+ [[section1]]:
866
+ Click [a passage].
867
+
868
+ [a passage]:
869
+ Embedded: {{embed "other"}}
870
+ `;
871
+
872
+ const { squiffyApi, element } = await initScript(script);
873
+
874
+ await squiffyApi.clickLink(findLink(element, "passage", "a passage"));
875
+ let output = getPassageContent(element, "section1", "a passage");
876
+ expect(output).toBe("Embedded: ");
877
+
878
+ const script2 = `
879
+ [[section1]]:
880
+ Click [a passage].
881
+
882
+ [a passage]:
883
+ Updated embedded: {{embed "other"}}
884
+
885
+ [other]:
886
+ Other passage text.
887
+ `;
888
+
889
+ const updated = await compile(script2);
890
+ squiffyApi.update(updated.story);
891
+ output = getPassageContent(element, "section1", "a passage");
892
+
893
+ expect(output).toBe("Updated embedded: Other passage text.");
894
+ });
895
+
896
+ test("Update section to embed a newly added section", async () => {
897
+ const script = `
898
+ [[section1]]:
899
+ Here is some text from the next section: {{embed "section2"}}
900
+ `;
901
+
902
+ const { squiffyApi, element } = await initScript(script);
903
+
904
+ let output = getSectionContent(element, "section1");
905
+ expect(output).toBe("Here is some text from the next section: ");
906
+
907
+ const script2 = `
908
+ [[section1]]:
909
+ Here is updated text from the next section: {{embed "section2"}}
910
+
911
+ [[section2]]:
912
+ Text from next section.
913
+ `;
914
+
915
+ const updated = await compile(script2);
916
+ squiffyApi.update(updated.story);
917
+ output = getSectionContent(element, "section1");
918
+
919
+ expect(output).toBe("Here is updated text from the next section: Text from next section.");
920
+ });
921
+
922
+ test("Section link to a random section", async () => {
923
+ const script = `
924
+ [[start]]:
925
+ {{section (random (array "a" "b" "c"))}}
926
+
927
+ [[a]]:
928
+ A
929
+
930
+ [[b]]:
931
+ B
932
+
933
+ [[c]]:
934
+ C
935
+ `;
936
+
937
+ const { element } = await initScript(script);
938
+
939
+ expect(element.textContent).not.toContain("Template error");
940
+ const link = element.querySelector("a.squiffy-link.link-section");
941
+ expect(link).not.toBeNull();
942
+ const section = link!.getAttribute("data-section");
943
+ expect(["a", "b", "c"]).toContain(section);
944
+ expect(link!.textContent).toBe(section);
945
+ });
946
+
947
+ test("Section link to a random section with link text", async () => {
948
+ const script = `
949
+ [[start]]:
950
+ {{section (random (array "a" "b")) text="Go through the door"}}
951
+
952
+ [[a]]:
953
+ A
954
+
955
+ [[b]]:
956
+ B
957
+ `;
958
+
959
+ const { element } = await initScript(script);
960
+
961
+ expect(element.textContent).not.toContain("Template error");
962
+ const link = findLink(element, "section", "Go through the door");
963
+ expect(link).toBeDefined();
964
+ expect(["a", "b"]).toContain(link.getAttribute("data-section"));
581
965
  });
582
966
 
583
967
  test("Clear entire script, then update", async () => {
@@ -1666,7 +2050,7 @@ Hello, {{player_name}}!
1666
2050
 
1667
2051
  // Fill the input
1668
2052
  input.value = "Alice";
1669
- input.dispatchEvent(new Event("input", { bubbles: true }));
2053
+ input.dispatchEvent(new window.Event("input", { bubbles: true }));
1670
2054
 
1671
2055
  // Link should now be enabled
1672
2056
  expect(continueLink.classList.contains("validation-disabled")).toBe(false);
@@ -1695,14 +2079,14 @@ Hello, {{player_name}}!
1695
2079
 
1696
2080
  // Fill the input
1697
2081
  input.value = "Test";
1698
- input.dispatchEvent(new Event("input", { bubbles: true }));
2082
+ input.dispatchEvent(new window.Event("input", { bubbles: true }));
1699
2083
 
1700
2084
  // Link should be enabled
1701
2085
  expect(continueLink.classList.contains("validation-disabled")).toBe(false);
1702
2086
 
1703
2087
  // Clear the input
1704
2088
  input.value = "";
1705
- input.dispatchEvent(new Event("input", { bubbles: true }));
2089
+ input.dispatchEvent(new window.Event("input", { bubbles: true }));
1706
2090
 
1707
2091
  // Link should be disabled again
1708
2092
  expect(continueLink.classList.contains("validation-disabled")).toBe(true);
@@ -1743,7 +2127,7 @@ Your pockets are empty.
1743
2127
 
1744
2128
  // Fill the input
1745
2129
  input.value = "Bob";
1746
- input.dispatchEvent(new Event("input", { bubbles: true }));
2130
+ input.dispatchEvent(new window.Event("input", { bubbles: true }));
1747
2131
 
1748
2132
  // Links should now be enabled
1749
2133
  expect(lookLink.classList.contains("validation-disabled")).toBe(false);
@@ -1776,21 +2160,21 @@ Name: {{name}}, Age: {{age}}
1776
2160
 
1777
2161
  // Fill only name
1778
2162
  nameInput.value = "Charlie";
1779
- nameInput.dispatchEvent(new Event("input", { bubbles: true }));
2163
+ nameInput.dispatchEvent(new window.Event("input", { bubbles: true }));
1780
2164
 
1781
2165
  // Still disabled because age is empty
1782
2166
  expect(continueLink.classList.contains("validation-disabled")).toBe(true);
1783
2167
 
1784
2168
  // Fill age with invalid value
1785
2169
  ageInput.value = "150";
1786
- ageInput.dispatchEvent(new Event("input", { bubbles: true }));
2170
+ ageInput.dispatchEvent(new window.Event("input", { bubbles: true }));
1787
2171
 
1788
2172
  // Still disabled because age is out of range
1789
2173
  expect(continueLink.classList.contains("validation-disabled")).toBe(true);
1790
2174
 
1791
2175
  // Fill age with valid value
1792
2176
  ageInput.value = "25";
1793
- ageInput.dispatchEvent(new Event("input", { bubbles: true }));
2177
+ ageInput.dispatchEvent(new window.Event("input", { bubbles: true }));
1794
2178
 
1795
2179
  // Now should be enabled
1796
2180
  expect(continueLink.classList.contains("validation-disabled")).toBe(false);
@@ -1820,7 +2204,7 @@ Done!
1820
2204
  // Fill first input and navigate
1821
2205
  const nameInput = element.querySelector('input[data-attribute="name"]') as HTMLInputElement;
1822
2206
  nameInput.value = "Dave";
1823
- nameInput.dispatchEvent(new Event("input", { bubbles: true }));
2207
+ nameInput.dispatchEvent(new window.Event("input", { bubbles: true }));
1824
2208
 
1825
2209
  const continueLink = findLink(element, "section", "Continue", true);
1826
2210
  await squiffyApi.clickLink(continueLink);
@@ -1832,7 +2216,7 @@ Done!
1832
2216
  // Fill email and continue
1833
2217
  const emailInput = element.querySelector('input[data-attribute="email"]') as HTMLInputElement;
1834
2218
  emailInput.value = "dave@example.com";
1835
- emailInput.dispatchEvent(new Event("input", { bubbles: true }));
2219
+ emailInput.dispatchEvent(new window.Event("input", { bubbles: true }));
1836
2220
 
1837
2221
  expect(finishLink.classList.contains("validation-disabled")).toBe(false);
1838
2222
 
@@ -1858,7 +2242,7 @@ Done!
1858
2242
 
1859
2243
  // Fill only the active input (disabled one should be ignored)
1860
2244
  activeInput.value = "test";
1861
- activeInput.dispatchEvent(new Event("input", { bubbles: true }));
2245
+ activeInput.dispatchEvent(new window.Event("input", { bubbles: true }));
1862
2246
 
1863
2247
  // Link should be enabled (disabled input doesn't count)
1864
2248
  expect(continueLink.classList.contains("validation-disabled")).toBe(false);
@@ -1897,7 +2281,7 @@ You selected {{difficulty}} mode.
1897
2281
 
1898
2282
  // Select an option
1899
2283
  select.value = "normal";
1900
- select.dispatchEvent(new Event("change", { bubbles: true }));
2284
+ select.dispatchEvent(new window.Event("change", { bubbles: true }));
1901
2285
 
1902
2286
  // Link should now be enabled
1903
2287
  expect(startLink.classList.contains("validation-disabled")).toBe(false);
@@ -1931,7 +2315,7 @@ Difficulty: {{difficulty}}
1931
2315
 
1932
2316
  // Select an option
1933
2317
  select.value = "hard";
1934
- select.dispatchEvent(new Event("change", { bubbles: true }));
2318
+ select.dispatchEvent(new window.Event("change", { bubbles: true }));
1935
2319
 
1936
2320
  // Navigate
1937
2321
  await squiffyApi.clickLink(startLink);
@@ -1962,14 +2346,14 @@ Done!
1962
2346
 
1963
2347
  // Select an option
1964
2348
  select.value = "a";
1965
- select.dispatchEvent(new Event("change", { bubbles: true }));
2349
+ select.dispatchEvent(new window.Event("change", { bubbles: true }));
1966
2350
 
1967
2351
  // Link should be enabled
1968
2352
  expect(continueLink.classList.contains("validation-disabled")).toBe(false);
1969
2353
 
1970
2354
  // Change back to empty
1971
2355
  select.value = "";
1972
- select.dispatchEvent(new Event("change", { bubbles: true }));
2356
+ select.dispatchEvent(new window.Event("change", { bubbles: true }));
1973
2357
 
1974
2358
  // Link should be disabled again
1975
2359
  expect(continueLink.classList.contains("validation-disabled")).toBe(true);
@@ -2005,23 +2389,23 @@ Level: <select data-attribute="level" required>
2005
2389
 
2006
2390
  // Fill only name
2007
2391
  nameInput.value = "Player";
2008
- nameInput.dispatchEvent(new Event("input", { bubbles: true }));
2392
+ nameInput.dispatchEvent(new window.Event("input", { bubbles: true }));
2009
2393
 
2010
2394
  // Still disabled because select is empty
2011
2395
  expect(continueLink.classList.contains("validation-disabled")).toBe(true);
2012
2396
 
2013
2397
  // Select level but clear name
2014
2398
  levelSelect.value = "1";
2015
- levelSelect.dispatchEvent(new Event("change", { bubbles: true }));
2399
+ levelSelect.dispatchEvent(new window.Event("change", { bubbles: true }));
2016
2400
  nameInput.value = "";
2017
- nameInput.dispatchEvent(new Event("input", { bubbles: true }));
2401
+ nameInput.dispatchEvent(new window.Event("input", { bubbles: true }));
2018
2402
 
2019
2403
  // Still disabled because name is empty
2020
2404
  expect(continueLink.classList.contains("validation-disabled")).toBe(true);
2021
2405
 
2022
2406
  // Fill both
2023
2407
  nameInput.value = "Hero";
2024
- nameInput.dispatchEvent(new Event("input", { bubbles: true }));
2408
+ nameInput.dispatchEvent(new window.Event("input", { bubbles: true }));
2025
2409
 
2026
2410
  // Now should be enabled
2027
2411
  expect(continueLink.classList.contains("validation-disabled")).toBe(false);
@@ -2274,4 +2658,62 @@ test("Template error preserves monospace styling", async () => {
2274
2658
  expect(errorDiv).not.toBeNull();
2275
2659
  expect(errorDiv.style.fontFamily).toBe("monospace");
2276
2660
  expect(errorDiv.style.whiteSpace).toBe("pre-wrap");
2277
- });
2661
+ });
2662
+ // Infix expressions in {{#if}} / {{#unless}}
2663
+
2664
+ test("Infix comparisons in {{#if}}", async () => {
2665
+ const script = `@set gender = male
2666
+ @set score = 150
2667
+ @set has_key = true
2668
+
2669
+ {{#if gender = "male"}}Male.{{/if}}
2670
+ {{#if score > 100 and has_key}}High score with key.{{else}}No.{{/if}}
2671
+ {{#if score < 100}}Low.{{else if score >= 150}}At least 150.{{else}}Medium.{{/if}}
2672
+ {{#unless score != 150}}Exactly 150.{{/unless}}
2673
+ {{#if not gender = "female"}}Not female.{{/if}}`;
2674
+
2675
+ const { element } = await initScript(script);
2676
+ const output = getSectionContent(element, "_default");
2677
+ expect(output).toBe("Male.\nHigh score with key.\nAt least 150.\nExactly 150.\nNot female.");
2678
+ });
2679
+
2680
+ test("Infix expressions combine with existing helpers", async () => {
2681
+ const script = `
2682
+ [[start]]:
2683
+ @set count = 1
2684
+ {{#if not (seen "other") or count >= 3}}Not seen other.{{else}}Seen other.{{/if}}
2685
+ {{#if (at "start") && !(seen "other")}}At start.{{/if}}
2686
+ `;
2687
+
2688
+ const { element } = await initScript(script);
2689
+ const output = getSectionContent(element, "start");
2690
+ expect(output).toBe("Not seen other.\nAt start.");
2691
+ });
2692
+
2693
+ test("Malformed infix expression shows a template error", async () => {
2694
+ const script = `
2695
+ [[start]]:
2696
+ {{#if score >}}Yes{{/if}}
2697
+ `;
2698
+
2699
+ const { element } = await initScript(script);
2700
+ const errorDiv = element.querySelector(".squiffy-error");
2701
+ expect(errorDiv?.textContent).toContain("Invalid condition \"score >\": expected a value after '>'");
2702
+ });
2703
+
2704
+ test("Custom helpers can be registered in @ui", async () => {
2705
+ const script = `
2706
+ @ui
2707
+ squiffy.registerHelper("shout", (text) => text.toUpperCase() + "!");
2708
+ squiffy.registerHelper("bold", (text) => new squiffy.SafeString("<b>" + text + "</b>"));
2709
+ ---
2710
+
2711
+ {{shout "hello"}} {{bold "world"}} {{shout "<i>"}}
2712
+ `;
2713
+
2714
+ const { element } = await initScript(script);
2715
+ const section = element.querySelector("[data-source='[[_default]]']");
2716
+ expect(section?.textContent).toBe("HELLO! world <I>!");
2717
+ expect(section?.querySelector("b")?.textContent).toBe("world");
2718
+ expect(section?.querySelector("i")).toBeNull();
2719
+ });