| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "os" |
| 6 | "os/exec" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestTopicRemovalRejectsAnotherProcessRename(t *testing.T) { |
| 11 | if id := os.Getenv("REASONIX_TOPIC_REMOVAL_RENAME"); id != "" { |
| 12 | // TestMain deliberately isolates each process. Rebind only this child's |
| 13 | // test profile to the parent's disposable fixture after that isolation. |
| 14 | var environment map[string]string |
| 15 | if err := json.Unmarshal([]byte(os.Getenv("REASONIX_TOPIC_REMOVAL_ENV")), &environment); err != nil { |
| 16 | t.Fatal(err) |
| 17 | } |
| 18 | for key, value := range environment { |
| 19 | t.Setenv(key, value) |
| 20 | } |
| 21 | a := NewApp() |
| 22 | a.ctx = t.Context() |
| 23 | installNoopRuntimeEvents(a) |
| 24 | defer a.closeSessionServices() |
| 25 | if err := a.RenameTopic(id, "Saved by another process"); err != nil { |
| 26 | t.Fatal(err) |
| 27 | } |
| 28 | return |
| 29 | } |
| 30 | a, topic, _ := topicRemovalFixture(t, "global", "") |
| 31 | request := inspectedTopicRemoval(t, a, topic.ID) |
| 32 | child := exec.Command(os.Args[0], "-test.run=^TestTopicRemovalRejectsAnotherProcessRename$") |
| 33 | environment := map[string]string{} |
| 34 | for _, key := range []string{"HOME", "USERPROFILE", "XDG_CONFIG_HOME", "REASONIX_STATE_HOME", "REASONIX_CACHE_HOME", "AppData"} { |
| 35 | environment[key] = os.Getenv(key) |
| 36 | } |
| 37 | body, err := json.Marshal(environment) |
| 38 | if err != nil { |
| 39 | t.Fatal(err) |
| 40 | } |
| 41 | child.Env = append(os.Environ(), "REASONIX_TOPIC_REMOVAL_RENAME="+topic.ID, "REASONIX_TOPIC_REMOVAL_ENV="+string(body)) |
| 42 | if output, err := child.CombinedOutput(); err != nil { |
| 43 | t.Fatalf("rename process: %v %s", err, output) |
| 44 | } |
| 45 | result, err := a.RemoveTopic(request) |
| 46 | if err != nil || result.Committed || result.ErrorCode != "state_conflict" { |
| 47 | t.Fatalf("stale cross-process confirmation: %+v %v", result, err) |
| 48 | } |
| 49 | if loadTopicTitle("", topic.ID) != "Saved by another process" { |
| 50 | t.Fatal("another process's saved metadata was removed") |
| 51 | } |
| 52 | } |
| 53 |