#!/usr/bin/env rexx
/******************************************************************************/
/*                                                                            */
/* Fragments.testGroup - reveal grouping: `with` and `group=`                 */
/* ==================================================                         */
/*                                                                            */
/* This program is part of the Rexx Parser package                            */
/* [See https://rexx.epbcn.com/rexx-parser/]                                  */
/*                                                                            */
/* Copyright (c) 2024-2026 Josep Maria Blasco <josep.maria.blasco@epbcn.com>  */
/*                                                                            */
/* License: Apache License 2.0 (https://www.apache.org/licenses/LICENSE-2.0)  */
/*                                                                            */
/* Version history:                                                           */
/*                                                                            */
/* Date     Version Details                                                   */
/* -------- ------- --------------------------------------------------------- */
/* 20260921         First version -- group= (named simultaneous fragments)    */
/* 20261117         The destination of .Error (or .Output) is put back in a   */
/*                  FINALLY clause (ooRexx r13535): a test that fails no      */
/*                  longer leaves it redirected                               */
/*                                                                            */
/******************************************************************************/
/*                                                                            */
/* WHAT THIS PINS                                                              */
/*                                                                            */
/* A STEP is what one key press reveals. Two markers let fragments share a     */
/* step: the class `with` (join the step of the fragment right before, the     */
/* contiguous case, used by empty table cells) and `group=NAME` (every         */
/* fragment with that name shares one step, contiguous or not, anchored at     */
/* the first member -- rule a). The grouping itself lives in the runtime's     */
/* stepsOf() and is verified in the browser; what a build test CAN pin is the  */
/* CONTRACT the runtime reads: that `group=` on a fragment survives the        */
/* pipeline as data-group, on a block fragment AND on a fragment hoisted from  */
/* a list item -- and that a plain fragment marker is still not polluted by an */
/* unrelated attribute (the hoist guard). If data-group stopped reaching the   */
/* fragment, group= would silently do nothing; this is that guard.            */
/*                                                                            */
/******************************************************************************/

parse source . . fileSpec
group = .TestGroup~new(fileSpec)
group~add(.Fragments.testGroup)
If group~isAutomatedTest Then
  Return group
Else
  Return group~suite~execute~~print

::Requires "ooTest.frm"

::Class Fragments.testGroup Subclass ooTestCase Public

/* A fragment span carrying data-group is still a bare fragment marker, so it  */
/* is hoisted onto its <li> -- carrying data-group with it. Before this, the   */
/* guard demanded class exactly "fragment" (+ data-anim), so group= on a list  */
/* item was dropped and the item never joined its group.                      */
::Method test_a_group_span_is_a_fragment_marker
  self~assertTrue(self~run("FragmentSpanAttrs", -
    .Array~of('class="fragment" data-group="g1"')) \== 0, "fragment + group is hoistable")

::Method test_group_and_anim_together_are_still_a_marker
  self~assertTrue(self~run("FragmentSpanAttrs", -
    .Array~of('class="fragment" data-anim="fade" data-group="g1"')) \== 0, -
    "fragment + anim + group is still a bare marker")

::Method test_an_unrelated_attribute_still_blocks_the_hoist
  self~assertTrue(self~run("FragmentSpanAttrs", -
    .Array~of('class="fragment" data-foo="x"')) == 0, -
    "a span with an unrelated attribute is a marked PIECE, not the bullet")

/* The hoist actually moves data-group onto the <li>, subtree included, so the */
/* whole item is the fragment and it carries its group name to the runtime.   */
::Method test_group_is_hoisted_onto_the_item
  html = "<li><span "'class="fragment" data-group="g2"'">shares</span></li>"
  out  = self~run("HoistListItemFragments", .Array~of(html))
  self~assertTrue(out~pos('<li class="fragment" data-group="g2">') > 0, -
    "the item is the fragment and keeps its group")
  self~assertEquals(0, out~pos("<span"), "the marker span is unwrapped")

::Method test_a_plain_fragment_item_still_hoists
  html = "<li><span "'class="fragment"'">plain</span></li>"
  out  = self~run("HoistListItemFragments", .Array~of(html))
  self~assertTrue(out~pos('<li class="fragment">') > 0, "an unnamed fragment item still works")

/* -- 23-Sep: timing words (.afterPrevious / .withPrevious) ------------------ */
/* On an incremental BLOCK they time the block against what came before, so   */
/* they land on its FIRST step; on the wrapper they did nothing. On an ITEM    */
/* (`- [text]{.afterPrevious}`) Pandoc puts them on a span, and they must      */
/* reach the <li> the runtime reads. Rony's test.md, slides 2 and 3.           */

::Method test_block_timing_goes_to_the_first_item
  html = '<div class="incremental afterPrevious">'"0a"x"<ul>"||"0a"x -
         "<li>one</li>"||"0a"x"<li>two</li>"||"0a"x"</ul>"||"0a"x"</div>"
  out = self~run("MarkIncrementalItems", .Array~of(html))
  self~assertTrue(out~pos('<li class="fragment afterPrevious">one') > 0, "first item is timed")
  self~assertTrue(out~pos('<li class="fragment">two') > 0,               "second keeps its press")
  self~assertEquals(0, out~pos('class="body afterPrevious"'),           "not left on the wrapper")

::Method test_block_withPrevious_goes_to_the_first_item
  html = '<div class="incremental withPrevious">'"0a"x"<ul>"||"0a"x -
         "<li>one</li>"||"0a"x"</ul>"||"0a"x"</div>"
  out = self~run("MarkIncrementalItems", .Array~of(html))
  self~assertTrue(out~pos('<li class="fragment withPrevious">one') > 0, "withPrevious too")

::Method test_item_timing_span_reaches_the_li
  html = '<div class="incremental">'"0a"x"<ul>"||"0a"x -
         '<li><span class="afterPrevious">two <code>x</code></span></li>'"0a"x -
         '<li><span class="withPrevious">four</span></li>'"0a"x"</ul>"||"0a"x"</div>"
  out = self~run("MarkIncrementalItems", .Array~of(html))
  self~assertTrue(out~pos('<li class="fragment afterPrevious">two <code>x</code></li>') > 0, -
    "the item carries afterPrevious, the span is gone, its content intact")
  self~assertTrue(out~pos('<li class="fragment withPrevious">four</li>') > 0, "and withPrevious")

::Method test_an_items_own_timing_beats_the_blocks
  html = '<div class="incremental withPrevious">'"0a"x"<ul>"||"0a"x -
         '<li><span class="afterPrevious">one</span></li>'"0a"x"</ul>"||"0a"x"</div>"
  out = self~run("MarkIncrementalItems", .Array~of(html))
  self~assertTrue(out~pos('<li class="fragment afterPrevious">one') > 0, "the item's word wins")
  self~assertEquals(0, out~pos("afterPrevious withPrevious"), "not both")

::Method test_a_timed_fragment_span_is_hoisted
  self~assertTrue(self~run("FragmentSpanAttrs", -
    .Array~of('class="fragment afterPrevious"')) \== 0, "fragment + afterPrevious is hoistable")
  self~assertEquals(0, self~run("FragmentSpanAttrs", -
    .Array~of('class="fragment keys"')), "an unrelated class still is not")

/* ApplyDefaultAnim finds "fragment" anywhere in the class list: level= puts  */
/* its class first (`class="level-2 fragment"`), and those items popped in   */
/* with no effect.                                                           */
::Method test_default_anim_reaches_a_fragment_not_named_first
  out = self~run("ApplyDefaultAnim", .Array~of('<div class="level-2 fragment">x</div>', "fade-up"))
  self~assertTrue(out~pos('class="level-2 fragment" data-anim="fade-up"') > 0, "level-first fragment animates")
  out = self~run("ApplyDefaultAnim", .Array~of('<div class="fragmentary">x</div>', "fade-up"))
  self~assertEquals(0, out~pos("data-anim"), "a class that only contains the letters is not a fragment")
  out = self~run("ApplyDefaultAnim", .Array~of('<div class="fragment" data-anim="zoom">x</div>', "fade-up"))
  self~assertEquals(1, out~countStr("data-anim"), "an own effect is kept, not doubled")

/* anim: page: effect: is the transition of every slide that names none     */
/* (the manual always said so; the build never read it -- 26-Sep).          */
::Method test_the_deck_page_effect_goes_to_slides_without_one
  stage = '<section class="slide" id="a">x</section>' || -
          '<section class="slide" id="b" data-anim="cut">y</section>'
  out = self~run("ApplyDefaultPageAnim", .Array~of(stage, "fade"))
  self~assertTrue(out~pos('id="a" data-anim="fade">') > 0, "a slide with none gets the deck's")
  self~assertTrue(out~pos('id="b" data-anim="cut">') > 0,  "a slide's own anim= wins")
  self~assertEquals(0, out~pos('data-anim="cut" data-anim'), "and gets no second one")

/* The runtime starts a slide's cascade by itself when its first step is     */
/* afterPrevious (Rony's feature request), from showSlide and from init.     */
::Method test_runtime_starts_a_leading_afterPrevious
  src = self~runtime
  self~assertTrue(src~pos("function startEntryChain(") > 0,   "the entry chain exists")
  self~assertTrue(src~pos("startEntryChain(slide);") > 0,     "showSlide starts it")
  self~assertTrue(src~pos("startEntryChain(slides[current]);") > 0, "and so does init")
  self~assertTrue(src~pos("!steps[0].after") > 0,             "only for a leading afterPrevious")

/* -- 23-Sep: short timing words ------------------------------------------ */
/* Rony: "these verbs get used a lot, and it is easy to mistype the 'ious'    */
/* portion without noticing it". The short forms and any case are spelled out */
/* right after Pandoc; a near miss is reported, since unknown it does nothing.*/

::Method test_short_timing_words_are_spelled_out
  html = '<div class="fragment afterPrev">a</div>' || "0a"x || -
         '<div class="fragment WithPrev">b</div>'  || "0a"x || -
         '<span class="afterprevious">c</span>'
  out = self~run("TimingAliases", .Array~of(html))
  self~assertTrue(out~pos('class="fragment afterPrevious"') > 0, ".afterPrev")
  self~assertTrue(out~pos('class="fragment withPrevious"') > 0,  ".WithPrev, any case")
  self~assertTrue(out~pos('class="afterPrevious"') > 0,          "on a span too")

::Method test_a_near_miss_is_reported_and_left_alone
  html = '<hr class="slide" /><div class="fragment afterPrevios">a</div>'
  sink    = .Said~new
  rtn     = self~md2slides~findRoutine("TIMINGALIASES")
  prev    = .Error~destination
  discard = .Error~destination(sink)
  Do
    out     = rtn~call(html)
  Finally
    discard = .Error~destination(prev)
  End
  self~assertTrue(out~pos('afterPrevios') > 0, "the class is not guessed at")
  self~assertTrue(sink~text~pos("slide 1: unknown class .afterPrevios") > 0, "it is reported, with its slide")
  self~assertTrue(sink~text~pos(".afterPrevious (or .afterPrev)") > 0, "with the right spellings")

::Method test_text_is_not_touched
  html = '<p>afterPrev and withPrev in prose</p>'
  self~assertEquals(html, self~run("TimingAliases", .Array~of(html)), "only class values are rewritten")

/* -- 23-Sep (2): timing on a block, and .afterClick ----------------------- */
/* `::: {.contents scale=0.85 .afterPrevious}` around the first fragment: the */
/* word goes to the first fragment inside, unless it has its own. afterClick  */
/* is the default said out loud, and counts as a fragment's own timing.       */

::Method test_block_timing_moves_to_its_first_fragment
  html = '<section class="slide"><div class="contents afterPrevious" style="x">' || -
         '<div class="fragment">a</div><div class="fragment">b</div></div></section>'
  out = self~run("HoistContainerTiming", .Array~of(html))
  self~assertTrue(out~pos('<div class="contents" style="x">') > 0, "the block loses it")
  self~assertTrue(out~pos('<div class="fragment afterPrevious">a') > 0, "its first fragment gets it")
  self~assertTrue(out~pos('<div class="fragment">b') > 0, "the second keeps its press")

::Method test_a_fragments_own_timing_wins
  html = '<div class="contents withPrevious"><div class="fragment afterClick">a</div></div>'
  out = self~run("HoistContainerTiming", .Array~of(html))
  self~assertTrue(out~pos('<div class="fragment afterClick">a') > 0, "afterClick is its own timing")
  self~assertEquals(0, out~pos("withPrevious"), "and the block's word is dropped")

::Method test_a_block_with_no_fragment_is_reported
  html = '<section class="slide"><div class="contents afterPrevious"><p>t</p></div></section>'
  sink    = .Said~new
  rtn     = self~md2slides~findRoutine("HOISTCONTAINERTIMING")
  prev    = .Error~destination
  discard = .Error~destination(sink)
  Do
    out     = rtn~call(html)
  Finally
    discard = .Error~destination(prev)
  End
  self~assertTrue(sink~text~pos("slide 1: .afterPrevious is on a block with no fragment") > 0, "reported")

::Method test_afterClick_is_a_timing_word
  self~assertTrue(WordPos("afterClick", self~run("TimingClasses", .Array~new)) > 0, "listed")
  out = self~run("TimingAliases", .Array~of('<div class="fragment AFTERCLICK">a</div>'))
  self~assertTrue(out~pos('class="fragment afterClick"') > 0, "any case")

/* appear= (Rony, 24-Sep): one timing word for every step of a block, said  */
/* once. An item's own word wins, and so does the block's own word on its    */
/* first step; a nested appear= wins inside its block.                      */
::Method test_appear_times_every_item_of_an_incremental
  html = '<div class="incremental withPrevious" data-appear="afterPrev">'"0a"x"<ul>"||"0a"x -
         "<li>one</li>"||"0a"x"<li>[two]</li>"||"0a"x"<li>three</li>"||"0a"x"</ul>"||"0a"x"</div>"
  html = html~changeStr("<li>[two]</li>", '<li class="fragment afterClick">two</li>')
  out = self~run("MarkIncrementalItems", .Array~of(html))
  self~assertTrue(out~pos('<li class="fragment withPrevious">one') > 0,   "the block's own word on the first")
  self~assertTrue(out~pos('afterClick">two') > 0,                         "an item's own word wins")
  self~assertTrue(out~pos('<li class="fragment afterPrevious">three') > 0, "the rest take appear=")

::Method test_appear_times_every_fragment_in_a_block
  html = '<section class="slide"><div class="contents" data-appear=".withPrev">' -
         '<div class="fragment">a</div>' -
         '<div class="fragment" data-appear="afterClick"><div class="fragment">b</div></div>' -
         '<div class="fragment afterPrevious">c</div></div></section>'
  out = self~run("ApplyAppear", .Array~of(html))
  self~assertEquals(0, out~pos("data-appear"), "the attribute is consumed")
  self~assertTrue(out~pos('<div class="fragment withPrevious">a') > 0,  "a fragment inside")
  self~assertTrue(out~pos('<div class="fragment afterClick"><div class="fragment afterClick">b') > 0, -
    "a nested appear= wins inside its block, and times that block too")
  self~assertTrue(out~pos('<div class="fragment afterPrevious">c') > 0, "an own word wins")

::Method test_a_bad_appear_is_reported
  html = '<section class="slide"><div class="fragment" data-appear="soon">a</div></section>'
  sink    = .Said~new
  rtn     = self~md2slides~findRoutine("APPLYAPPEAR")
  prev    = .Error~destination
  discard = .Error~destination(sink)
  Do
    out     = rtn~call(html)
  Finally
    discard = .Error~destination(prev)
  End
  self~assertTrue(sink~text~pos("slide 1: appear=soon is not a timing word") > 0, "reported")
  self~assertTrue(out~pos('<div class="fragment">a') > 0, "and nothing is timed")

::Method runtime
  rt  = self~projectRoot"/bin/md2slides/js/runtime.js"
  src = CharIn(rt, 1, Stream(rt, "C", "QUERY SIZE"))
  Call Stream rt, "C", "CLOSE"
  Return src

::Method run
  Use Strict Arg name, args
  Return self~md2slides~findRoutine(name~upper)~callWith(args)

::Method md2slides
  pkg = .local["FRAGMENTS.TEST.MD2SLIDES"]
  If pkg \== .Nil Then Return pkg
  prev    = .Error~destination
  discard = .Error~destination(.QuietSink~new)
  Do
    pkg     = .Package~new(self~projectRoot"/bin/md2slides.rex")
  Finally
    discard = .Error~destination(prev)
  End
  .local["FRAGMENTS.TEST.MD2SLIDES"] = pkg
  Return pkg

::Method projectRoot
  /* This file is <root>/tests/suites/Fragments.testGroup. */
  Parse Source . . me
  Return .File~new(.File~new(me)~parent)~parentFile~parent

::Class Said

::Method init
  Expose text
  text = ""

::Method text
  Expose text
  Return text

::Method lineOut
  Expose text
  Use Arg line
  text = text || line
  Return 0

::Method say
  Expose text
  Use Arg line
  text = text || line
  Return 0

::Method charOut
  Expose text
  Use Arg chars
  text = text || chars
  Return 0

::Class QuietSink

::Method lineOut
  Return 0

::Method say
  Return 0

::Method charOut
  Return 0
