Commit a multi-jump turn with push
Bob sends 11 and then 18 on checkers/jump. The worker collects the path while the board still shows all three pieces. When Bob finishes the input stream, the worker submits both landings to the double-jump mutation. It checks both captures against the current board and commits them together. The worker then publishes the animation path and replies to Bob.
Go example
func push(t *testing.T, ctx context.Context, db *stardust.Database) {
worker := must(stardust.Accept[choice](db, "push", "checkers/jump"))
defer worker.Close()
before := board(db)
call := must(stardust.OpenExchange[bool](ctx, db, "push", "checkers/jump"))
defer call.Close()
open := must(worker.Next(ctx))
for _, square := range []int64{11, 18} {
check(call.Send(choice{2, square}))
}
check(call.Finish())
var path []int64
for {
step := must(worker.Next(ctx))
if step.Handle != open.Handle {
t.Fatal("unexpected exchange", step)
}
if step.Kind == "finish" {
break
}
if step.Kind != "message" || step.Data.From != 2 || len(path) >= 12 {
check(worker.Cancel(open.Handle))
t.Fatal("invalid jump draft", step)
}
path = append(path, step.Data.Landing)
if !slices.Equal(board(db), before) {
t.Fatal("partial jump was committed")
}
}
if len(path) != 2 {
t.Fatal("this turn needs two landings", path)
}
// The first capture must roll back if the second landing is illegal.
rejected := must(stardust.CallAs[stardust.MutationResult](db, table, "double-jump", 2, path[0], 19))
if rejected.Transaction != nil || !slices.Equal(board(db), before) {
t.Fatal("invalid second landing changed the board")
}
result := must(stardust.CallAs[stardust.MutationResult](db, table, "double-jump", 2, path[0], path[1]))
if result.Transaction == nil {
t.Fatal("jump rejected")
}
must(stardust.Publish(db, "checkers/table/turn", path[1]))
must(stardust.Publish(db, "checkers/table/animate", append([]int64{2}, path...)))
check(worker.Send(open.Handle, true))
if !must(call.Next(ctx)).Data || must(call.Next(ctx)).Kind != "complete" {
t.Fatal("incomplete push")
}
}