| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "os" |
| 6 | "os/exec" |
| 7 | "path/filepath" |
| 8 | "testing" |
| 9 | |
| 10 | "reasonix/internal/config" |
| 11 | ) |
| 12 | |
| 13 | func TestAuditDurableReceiptAcrossProcessRestart(t *testing.T) { |
| 14 | isolateDesktopUserDirs(t) |
| 15 | _, newRef := configureSwitchableDefaultModels(t) |
| 16 | a := NewApp() |
| 17 | change := ModelSettingsChange{Kind: "preference", Field: "default", Ref: newRef, RequestID: "real-process-restart", ExpectedFingerprint: a.Settings().ModelSettingsFingerprint} |
| 18 | first := a.ApplyModelSettings(change) |
| 19 | if !first.Persisted { |
| 20 | t.Fatalf("initial save failed: %+v", first) |
| 21 | } |
| 22 | raw, err := json.Marshal(change) |
| 23 | if err != nil { |
| 24 | t.Fatal(err) |
| 25 | } |
| 26 | path := filepath.Join(t.TempDir(), "request.json") |
| 27 | if err := os.WriteFile(path, raw, 0600); err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | exe, err := os.Executable() |
| 31 | if err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | cmd := exec.Command(exe, "-test.run=^TestAuditDurableReceiptChild$", "-test.count=1") |
| 35 | cmd.Env = append(os.Environ(), "REASONIX_AUDIT_REQUEST="+path, "REASONIX_AUDIT_HOME="+config.ReasonixHomeDir()) |
| 36 | output, err := cmd.CombinedOutput() |
| 37 | if err != nil { |
| 38 | t.Fatalf("same request after real restart failed: %v\n%s", err, output) |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | func TestAuditDurableReceiptChild(t *testing.T) { |
| 43 | path := os.Getenv("REASONIX_AUDIT_REQUEST") |
| 44 | if path == "" { |
| 45 | t.Skip("child process only") |
| 46 | } |
| 47 | t.Setenv("REASONIX_HOME", os.Getenv("REASONIX_AUDIT_HOME")) |
| 48 | raw, err := os.ReadFile(path) |
| 49 | if err != nil { |
| 50 | t.Fatal(err) |
| 51 | } |
| 52 | var change ModelSettingsChange |
| 53 | if err := json.Unmarshal(raw, &change); err != nil { |
| 54 | t.Fatal(err) |
| 55 | } |
| 56 | if _, ok := config.LookupModelSettingsReceipt(change.RequestID); !ok { |
| 57 | t.Fatal("fixture receipt unavailable in child") |
| 58 | } |
| 59 | result := NewApp().ApplyModelSettings(change) |
| 60 | if !result.Persisted { |
| 61 | t.Fatalf("restarted process rejected identical request: %+v", result) |
| 62 | } |
| 63 | } |
| 64 |