gunk-dev/the-valley

backend_test.go

source30e24c82839bmain's published tip history
2.9 KiB
1package cmd
2
3import (
4 "fmt"
5 "the-valley/flow/internal/backend"
6 "the-valley/flow/internal/run"
7 "os"
8 "os/exec"
9 "path/filepath"
10 "strings"
11 "testing"
12)
13
14// The prompt file must reach the backend's stdin — locally and across the ssh
15// hop (the stub forwards stdin as real ssh does) — byte for byte, never executed.
16func TestPaneCommandPromptTransport(t *testing.T) {
17 dir := t.TempDir()
18 worktree := filepath.Join(dir, "worktree with ' quotes")
19 if err := os.Mkdir(worktree, 0700); err != nil {
20 t.Fatal(err)
21 }
22 for name, script := range map[string]string{
23 "ssh": "#!/bin/sh\nshift\nexec sh -c \"$1\"\n",
24 "rsync": "#!/bin/sh\nexit 0\n",
25 "klaus": "#!/bin/sh\nif [ \"$1\" = _format-stream ]; then cat; fi\n",
26 } {
27 if err := os.WriteFile(filepath.Join(dir, name), []byte(script), 0700); err != nil {
28 t.Fatal(err)
29 }
30 }
31 t.Setenv("PATH", dir+":"+os.Getenv("PATH"))
32 t.Setenv(sessionIDEnv, "")
33 prompt := "a 'quoted' prompt\n$(exit 17) `exit 18` #{pane_id}\n"
34 promptPath := filepath.Join(dir, "prompts", "it's.md")
35 if err := run.WritePromptFile(promptPath, prompt); err != nil {
36 t.Fatal(err)
37 }
38 agent := backend.ShellCommand([]string{"cat"})
39 logFile := filepath.Join(dir, "log.jsonl")
40 for name, command := range map[string]string{
41 "local": buildPaneCommand(worktree, agent, promptPath, logFile, "klaus", "", "test-run"),
42 "sandbox": buildSandboxPaneCommand("test-host", worktree, agent, promptPath, logFile, "klaus", "", "test-run"),
43 } {
44 t.Run(name, func(t *testing.T) {
45 out, err := exec.Command("sh", "-c", command).CombinedOutput()
46 if err != nil {
47 t.Fatalf("%v: %s", err, out)
48 }
49 if !strings.HasPrefix(string(out), prompt) || !strings.Contains(string(out), `"exit_code":0`) {
50 t.Fatalf("transport corrupted output: %s", out)
51 }
52 })
53 }
54}
55
56func TestClaudeLimitExitPreservesResult(t *testing.T) {
57 for _, subtype := range []string{"error_max_budget_usd", "error_max_turns", "success", "error_during_execution"} {
58 t.Run(subtype, func(t *testing.T) {
59 log := `{"type":"result","subtype":"` + subtype + `","is_error":` + fmt.Sprint(subtype != "success") + `,"total_cost_usd":4.99,"session_id":"previous-conversation"}
60{"type":"klaus_exit","exit_code":1}
61`
62 state, store := setupFinalizeTest(t, log)
63 budget := "5.00"
64 state.Budget = &budget
65 got, err := finalizeFromLog(store, state)
66 if err != nil {
67 t.Fatal(err)
68 }
69 if got != subtype {
70 t.Fatalf("subtype %q replaced with %q", subtype, got)
71 }
72 if (state.FailureReason != nil) != (subtype == "error_during_execution") {
73 t.Fatalf("wrong failure classification: %+v", state)
74 }
75 if subtype == "error_max_budget_usd" && !isBudgetExhausted(state, got) {
76 t.Fatal("budget pause lost")
77 }
78 if state.ClaudeSessionID == nil {
79 t.Fatal("resume identity lost")
80 }
81 })
82 }
83}
84
85func TestFindResumeConversationNil(t *testing.T) {
86 if got := findResumeConversation(nil); got != "" {
87 t.Fatal(got)
88 }
89}