|
| 1 | +package metrics |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "runtime" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +// TestCachedMachineIDReusesCacheWithoutRecomputing seeds the on-disk cache |
| 12 | +// with a sentinel and asserts cachedMachineID returns it verbatim — proving |
| 13 | +// the cached path never reaches the (slow, forking) platform probe, which |
| 14 | +// could not produce the sentinel. |
| 15 | +func TestCachedMachineIDReusesCacheWithoutRecomputing(t *testing.T) { |
| 16 | + home := t.TempDir() |
| 17 | + t.Setenv("HOME", home) |
| 18 | + if runtime.GOOS == "windows" { |
| 19 | + t.Setenv("USERPROFILE", home) |
| 20 | + } |
| 21 | + |
| 22 | + sentinel := strings.Repeat("ab12", 16) // 64 chars, valid shape |
| 23 | + dir := filepath.Join(home, ".beads") |
| 24 | + if err := os.MkdirAll(dir, 0o700); err != nil { |
| 25 | + t.Fatalf("mkdir: %v", err) |
| 26 | + } |
| 27 | + if err := os.WriteFile(filepath.Join(dir, machineIDCacheName), []byte(sentinel+"\n"), 0o600); err != nil { |
| 28 | + t.Fatalf("seed cache: %v", err) |
| 29 | + } |
| 30 | + |
| 31 | + if got := cachedMachineID(AppName); got != sentinel { |
| 32 | + t.Errorf("cachedMachineID = %q, want cached sentinel %q", got, sentinel) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +// TestCachedMachineIDComputesAndPersistsOnMiss exercises the cold path: no |
| 37 | +// cache file, so the ID is computed once and written to ~/.beads/machine-id |
| 38 | +// (0600) for every later invocation to reuse. |
| 39 | +func TestCachedMachineIDComputesAndPersistsOnMiss(t *testing.T) { |
| 40 | + home := t.TempDir() |
| 41 | + t.Setenv("HOME", home) |
| 42 | + if runtime.GOOS == "windows" { |
| 43 | + t.Setenv("USERPROFILE", home) |
| 44 | + } |
| 45 | + |
| 46 | + first := cachedMachineID(AppName) |
| 47 | + if first == "" { |
| 48 | + t.Fatalf("cachedMachineID returned empty ID") |
| 49 | + } |
| 50 | + |
| 51 | + path := filepath.Join(home, ".beads", machineIDCacheName) |
| 52 | + if !validMachineID(first) { |
| 53 | + // The platform probe failed (returns "invalid" in sandboxed CI); |
| 54 | + // a failure must NOT be cached, so the next run retries. |
| 55 | + if _, err := os.Stat(path); !os.IsNotExist(err) { |
| 56 | + t.Errorf("invalid probe result was cached at %s (stat err=%v)", path, err) |
| 57 | + } |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + data, err := os.ReadFile(path) |
| 62 | + if err != nil { |
| 63 | + t.Fatalf("cache not written: %v", err) |
| 64 | + } |
| 65 | + if got := strings.TrimSpace(string(data)); got != first { |
| 66 | + t.Errorf("cache content = %q, want %q", got, first) |
| 67 | + } |
| 68 | + if runtime.GOOS != "windows" { |
| 69 | + fi, err := os.Stat(path) |
| 70 | + if err != nil { |
| 71 | + t.Fatalf("stat cache: %v", err) |
| 72 | + } |
| 73 | + if perm := fi.Mode().Perm(); perm != 0o600 { |
| 74 | + t.Errorf("cache perms = %o, want 0600", perm) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + // Second call returns the identical ID (now from cache). |
| 79 | + if second := cachedMachineID(AppName); second != first { |
| 80 | + t.Errorf("second cachedMachineID = %q, want %q", second, first) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +// TestReadCachedMachineIDRejectsGarbage: a corrupt, oversized, multi-token, or |
| 85 | +// probe-failure ("invalid") cache must read as a miss so the ID is recomputed, |
| 86 | +// never fed into every event's distinct_id. |
| 87 | +func TestReadCachedMachineIDRejectsGarbage(t *testing.T) { |
| 88 | + dir := t.TempDir() |
| 89 | + cases := []struct { |
| 90 | + name string |
| 91 | + content string |
| 92 | + }{ |
| 93 | + {name: "empty", content: ""}, |
| 94 | + {name: "whitespace-only", content: " \n\t\n"}, |
| 95 | + {name: "probe-failure-marker", content: "invalid\n"}, |
| 96 | + {name: "embedded-space", content: "abc def\n"}, |
| 97 | + {name: "multi-line", content: "abc123\nxyz789\n"}, |
| 98 | + {name: "control-chars", content: "abc\x01def\n"}, |
| 99 | + {name: "non-ascii", content: "abcédef\n"}, |
| 100 | + {name: "oversized", content: strings.Repeat("a", maxMachineIDLen+1) + "\n"}, |
| 101 | + } |
| 102 | + for _, tc := range cases { |
| 103 | + t.Run(tc.name, func(t *testing.T) { |
| 104 | + path := filepath.Join(dir, "cache-"+tc.name) |
| 105 | + if err := os.WriteFile(path, []byte(tc.content), 0o600); err != nil { |
| 106 | + t.Fatalf("write: %v", err) |
| 107 | + } |
| 108 | + if got := readCachedMachineID(path); got != "" { |
| 109 | + t.Errorf("readCachedMachineID(%q content=%q) = %q, want miss", tc.name, tc.content, got) |
| 110 | + } |
| 111 | + }) |
| 112 | + } |
| 113 | + |
| 114 | + // Positive control: a well-formed single-token cache is accepted, with |
| 115 | + // surrounding whitespace trimmed. |
| 116 | + path := filepath.Join(dir, "cache-good") |
| 117 | + if err := os.WriteFile(path, []byte(" deadbeef42\n"), 0o600); err != nil { |
| 118 | + t.Fatalf("write: %v", err) |
| 119 | + } |
| 120 | + if got := readCachedMachineID(path); got != "deadbeef42" { |
| 121 | + t.Errorf("readCachedMachineID(good) = %q, want %q", got, "deadbeef42") |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +// TestInitDisabledDoesNotTouchMachineID: a disabled invocation (the |
| 126 | +// BD_DISABLE_METRICS / DO_NOT_TRACK path — and every `bd --version`) must not |
| 127 | +// compute, read, or write a machine ID at all. The observable half of that |
| 128 | +// contract is that no cache file appears. |
| 129 | +func TestInitDisabledDoesNotTouchMachineID(t *testing.T) { |
| 130 | + home := t.TempDir() |
| 131 | + t.Setenv("HOME", home) |
| 132 | + if runtime.GOOS == "windows" { |
| 133 | + t.Setenv("USERPROFILE", home) |
| 134 | + } |
| 135 | + |
| 136 | + if _, err := Init("0.0.0-test", false, ""); err != nil { |
| 137 | + t.Fatalf("Init: %v", err) |
| 138 | + } |
| 139 | + |
| 140 | + path := filepath.Join(home, ".beads", machineIDCacheName) |
| 141 | + if _, err := os.Stat(path); !os.IsNotExist(err) { |
| 142 | + t.Errorf("disabled Init created machine-id cache at %s (stat err=%v)", path, err) |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +// TestWriteMachineIDCacheAtomicReplace: writing over an existing cache goes |
| 147 | +// through temp-file + rename, so no reader can observe a truncated ID and no |
| 148 | +// tmp litter survives. |
| 149 | +func TestWriteMachineIDCacheAtomicReplace(t *testing.T) { |
| 150 | + dir := t.TempDir() |
| 151 | + path := filepath.Join(dir, machineIDCacheName) |
| 152 | + if err := os.WriteFile(path, []byte("oldvalue\n"), 0o600); err != nil { |
| 153 | + t.Fatalf("seed: %v", err) |
| 154 | + } |
| 155 | + |
| 156 | + writeMachineIDCache(path, "newvalue") |
| 157 | + |
| 158 | + if got := readCachedMachineID(path); got != "newvalue" { |
| 159 | + t.Errorf("after replace, cache = %q, want %q", got, "newvalue") |
| 160 | + } |
| 161 | + entries, err := os.ReadDir(dir) |
| 162 | + if err != nil { |
| 163 | + t.Fatalf("readdir: %v", err) |
| 164 | + } |
| 165 | + for _, e := range entries { |
| 166 | + if strings.Contains(e.Name(), ".tmp-") { |
| 167 | + t.Errorf("temp file litter left behind: %s", e.Name()) |
| 168 | + } |
| 169 | + } |
| 170 | +} |
0 commit comments