| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "strconv" |
| 7 | "testing" |
| 8 | |
| 9 | "reasonix/internal/agent" |
| 10 | "reasonix/internal/provider" |
| 11 | ) |
| 12 | |
| 13 | func TestCurrentMessageOriginIsAuthoritativeAcrossDesktopHistory(t *testing.T) { |
| 14 | quotedHostText := agent.CompletionValidationContinuationPrefix + " explain what this message means" |
| 15 | msgs := []provider.Message{ |
| 16 | {Role: provider.RoleUser, Origin: provider.MessageOriginUser, Content: "wrapped", RawContent: quotedHostText}, |
| 17 | {Role: provider.RoleAssistant, Content: "answer"}, |
| 18 | {Role: provider.RoleUser, Origin: provider.MessageOriginUser, Content: agent.MidTurnSteerPrefix + "\nuse the smaller patch", RawContent: "use the smaller patch"}, |
| 19 | {Role: provider.RoleUser, Origin: provider.MessageOriginHost, Content: "innocent looking continuation"}, |
| 20 | } |
| 21 | |
| 22 | if got := visibleHistoryUserTurns(msgs, identityPromptDisplay); got != 1 { |
| 23 | t.Fatalf("visible user turns = %d, want only the explicitly user-authored turn", got) |
| 24 | } |
| 25 | turns := historyCheckpointTurns(msgs, identityPromptDisplay, map[int]int{0: 7, 2: 8, 3: 9}) |
| 26 | if len(turns) != 1 || turns[0] != 7 { |
| 27 | t.Fatalf("checkpoint turns = %v, want [7]", turns) |
| 28 | } |
| 29 | history := historyMessages(msgs, identityPromptDisplay) |
| 30 | if len(history) != 3 || history[0].Role != "user" || history[0].Content != quotedHostText || |
| 31 | history[2].Role != "notice" || history[2].Content != "↪ use the smaller patch" { |
| 32 | t.Fatalf("history = %+v, want quoted host text plus one steer notice", history) |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | func TestPromptHistoryUsesOriginAndRawContentForCurrentJSONL(t *testing.T) { |
| 37 | dir := t.TempDir() |
| 38 | path := filepath.Join(dir, "session.jsonl") |
| 39 | quotedHostText := agent.CompletionValidationContinuationPrefix + " show this literally" |
| 40 | raw := `{"role":"user","origin":"host","content":"ordinary looking host text"}` + "\n" + |
| 41 | `{"role":"user","origin":"user","content":"wrapped provider text","raw_content":` + strconv.Quote(quotedHostText) + `}` + "\n" |
| 42 | if err := os.WriteFile(path, []byte(raw), 0o600); err != nil { |
| 43 | t.Fatal(err) |
| 44 | } |
| 45 | info, err := os.Stat(path) |
| 46 | if err != nil { |
| 47 | t.Fatal(err) |
| 48 | } |
| 49 | entries, err := collectPromptHistoryEntries(path, info, identityPromptDisplay) |
| 50 | if err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | if len(entries) != 1 || entries[0].Text != quotedHostText { |
| 54 | t.Fatalf("prompt history = %+v, want the explicit user raw content only", entries) |
| 55 | } |
| 56 | } |
| 57 |