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