| 1 | package repair |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestDiagnoseFindsProviderPluginAndPermissionProblems(t *testing.T) { |
| 11 | home := t.TempDir() |
| 12 | t.Setenv("REASONIX_HOME", home) |
| 13 | configText := `default_model = "custom/missing" |
| 14 | |
| 15 | [[providers]] |
| 16 | name = "custom" |
| 17 | kind = "openai" |
| 18 | base_url = "not-a-url" |
| 19 | model = "model-a" |
| 20 | api_key_env = "CUSTOM_KEY" |
| 21 | |
| 22 | [[plugins]] |
| 23 | name = "missing-command" |
| 24 | command = "reasonix-command-that-does-not-exist" |
| 25 | |
| 26 | [permissions] |
| 27 | allow = ["bash"] |
| 28 | deny = ["bash"] |
| 29 | ` |
| 30 | if err := os.WriteFile(filepath.Join(home, "config.toml"), []byte(configText), 0o600); err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | report, err := Diagnose(context.Background(), DiagnoseOptions{Root: t.TempDir()}) |
| 34 | if err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | codes := map[string]bool{} |
| 38 | for _, finding := range report.Findings { |
| 39 | codes[finding.Code] = true |
| 40 | } |
| 41 | for _, want := range []string{"provider.invalid_url", "provider.missing_key", "model.invalid_default", "plugin.command_missing", "permissions.conflict"} { |
| 42 | if !codes[want] { |
| 43 | t.Errorf("missing finding %s: %+v", want, report.Findings) |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | func TestDiagnoseDoesNotRewriteLegacyMCPTierConfig(t *testing.T) { |
| 49 | home := t.TempDir() |
| 50 | t.Setenv("REASONIX_HOME", home) |
| 51 | configText := "[[plugins]]\nname = \"srv\"\ncommand = \"echo\"\ntier = \"eager\"\n" |
| 52 | path := filepath.Join(home, "config.toml") |
| 53 | if err := os.WriteFile(path, []byte(configText), 0o600); err != nil { |
| 54 | t.Fatal(err) |
| 55 | } |
| 56 | if _, err := Diagnose(context.Background(), DiagnoseOptions{Root: t.TempDir()}); err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | got, err := os.ReadFile(path) |
| 60 | if err != nil { |
| 61 | t.Fatal(err) |
| 62 | } |
| 63 | if string(got) != configText { |
| 64 | t.Fatalf("read-only diagnose rewrote config:\n%s", got) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | func TestRebuildDerivedStateQuarantinesWithoutDeleting(t *testing.T) { |
| 69 | home := t.TempDir() |
| 70 | t.Setenv("REASONIX_HOME", home) |
| 71 | path := filepath.Join(home, "desktop-tabs.json") |
| 72 | if err := os.WriteFile(path, []byte("bad"), 0o600); err != nil { |
| 73 | t.Fatal(err) |
| 74 | } |
| 75 | applied, err := RebuildDerivedState("tabs") |
| 76 | if err != nil { |
| 77 | t.Fatal(err) |
| 78 | } |
| 79 | if len(applied) != 1 { |
| 80 | t.Fatalf("applied = %v", applied) |
| 81 | } |
| 82 | if _, err := os.Stat(applied[0]); err != nil { |
| 83 | t.Fatalf("quarantined state missing: %v", err) |
| 84 | } |
| 85 | if _, err := os.Stat(path); !os.IsNotExist(err) { |
| 86 | t.Fatalf("original derived state remains: %v", err) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | func TestRebuildDerivedStateCommitsWhenAuditLogFails(t *testing.T) { |
| 91 | home := t.TempDir() |
| 92 | t.Setenv("REASONIX_HOME", home) |
| 93 | path := filepath.Join(home, "desktop-tabs.json") |
| 94 | original := []byte("bad") |
| 95 | if err := os.WriteFile(path, original, 0o600); err != nil { |
| 96 | t.Fatal(err) |
| 97 | } |
| 98 | if err := os.MkdirAll(repairLogPath(), 0o700); err != nil { |
| 99 | t.Fatal(err) |
| 100 | } |
| 101 | |
| 102 | if _, err := RebuildDerivedState("tabs"); err != nil { |
| 103 | t.Fatalf("rebuild must commit despite a failing audit log: %v", err) |
| 104 | } |
| 105 | if _, err := UndoLastRepair(); err != nil { |
| 106 | t.Fatalf("undo after audit-log failure: %v", err) |
| 107 | } |
| 108 | if got, err := os.ReadFile(path); err != nil || string(got) != string(original) { |
| 109 | t.Fatalf("undone derived state = %q (%v), want %q", got, err, original) |
| 110 | } |
| 111 | } |
| 112 |