Files
donlucasandClaude Fable 5.1 4b86a5ac9b Four hygiene items from Kimi's assessment: a test that never ran, two log switches unscrubbed, an interrupt taken for a failure, a portability skip
The companion test for the near pose OFF skipped whenever Near was not
zero, and unset XSCAPES_NEAR has meant rung 2 since 2026-09-11, so it
skipped every run; it sets the pose off itself now. The test harness
scrubs XSCAPES_HOOKLOG and XSCAPES_EVENTLOG as it scrubs HOME and TRACE,
so no test can append to the logs of whoever runs the suite inside a
scape. A StopFailure flagged is_interrupt is the user's Esc and becomes an
Interrupt, as PostToolUseFailure's flag already did, instead of a worried
owl. The installer test skips rather than fails on a box whose Go lives
in /usr/bin.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-18 13:19:48 -07:00

676 lines
27 KiB
Go

package main
import (
"fmt"
"math"
"testing"
"time"
"github.com/donlucasx/xscapes/internal/canvas"
"github.com/donlucasx/xscapes/internal/companion"
"github.com/donlucasx/xscapes/internal/reduce"
"github.com/donlucasx/xscapes/internal/scape"
"github.com/donlucasx/xscapes/internal/term"
)
// THE COMPANION COMES CLOSER TO ASK, and it must not walk through the litter.
//
// His instruction, 2026-09-10: "it should walk up closer to prompt, without
// overlapping sub agents. OK if a part of its body is cut on the bottom of the
// screen." The idea itself, earlier: when the agent needs him the companion
// should "walk up closer to the screen and get bigger".
//
// Everything in this file reads the RENDERED frame rather than the arithmetic
// that produced it, for the reason bubble_aim_test.go gives: a glyph the layer
// holds can still lose to the resolver, and the composition is what he sees.
//
// HOW THE OVERLAP IS MEASURED, because "did the litter collide" has an obvious
// wrong way to ask it. Nothing here transcribes drawScene's placement: each
// case renders the SAME frame twice, once with the thing under test present and
// once with it absent, and takes the cells that CHANGED as that thing's cells.
// A transcription measures a different composition, and drawScene draws the
// companion BEFORE the litter, so whoever wins the cell tells you nothing about
// whether two animals are standing in the same place.
// hisWidths are the window widths he has actually run at, from the session
// banners, plus the brief's 40-column floor.
var hisWidths = []int{40, 80, 111, 124, 143, 153}
// hisHeights pairs a plausible height with each width. The near pose is allowed
// to run off the BOTTOM (his ruling), so the height is load-bearing: it decides
// how many of the sprite's rows exist at all.
var hisHeights = map[int]int{40: 20, 80: 24, 111: 25, 124: 22, 143: 27, 153: 51}
// hisLitters are his real subagent counts. The peak in his recordings is 38 and
// the p90 is 14; 1 and 4 are the common cases and 8 is the size at which the
// ladder drops a rung.
var hisLitters = []int{1, 4, 8, 14, 38}
// nearScene is one composed frame, rendered the way frames.frame renders it.
//
// rung is how far the approach has climbed: 0 is the shipped 12x7 box, 1 the
// 16x9 pose, 2 the 24x14 one. It is reached by calling Approach with real
// seconds rather than by reaching into the companion, so the test exercises the
// same path the live loop does.
type nearScene struct {
c *canvas.Canvas
cat *companion.Cat
lay layout
top int
// box is DrawnBox, CAPTURED WHILE THE FLAG IS STILL SET. Reading it off
// the returned Cat instead is how the first version of this file measured
// the shipped 12x7 box at every rung and reported the near pose as
// harmless: companion.Near is a package variable that renderNear restores
// on the way out, and nearSteps() returns 0 the moment it is back to zero.
box [3]int
w, h int
sandTop int
}
func renderNear(t *testing.T, name string, w, h, rung int, st reduce.State) nearScene {
t.Helper()
old := companion.Near
companion.Near = rung
defer func() { companion.Near = old }()
c := canvas.New(w, h, canvas.AlphaFar, canvas.AlphaMid, canvas.AlphaNear)
sh := scape.NewShore(7, false)
cat := companion.New(name)
cat.FaceLeft(true)
ccw, chh := cat.Size()
lay := compose(w, ccw, true)
sh.MoonX = lay.MoonX
// Walk it all the way in. One rung is nearStep = 0.28 s and the ladder is
// at most two rungs, so a second of frames is more than enough; Approach
// clamps at 1 and stays there.
if rung > 0 {
for i := 0; i < 60; i++ {
cat.Approach(0.05, true)
}
}
bdx, bw, bh := cat.DrawnBox(w)
if rung > 0 && name == companion.NameCrab && bdx == 0 {
t.Fatalf("rung %d: the approach never left home -- DrawnBox dx is still 0, "+
"so every overlap number below would be measuring the shipped box", rung)
}
sh.Update(c, 2.0, st.Act)
top := c.H - 2 - chh
drawScene(c, sh, cat, lay, st, 2.0, 7, top)
return nearScene{c: c, cat: cat, lay: lay, top: top,
box: [3]int{bdx, bw, bh}, w: w, h: h, sandTop: sh.SandTop()}
}
// coatSpan is the columns the companion's own ink occupies, found by COLOUR.
//
// ⚠ THE OBVIOUS WAYS TO FIND IT BOTH FAIL, and the first version of this file
// shipped one of them. Reading the eye glyphs back over the companion's row
// band catches the activity tail too -- the sand's last line shares the band's
// bottom row and is full of the letter o, so the leftmost "eye" was a letter in
// a filename and sat still no matter what the companion did. The whole test
// then passed with the defect put back. Taking every inked cell in the band is
// worse: the shore paints foam and wave glyphs into the NEAR layer, so the span
// comes back as the full width of the frame (measured: 0..39 of 40, 0..123 of
// 124).
//
// CrabCoat is salmon, cube index 210, and nothing else in the scene is salmon.
// Read off the raw layer rather than through ResolveAt, so it is the authored
// colour and not a quantised one.
func coatSpan(s nearScene) (lo, hi, n int) {
lo, hi = 1<<30, -1
l := s.c.Near()
for y := s.top; y < s.top+s.box[2] && y < l.H; y++ {
for x := 0; x < l.W; x++ {
cell := l.Cells[y*l.W+x]
if !cell.Set || cell.R == ' ' || cell.FG != companion.CrabCoat {
continue
}
if x < lo {
lo = x
}
if x > hi {
hi = x
}
n++
}
}
return lo, hi, n
}
// inked is every cell of the near layer that carries a glyph. plotRim writes a
// SPACE to clear a ring around each sprite, and a cleared cell is not ink --
// counting it would report two animals as colliding whenever their rims touch,
// which is the legal case.
func inked(c *canvas.Canvas) map[[2]int]rune {
out := map[[2]int]rune{}
l := c.Near()
for y := 0; y < l.H; y++ {
for x := 0; x < l.W; x++ {
cell := l.Cells[y*l.W+x]
if cell.Set && cell.R != ' ' && cell.R != 0 {
out[[2]int{x, y}] = cell.R
}
}
}
return out
}
// askState is a realistic frame at the moment of an ask: the activity level his
// recordings actually show when a permission prompt goes up (p50 0.59), a
// context reading that puts the readout on screen, and a tail long enough to
// reach as far right as the sand is allowed to go.
func askState(pose companion.State, steps int, age float64, kittens int) reduce.State {
st := reduce.State{
Act: scape.Activity{
Working: true, Level: 0.59, ContextUsed: 0.62,
TimeOfDay: 11.0 / 24, TodoDone: 3, TodoTotal: 8,
},
Pose: pose, Steps: steps, StepAge: age, Kittens: kittens,
Tail: []reduce.Line{
{Text: "read internal/companion/crab_near.go 612 lines wide enough to reach the margin"},
{Text: "edit internal/companion/crab_near.go +118 -2 and still going on past here", Age: 0.3},
{Text: "bash go test ./internal/... -count=1 ok 4.412s with room to spare on the end", Age: 0.6},
},
}
if pose == companion.NeedsYou {
st.Bubble, st.BubbleAsk = "allow Bash?", true
}
if pose == companion.Done {
st.Bubble = "tests passed"
}
return st
}
// ---------------------------------------------------------------------------
// 1. THE PACE DEFECT
// The pose changing on its own does not move the companion one cell.
//
// ⭐ IT USED TO MOVE IT UP TO SIX. pace() answered every held pose with dx = 0,
// and 0 is HOME -- the column nearest the frame EDGE -- so at the exact frame
// the agent asked him for something the companion teleported AWAY from him, and
// teleported back when he answered. Measured through the shipped pace() on a
// real reducer before the fix (notes/s28-pace): 6 cells at 111, 124, 143 and
// 153 columns, 5 at 80, 2 at 40; 56 of 68 (width, step-count) pairs moved.
//
// This is the opposite of his idea, which is that the companion comes CLOSER
// before it prompts, so it is a defect fix and it ships with no switch.
//
// Read off the painted frame by the coat's colour -- see coatSpan for the two
// ways of locating the companion that look right and are not.
func TestThePoseChangeAloneDoesNotMoveTheCompanion(t *testing.T) {
checked, moved, worst := 0, 0, 0
for _, w := range hisWidths {
h := hisHeights[w]
span := paceSpan(w)
for steps := 0; steps <= 2*span+1; steps++ {
for _, age := range []float64{0, 0.14, 0.5} {
wasLo, wasHi, wasN := coatSpan(renderNear(t, "crab", w, h, 0,
askState(companion.Working, steps, age, 0)))
if wasN == 0 {
t.Fatalf("w=%d steps=%d: the companion has no coat cells at all -- the probe "+
"is empty, and a clean result from an empty probe looks exactly like a pass",
w, steps)
}
for _, pose := range []companion.State{companion.NeedsYou, companion.Done, companion.Worried} {
lo, hi, n := coatSpan(renderNear(t, "crab", w, h, 0, askState(pose, steps, age, 0)))
checked++
if n == 0 {
t.Fatalf("w=%d steps=%d pose=%v: no coat cells", w, steps, pose)
}
// The POSE changes the art, so the ink's right edge and its
// count legitimately differ -- a raised claw is not a
// lowered one. Its left edge is the box, and only the box.
if lo != wasLo {
moved++
if d := lo - wasLo; d > worst || -d > worst {
if d < 0 {
d = -d
}
worst = d
}
if moved < 6 {
t.Errorf("w=%d steps=%d age=%.2f: the companion's ink starts at column %d "+
"while working and at %d once the pose is %v -- it moved %d cells for "+
"no reason but the pose changing",
w, steps, age, wasLo, lo, pose, lo-wasLo)
}
}
_ = hi
_ = wasHi
}
}
}
}
t.Logf("%d pose changes over %d widths, %d step counts and 3 stride phases: "+
"%d moved the companion, worst %d cells", checked, len(hisWidths), 2*6+2, moved, worst)
if moved > 0 {
t.Errorf("%d of %d pose changes teleport the companion", moved, checked)
}
}
// The balloon's pointer still comes out of the face once the companion has
// paced away from home.
//
// bubble_aim_test.go cannot see this: it builds its state with demoState, which
// leaves Steps at 0, so the companion is at home and the balloon's old anchor
// -- lay.CatX, the box's HOME column -- happened to be right. The pace fix is
// what makes home and here different while a balloon is up, and an anchor on
// home would put the `v` up to six cells off the head. That is the same miss
// s27 measured and fixed from the other direction.
func TestTheBalloonPointsAtTheCompanionAfterItHasPaced(t *testing.T) {
for _, w := range hisWidths {
h := hisHeights[w]
span := paceSpan(w)
for steps := 0; steps <= 2*span; steps++ {
s := renderNear(t, "crab", w, h, 0, askState(companion.NeedsYou, steps, 0.5, 0))
read := func(y int, want rune) []int {
var cols []int
for x := 0; x < w; x++ {
if r, _, _ := s.c.ResolveAt(x, y, term.Profile256); r == want {
cols = append(cols, x)
}
}
return cols
}
tail := read(s.top-1, 'v')
eye := read(s.top+2, 'O')
if len(tail) != 1 || len(eye) != 2 {
t.Fatalf("w=%d steps=%d: %d pointers and %d open eyes, want 1 and 2",
w, steps, len(tail), len(eye))
}
if tail[0] < eye[0] || tail[0] > eye[1] {
t.Errorf("w=%d steps=%d: the pointer is on column %d, outside the eyes at %d and %d "+
"-- the balloon is speaking from bare sand",
w, steps, tail[0], eye[0], eye[1])
}
}
}
}
// ---------------------------------------------------------------------------
// 2. THE LITTER
// litterCells is every cell the litter puts on the frame, taken as the
// difference between the same frame with and without subagents. No placement
// arithmetic is copied out of drawScene, so this cannot drift from it.
func litterCells(t *testing.T, name string, w, h, rung int, pose companion.State, steps, kittens int) map[[2]int]rune {
t.Helper()
bare := inked(renderNear(t, name, w, h, rung, askState(pose, steps, 0.5, 0)).c)
full := inked(renderNear(t, name, w, h, rung, askState(pose, steps, 0.5, kittens)).c)
out := map[[2]int]rune{}
for cell, r := range full {
if was, ok := bare[cell]; !ok || was != r {
out[cell] = r
}
}
return out
}
// No subagent on the SAND is ever standing where the companion is standing, and
// coming forward does not put one there.
//
// HIS INSTRUCTION, verbatim: "it should walk up closer to prompt, without
// overlapping sub agents."
//
// The box, not the ink: a crablet inside the companion's drawn rectangle is a
// collision even where the companion's own art happens to leave that cell
// empty, because the next frame's breath fills it. plotRim already guarantees a
// clear ring, so this is the rule the composition was built for.
//
// ⚠ SITTERS AND SWIMMERS ARE DIFFERENT DEFECTS, and measuring them together
// would have hidden the answer to his question inside one that is not mine.
// A SITTER in the box is what he asked about, and the companion is the only
// thing that can push one: sitters are placed leftward from the anchor
// drawScene hands DrawKittens. A SWIMMER in the box is the waterline reaching
// the companion's head -- crab swimmers are placed across the WHOLE width, in
// sea lanes, and nothing about the companion moves them.
//
// ⭐ AND THE SWIMMERS ALREADY COLLIDE, with the near pose OFF. Measured below
// at rung 0, which is today's shipped frame: 21 of 90 cases, every overlapping
// cell above the waterline, at 40, 80 and 124 columns with eight or more
// subagents. It is the other half of the thing s27 logged -- "swimmers follow
// the tide; sitters do not" -- seen from the companion's side, and it is NOT
// this build's to fix. What this test holds is that coming forward makes it no
// worse.
func TestNoSubagentStandsInTheCompanionsDrawnBox(t *testing.T) {
type key struct {
w, steps, n int
}
base := map[key]int{}
cases, dryHits := 0, 0
wetAt := map[int]int{}
worse := 0
for _, rung := range []int{0, 1, 2} {
for _, w := range hisWidths {
h := hisHeights[w]
span := paceSpan(w)
for _, steps := range []int{0, span, 2 * span} {
for _, n := range hisLitters {
st := askState(companion.NeedsYou, steps, 0.5, n)
s := renderNear(t, "crab", w, h, rung, st)
bdx, bw, bh := s.box[0], s.box[1], s.box[2]
dx, _ := pace(st, w)
catX := s.lay.CatX + int(math.Round(dx))
inBox := func(x, y int) bool {
return x >= catX+bdx && x < catX+bdx+bw && y >= s.top && y < s.top+bh
}
dry, wet := 0, 0
for cell := range litterCells(t, "crab", w, h, rung, companion.NeedsYou, steps, n) {
if !inBox(cell[0], cell[1]) {
continue
}
if cell[1] < s.sandTop {
wet++ // a swimmer: the sea is over the companion's head
} else {
dry++ // a sitter: his question
}
}
cases++
wetAt[rung] += wet
if rung == 0 {
base[key{w, steps, n}] = wet
} else if wet > base[key{w, steps, n}] {
worse++
}
if dry > 0 {
dryHits++
t.Errorf("rung %d w=%d steps=%d subagents=%d: %d SITTER cells inside the "+
"companion's drawn box [%d..%d]x[%d..%d] -- he asked for exactly this "+
"not to happen",
rung, w, steps, n, dry, catX+bdx, catX+bdx+bw-1, s.top, s.top+bh-1)
}
}
}
}
}
t.Logf("%d cases (3 rungs x %d widths x 3 step counts x %d litter sizes): "+
"%d put a SITTER under the companion", cases, len(hisWidths), len(hisLitters), dryHits)
// ⚠ THE SWIMMERS, REPORTED AND NOT FIXED HERE. This is a separate defect
// in a file this build does not own, and it is on the record so the next
// session does not rediscover it as if it were new.
//
// Crab swimmers are placed across the whole sea by crabSwimSpans, which
// takes the frame width and knows nothing about the companion, and
// drawScene draws the litter AFTER the companion -- so a crablet whose
// lane crosses the companion's upper rows is painted over its head. With
// the near pose OFF that already happens in 21 of 90 cases. Coming forward
// widens the box six columns to the LEFT into the same sea rows, so it
// catches more of them.
//
// The one-line fix is a draw ORDER change in drawScene -- the companion
// after the litter rather than before -- which would also make the crab
// occlude a crablet that swims behind it, which is the correct depth. It
// is not taken here because it changes frames with XSCAPES_NEAR unset, and
// nothing in this build is allowed to do that without his ruling.
if wetAt[0] == 0 {
t.Fatalf("no swimmer ever reaches the companion's box, at any rung -- then the "+
"baseline below is an empty probe and the comparison means nothing "+
"(rung 1 %d, rung 2 %d)", wetAt[1], wetAt[2])
}
t.Logf("⚠ PRE-EXISTING, not fixed here: swimmer cells inside the companion's box are "+
"rung 0 (today's shipped frame) %d, rung 1 %d, rung 2 %d; coming forward makes it "+
"worse in %d of %d cases. Swimmers are placed across the whole sea and the "+
"companion cannot move them.", wetAt[0], wetAt[1], wetAt[2], worse, cases-90)
}
// ⚠ MUTATION CHECK for the test above, and the mutation is the REAL previous
// rule rather than an invented one.
//
// drawScene used to anchor the litter at lay.CatX - lay.PaceSpan and nothing
// else. That is one column clear of the furthest the companion could PACE, and
// it was right for as long as the sprite was always twelve cells wide. A near
// pose reaches (W-12)/2 further left -- 2 cells at rung 1, 6 at rung 2 -- so
// the old rule walks the companion straight through the crablets.
//
// This asserts the old rule FAILS. A collision test that has never been seen to
// go red is a test that proves nothing, and this one is cheap to fool: the
// litter is placed by a hash and can simply not be there.
func TestTheOldLitterAnchorWouldCollide(t *testing.T) {
const w, h = 124, 22
span := paceSpan(w)
worst := 0
for _, rung := range []int{1, 2} {
for _, n := range hisLitters {
s := renderNear(t, "crab", w, h, rung, askState(companion.NeedsYou, span, 0.5, n))
bdx, bw := s.box[0], s.box[1]
dx, _ := pace(askState(companion.NeedsYou, span, 0.5, n), w)
catX := s.lay.CatX + int(math.Round(dx))
// The OLD anchor, transcribed from the line this replaced.
oldLitterX := s.lay.CatX - s.lay.PaceSpan
// A sitter's rightmost cell is one clear column inside the anchor
// (internal/companion/crab_kittens.go:138, x = px-1-cw-n*(cw+1)).
oldRight := oldLitterX - 2
nearLeft := catX + bdx
if over := oldRight - nearLeft + 1; over > worst {
worst = over
}
_ = bw
}
}
if worst <= 0 {
t.Fatalf("the old anchor does not overlap the near pose at any rung, so "+
"TestNoSubagentStandsInTheCompanionsDrawnBox could be passing for free "+
"(worst overlap %d cells)", worst)
}
t.Logf("the old anchor overlaps the near pose by up to %d columns at %d wide -- "+
"the collision test has something to catch", worst, w)
}
// ---------------------------------------------------------------------------
// 3. THE SAND
// The activity tail never writes through the companion, and it keeps a usable
// column budget while doing it.
//
// drawSand runs LAST and canvas.Plot REPLACES a cell, so a line long enough to
// reach under a companion that has come forward would punch its own letters
// through the body. It could not happen before: SandTo is CatX-1-PaceSpan,
// one clear column past the furthest the companion could pace.
//
// The budget is stated rather than merely asserted, because the alternative
// rule s28 measured -- reserving the litter's whole span from the sand -- left
// 8 of 56 columns at 80 wide with fourteen subagents, and a fix that costs more
// than that is not a fix.
func TestTheSandKeepsClearOfTheCompanionAndStillHasRoom(t *testing.T) {
for _, rung := range []int{0, 1, 2} {
for _, w := range hisWidths {
h := hisHeights[w]
span := paceSpan(w)
for _, steps := range []int{0, span, 2 * span} {
st := askState(companion.NeedsYou, steps, 0.5, 0)
s := renderNear(t, "crab", w, h, rung, st)
bdx, bw, bh := s.box[0], s.box[1], s.box[2]
dx, _ := pace(st, w)
catX := s.lay.CatX + int(math.Round(dx))
// The sand's cells, taken the same way the litter's are.
noTail := st
noTail.Tail = nil
bare := inked(renderNear(t, "crab", w, h, rung, noTail).c)
full := inked(s.c)
in := 0
for cell, r := range full {
if was, ok := bare[cell]; ok && was == r {
continue
}
x, y := cell[0], cell[1]
if x >= catX+bdx && x < catX+bdx+bw && y >= s.top && y < s.top+bh {
in++
}
}
if in > 0 {
t.Errorf("rung %d w=%d steps=%d: %d cells of the activity tail land inside the "+
"companion's drawn box", rung, w, steps, in)
}
// And what it cost. Recomputed the way drawScene does it, then
// reported rather than merely bounded.
nearLeft := catX + bdx
sandTo := s.lay.SandTo
if nearLeft-1 < sandTo {
sandTo = nearLeft - 1
}
budget := sandTo - s.lay.SandFrom
shipped := s.lay.SandTo - s.lay.SandFrom
if steps == span && rung == 2 {
t.Logf("w=%d rung 2, paced fully in: the tail gets %d of its %d columns (%+d)",
w, budget, shipped, budget-shipped)
}
// drawSand gives up under twelve columns, which would drop the
// channel entirely. At his narrowest width that must not happen.
if w >= 80 && budget < 12 {
t.Errorf("rung %d w=%d steps=%d: the tail is down to %d columns, and drawSand "+
"writes nothing under 12", rung, w, steps, budget)
}
}
}
}
}
// ---------------------------------------------------------------------------
// 4. OFF BY DEFAULT
// With XSCAPES_NEAR unset the companion is the shipped 12x7 box, always, and
// every line drawScene added collapses to the expression it replaced.
//
// This is the arithmetic half of the proof. The rendered half is in the session
// notes: 3,780 composed frames -- two companions, six widths, five poses, seven
// step counts, three stride phases, three litter sizes -- were hashed at HEAD
// and again here, and the set that differs is EXACTLY the set where the pose is
// held and the companion was not standing at home. 0 of 1,512 resting and
// working frames differ; 0 differ for any reason but the teleport fix.
func TestWithTheNearPoseOffTheCompanionKeepsItsShippedBox(t *testing.T) {
// The pose is OFF here by hand: unset XSCAPES_NEAR has meant rung 2
// since 2026-09-11, so a guard that skipped whenever Near != 0 skipped
// every run of the suite (Kimi's assessment, 2026-09-18).
was := companion.Near
companion.Near = 0
defer func() { companion.Near = was }()
for _, name := range []string{"crab", "cat"} {
cat := companion.New(name)
cat.FaceLeft(true)
for i := 0; i < 200; i++ {
cat.Approach(0.05, true)
dx, bw, bh := cat.DrawnBox(200)
if dx != 0 || bw != 12 || bh != 7 {
t.Fatalf("%s: after %d approach frames DrawnBox is (%d,%d,%d), want (0,12,7) -- "+
"the near pose is not off", name, i+1, dx, bw, bh)
}
}
w, h := cat.Size()
if w != 12 || h != 7 {
t.Errorf("%s: Size() is %dx%d, want 12x7 -- Size() is the LAYOUT contract "+
"and compose() derives the margin, the litter, SandTo and the moon from it", name, w, h)
}
}
// And the two derived columns reduce to the lines they replaced.
for _, w := range hisWidths {
lay := compose(w, 12, true)
for steps := 0; steps <= 2*paceSpan(w); steps++ {
dx, _ := pace(askState(companion.NeedsYou, steps, 0.5, 0), w)
catX := lay.CatX + int(math.Round(dx))
nearLeft := catX + 0 // DrawnBox dx, proven 0 above
litterX := lay.CatX - lay.PaceSpan
if nearLeft < litterX {
litterX = nearLeft
}
if litterX != lay.CatX-lay.PaceSpan {
t.Errorf("w=%d steps=%d: the litter moved to %d with the near pose OFF, "+
"shipped is %d", w, steps, litterX, lay.CatX-lay.PaceSpan)
}
sandTo := lay.SandTo
if nearLeft-1 < sandTo {
sandTo = nearLeft - 1
}
if sandTo != lay.SandTo {
t.Errorf("w=%d steps=%d: the sand shortened to %d with the near pose OFF, "+
"shipped is %d", w, steps, sandTo, lay.SandTo)
}
}
}
}
// The live loop actually drives the approach, and it drives the retreat too.
//
// Everything else in this file calls Approach itself, which would keep passing
// if frames.frame never called it at all -- the wiring is the one thing those
// tests cannot see. This runs the real per-frame path: frames.frame, its own
// clock, its own dt, and the pose coming out of the state it builds.
//
// It uses the DEMO cycle rather than a reducer because the demo is what runs
// with no session attached and it swings through every pose on a fixed timer:
// phase 2 of every 32 seconds is NeedsYou with an ask balloon up, phase 3 is
// Done. So the ask arrives and is answered without a single event being faked.
func TestTheLiveLoopWalksTheCompanionInAndBackOutAgain(t *testing.T) {
old := companion.Near
companion.Near = 2
defer func() { companion.Near = old }()
t.Setenv("XSCAPES_COMPANION", companion.NameCrab)
f := newFrames(124, 22, 7, false, true, 0.3, 11.0/24)
if f.cat.Name() != companion.NameCrab {
t.Fatalf("the scape came up with the %s; the near pose is Hero's", f.cat.Name())
}
// 20 fps, the rate main.go uses. The point is that Approach gets real
// seconds and not a frame count: -fps is a flag and the two launchers
// already disagree (12 in inside.go, 20 in main.go).
const fps = 20.0
widest, atAsk := 0, 0
seen := map[int]bool{}
for i := 0; i < int(32*fps); i++ {
now := f.start.Add(time.Duration(float64(i) / fps * float64(time.Second)))
f.frame(now)
_, bw, _ := f.cat.DrawnBox(f.c.W)
seen[bw] = true
if bw > widest {
widest = bw
}
// Phase 2 of the demo cycle is the ask: t in [16, 24).
if s := float64(i) / fps; s >= 23 && s < 24 {
atAsk = bw
}
}
if atAsk != 24 {
t.Errorf("a second before the ask is answered the companion is %d cells wide, want 24 -- "+
"the approach is not being driven by the live loop", atAsk)
}
if !seen[12] || !seen[16] || !seen[24] {
t.Errorf("the walk did not climb the whole ladder: widths seen were %v, want 12, 16 and 24", seen)
}
// And it comes back. Done is not an ask, so the retreat runs on the same
// call, and the frame after the cycle turns over must be back at the box.
for i := 0; i < int(3*fps); i++ {
now := f.start.Add(time.Duration((24 + float64(i)/fps) * float64(time.Second)))
f.frame(now)
}
if _, bw, _ := f.cat.DrawnBox(f.c.W); bw != 12 {
t.Errorf("three seconds after the ask was answered the companion is still %d cells wide, "+
"want the shipped 12 -- it walked up and never walked back", bw)
}
}
// The near pose actually changes the frame when it IS switched on, so the test
// above is not passing because nothing in this file does anything.
func TestTheNearPoseChangesTheFrameWhenItIsOn(t *testing.T) {
const w, h = 124, 22
off := renderNear(t, "crab", w, h, 0, askState(companion.NeedsYou, paceSpan(w), 0.5, 4))
same := 0
for _, rung := range []int{1, 2} {
on := renderNear(t, "crab", w, h, rung, askState(companion.NeedsYou, paceSpan(w), 0.5, 4))
if off.c.Render(term.Profile256) == on.c.Render(term.Profile256) {
same++
t.Errorf("rung %d renders the same frame as the shipped box", rung)
}
bw, bh := on.box[1], on.box[2]
t.Log(fmt.Sprintf("rung %d draws %dx%d", rung, bw, bh))
}
if same > 0 {
t.Error("the near pose is inert; every off-by-default result above is vacuous")
}
}