| 1 | package boot |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "reasonix/internal/event" |
| 11 | ) |
| 12 | |
| 13 | func TestBuildMissingSystemPromptFileFallsBackToInlinePrompt(t *testing.T) { |
| 14 | isolateConfigHome(t) |
| 15 | root := robustTempDir(t) |
| 16 | t.Setenv("REASONIX_HOME", robustTempDir(t)) |
| 17 | writeFile(t, root, "reasonix.toml", systemPromptFileTestConfig("prompts/missing.md")) |
| 18 | |
| 19 | var notices []event.Event |
| 20 | ctrl, err := Build(context.Background(), Options{ |
| 21 | WorkspaceRoot: root, |
| 22 | Sink: event.FuncSink(func(e event.Event) { |
| 23 | if e.Kind == event.Notice { |
| 24 | notices = append(notices, e) |
| 25 | } |
| 26 | }), |
| 27 | }) |
| 28 | if err != nil { |
| 29 | t.Fatalf("Build: %v", err) |
| 30 | } |
| 31 | defer ctrl.Close() |
| 32 | if got := systemMessage(ctrl.History()); !strings.Contains(got, "INLINE FALLBACK") { |
| 33 | t.Fatalf("system prompt did not use inline fallback:\n%s", got) |
| 34 | } |
| 35 | for _, notice := range notices { |
| 36 | if notice.Level == event.LevelWarn && strings.Contains(notice.Text, "falling back to inline/default system prompt") { |
| 37 | return |
| 38 | } |
| 39 | } |
| 40 | t.Fatalf("missing fallback warning: %#v", notices) |
| 41 | } |
| 42 | |
| 43 | func TestBuildNonMissingSystemPromptReadFailureStaysFatal(t *testing.T) { |
| 44 | isolateConfigHome(t) |
| 45 | root := robustTempDir(t) |
| 46 | t.Setenv("REASONIX_HOME", robustTempDir(t)) |
| 47 | writeFile(t, root, "reasonix.toml", systemPromptFileTestConfig("prompts")) |
| 48 | if err := os.MkdirAll(filepath.Join(root, "prompts"), 0o755); err != nil { |
| 49 | t.Fatal(err) |
| 50 | } |
| 51 | |
| 52 | ctrl, err := Build(context.Background(), Options{WorkspaceRoot: root}) |
| 53 | if ctrl != nil { |
| 54 | ctrl.Close() |
| 55 | t.Fatal("Build returned a controller after system prompt read failure") |
| 56 | } |
| 57 | if err == nil || !strings.Contains(err.Error(), "could not be read") { |
| 58 | t.Fatalf("Build error = %v, want fatal system prompt read error", err) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | func systemPromptFileTestConfig(path string) string { |
| 63 | return ` |
| 64 | default_model = "test-model" |
| 65 | |
| 66 | [agent] |
| 67 | system_prompt = "INLINE FALLBACK" |
| 68 | system_prompt_file = "` + path + `" |
| 69 | |
| 70 | [[providers]] |
| 71 | name = "test-model" |
| 72 | kind = "openai" |
| 73 | base_url = "https://example.invalid" |
| 74 | model = "x" |
| 75 | api_key_env = "REASONIX_TEST_KEY_UNSET" |
| 76 | ` |
| 77 | } |
| 78 |