|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "log" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "path/filepath" |
| 9 | + "strconv" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | +) |
| 13 | + |
| 14 | +func captureLog(t *testing.T, fn func()) string { |
| 15 | + t.Helper() |
| 16 | + var buf bytes.Buffer |
| 17 | + flags := log.Flags() |
| 18 | + log.SetOutput(&buf) |
| 19 | + log.SetFlags(0) |
| 20 | + t.Cleanup(func() { |
| 21 | + log.SetOutput(os.Stderr) |
| 22 | + log.SetFlags(flags) |
| 23 | + }) |
| 24 | + fn() |
| 25 | + return buf.String() |
| 26 | +} |
| 27 | + |
| 28 | +func markerPath(t *testing.T) string { |
| 29 | + t.Helper() |
| 30 | + return filepath.Join(t.TempDir(), ".radar", "desktop-session") |
| 31 | +} |
| 32 | + |
| 33 | +// deadPID returns a PID that has certainly exited. Picking an arbitrary high |
| 34 | +// number risks colliding with a live process on a busy machine. |
| 35 | +func deadPID(t *testing.T) int { |
| 36 | + t.Helper() |
| 37 | + cmd := exec.Command("go", "version") |
| 38 | + if err := cmd.Start(); err != nil { |
| 39 | + t.Skipf("cannot spawn a throwaway process: %v", err) |
| 40 | + } |
| 41 | + pid := cmd.Process.Pid |
| 42 | + _ = cmd.Wait() |
| 43 | + return pid |
| 44 | +} |
| 45 | + |
| 46 | +func writeMarker(t *testing.T, path string, pid int) { |
| 47 | + t.Helper() |
| 48 | + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { |
| 49 | + t.Fatalf("prepare marker dir: %v", err) |
| 50 | + } |
| 51 | + if err := os.WriteFile(path, []byte(strconv.Itoa(pid)), 0o644); err != nil { |
| 52 | + t.Fatalf("write marker: %v", err) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func TestClaimSessionReportsUncleanExit(t *testing.T) { |
| 57 | + path := markerPath(t) |
| 58 | + writeMarker(t, path, deadPID(t)) |
| 59 | + |
| 60 | + out := captureLog(t, func() { claimSession(path) }) |
| 61 | + |
| 62 | + if !strings.Contains(out, "did not exit cleanly") { |
| 63 | + t.Errorf("log = %q, want an unclean-exit report", out) |
| 64 | + } |
| 65 | + if pid, ok := readSessionMarker(path); !ok || pid != os.Getpid() { |
| 66 | + t.Errorf("marker pid = %d (ok=%v), want this process %d", pid, ok, os.Getpid()) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +// A live PID means a second window is open, not that anything crashed. |
| 71 | +// Reporting a crash here would fire every time someone runs two instances. |
| 72 | +func TestClaimSessionDoesNotBlameALiveInstance(t *testing.T) { |
| 73 | + path := markerPath(t) |
| 74 | + cmd := exec.Command("sleep", "30") |
| 75 | + if err := cmd.Start(); err != nil { |
| 76 | + t.Skipf("cannot spawn a helper process: %v", err) |
| 77 | + } |
| 78 | + t.Cleanup(func() { |
| 79 | + _ = cmd.Process.Kill() |
| 80 | + _ = cmd.Wait() |
| 81 | + }) |
| 82 | + writeMarker(t, path, cmd.Process.Pid) |
| 83 | + |
| 84 | + out := captureLog(t, func() { claimSession(path) }) |
| 85 | + |
| 86 | + if strings.Contains(out, "did not exit cleanly") { |
| 87 | + t.Errorf("log = %q, want no crash claim while another instance runs", out) |
| 88 | + } |
| 89 | + if !strings.Contains(out, "another instance") { |
| 90 | + t.Errorf("log = %q, want the concurrent instance reported", out) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func TestClaimSessionSilentOnFirstRun(t *testing.T) { |
| 95 | + path := markerPath(t) |
| 96 | + |
| 97 | + out := captureLog(t, func() { claimSession(path) }) |
| 98 | + |
| 99 | + if out != "" { |
| 100 | + t.Errorf("log = %q, want silence when no previous run is recorded", out) |
| 101 | + } |
| 102 | + if _, ok := readSessionMarker(path); !ok { |
| 103 | + t.Error("first run did not record a marker") |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +// A graceful shutdown must leave nothing behind, or every clean quit is |
| 108 | +// reported as a crash on the next launch. |
| 109 | +func TestReleaseSessionClearsOwnMarker(t *testing.T) { |
| 110 | + path := markerPath(t) |
| 111 | + claimSession(path) |
| 112 | + |
| 113 | + releaseSession(path) |
| 114 | + |
| 115 | + if _, err := os.Stat(path); !os.IsNotExist(err) { |
| 116 | + t.Errorf("marker still present after shutdown (err=%v)", err) |
| 117 | + } |
| 118 | + out := captureLog(t, func() { claimSession(path) }) |
| 119 | + if strings.Contains(out, "did not exit cleanly") { |
| 120 | + t.Errorf("log = %q, want a clean shutdown to leave no crash report", out) |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +// Quitting one window must not clear a marker another instance owns. |
| 125 | +func TestReleaseSessionLeavesAnotherInstanceMarker(t *testing.T) { |
| 126 | + path := markerPath(t) |
| 127 | + other := deadPID(t) |
| 128 | + writeMarker(t, path, other) |
| 129 | + |
| 130 | + releaseSession(path) |
| 131 | + |
| 132 | + pid, ok := readSessionMarker(path) |
| 133 | + if !ok || pid != other { |
| 134 | + t.Errorf("marker pid = %d (ok=%v), want %d left untouched", pid, ok, other) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +func TestReadSessionMarkerRejectsGarbage(t *testing.T) { |
| 139 | + for _, content := range []string{"", " ", "not-a-pid", "-1", "0"} { |
| 140 | + path := markerPath(t) |
| 141 | + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { |
| 142 | + t.Fatalf("prepare marker dir: %v", err) |
| 143 | + } |
| 144 | + if err := os.WriteFile(path, []byte(content), 0o644); err != nil { |
| 145 | + t.Fatalf("write marker: %v", err) |
| 146 | + } |
| 147 | + if pid, ok := readSessionMarker(path); ok { |
| 148 | + t.Errorf("readSessionMarker(%q) = %d, true; want not ok", content, pid) |
| 149 | + } |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +func TestSessionHelpersTolerateNoHomeDirectory(t *testing.T) { |
| 154 | + // sessionMarkerPath returns "" when the home directory is unknown; the |
| 155 | + // helpers must degrade to doing nothing rather than panicking. |
| 156 | + claimSession("") |
| 157 | + releaseSession("") |
| 158 | +} |
| 159 | + |
| 160 | +func TestProcessAliveOnSelf(t *testing.T) { |
| 161 | + if !processAlive(os.Getpid()) { |
| 162 | + t.Error("processAlive(self) = false, want true") |
| 163 | + } |
| 164 | + if processAlive(deadPID(t)) { |
| 165 | + t.Error("processAlive(exited process) = true, want false") |
| 166 | + } |
| 167 | +} |
0 commit comments