package ingest import ( "bytes" "context" "os" "path/filepath" "strconv" "strings" "testing" ) // fakeEngine writes a shell script that behaves like tmctl's contract: NDJSON on stdout, human log // on stderr, and one of the ratified exit codes. The supervision path is exercised with a real // process — a mocked one would prove nothing about pipes, signals or exit codes. func fakeEngine(t *testing.T, stdout, stderr string, code int) string { t.Helper() path := filepath.Join(t.TempDir(), "tmctl") script := "#!/bin/sh\nprintf '%s' " + shellQuote(stdout) + "\nprintf '%s' " + shellQuote(stderr) + " >&2\nexit " + strconv.Itoa(code) + "\n" if err := os.WriteFile(path, []byte(script), 0o755); err != nil { t.Fatal(err) } return path } func TestRunIngestsStreamAndMapsExitCode(t *testing.T) { stream := helloLine + "\n" + `{"seq":2,"type":"bank_stop","data":{"terms_proposed":7}}` + "\n" var engineLog bytes.Buffer s := &Supervisor{ Bin: fakeEngine(t, stream, "bank-mining: new terms await owner signature\n", 3), Workdir: t.TempDir(), EngineLog: &engineLog, } sink := &recordingSink{} got, err := s.Run(context.Background(), sink, "translate", "--verify-bank") if err != nil { t.Fatalf("run: %v", err) } if got != OutcomeBankStop { t.Fatalf("outcome = %q, want %q (exit 3 is a deliberate halt, not a failure)", got, OutcomeBankStop) } if len(sink.applied) != 1 || sink.applied[0].Type != TypeBankStop { t.Fatalf("stream not materialized: %+v", sink.applied) } // The engine's own log must land in the file, not in our structured logger: it carries money. if !bytes.Contains(engineLog.Bytes(), []byte("bank-mining")) { t.Fatalf("engine stderr not captured: %q", engineLog.String()) } } func TestRunReportsOutcomeEvenWhenStreamBreaks(t *testing.T) { s := &Supervisor{Bin: fakeEngine(t, "not a stream\n", "", 1), Workdir: t.TempDir()} got, err := s.Run(context.Background(), &recordingSink{}) if err == nil { t.Fatal("a broken stream must be reported") } if got != OutcomeFailed { t.Fatalf("outcome = %q, want %q", got, OutcomeFailed) } } func TestStatusDecodesTheResyncChannel(t *testing.T) { // A real `tmctl status --json` body carries money and snapshot fields; the allowlist ignores // them, and this fixture keeps one of each to prove it. body := `{"book_id":"gzr","snapshot_id":"a1","committed_usd":1.25,"reserved_usd":0.5, "total_units":10,"done":4,"pending":6,"eta_seconds":900,"unsigned_bank_terms":3, "chapters":[{"chapter":1,"units_total":2,"units_done":2,"cost_usd":0.4,"verdict":"pass"}]}` s := &Supervisor{Bin: fakeEngine(t, body, "", 0), Workdir: t.TempDir()} got, err := s.Status(context.Background()) if err != nil { t.Fatalf("status: %v", err) } if got.BookID != "gzr" || got.Done != 4 || got.ETASeconds != 900 || got.UnsignedBankTerms != 3 { t.Fatalf("status = %+v", got) } if got.SpendUSD != 1.25 { t.Fatalf("spend must be metered from status: %v", got.SpendUSD) } if len(got.Chapters) != 1 || got.Chapters[0].UnitsDone != 2 { t.Fatalf("chapters = %+v", got.Chapters) } } func TestOutcomeOfCoversTheExitContract(t *testing.T) { for code, want := range map[int]Outcome{0: OutcomeClean, 1: OutcomeFailed, 2: OutcomeFlagged, 3: OutcomeBankStop, 7: OutcomeFailed} { if got := outcomeOf(code); got != want { t.Errorf("exit %d = %q, want %q", code, got, want) } } } func shellQuote(s string) string { return "'" + strings.ReplaceAll(s, "'", `'\''`) + "'" }