| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "reflect" |
| 9 | "sort" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | ) |
| 13 | |
| 14 | // The timing log is a file other tools will read line by line, so its |
| 15 | // lines are pinned here: one JSON object per check, with exactly these |
| 16 | // fields, appended to whatever the file already holds. |
| 17 | func TestTimingLogAppendsOneLinePerCheck(t *testing.T) { |
| 18 | path := filepath.Join(t.TempDir(), "state", "valley", "timings.jsonl") |
| 19 | var warned bytes.Buffer |
| 20 | log := &timingLog{path: path, warn: &warned} |
| 21 | log.append(checkTiming{ |
| 22 | Time: "2026-10-20T12:00:00.000Z", Repository: "ssh://git@host/project.git", |
| 23 | Change: "kqxmkqxmkqxmkqxmkqxmkqxmkqxmkqxm", Version: 2, Head: strings.Repeat("a", 40), |
| 24 | Check: "prose-format", Runner: "nix", Cost: "quick", Result: "passed", Satisfied: satisfiedCached, |
| 25 | DurationMS: 1234, |
| 26 | }) |
| 27 | // A plain branch has no change ID or version. |
| 28 | log.append(checkTiming{ |
| 29 | Time: "2026-10-20T12:00:01.234Z", Repository: "/srv/project", Head: strings.Repeat("b", 40), |
| 30 | Check: "tree-ok", Runner: "command", Result: "failed", Satisfied: satisfiedRan, |
| 31 | }) |
| 32 | if warned.Len() != 0 { |
| 33 | t.Fatalf("a writable log warned: %s", warned.String()) |
| 34 | } |
| 35 | |
| 36 | data, err := os.ReadFile(path) |
| 37 | if err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | lines := strings.Split(strings.TrimSuffix(string(data), "\n"), "\n") |
| 41 | if len(lines) != 2 { |
| 42 | t.Fatalf("the log holds %d lines, want 2:\n%s", len(lines), data) |
| 43 | } |
| 44 | want := [][]string{ |
| 45 | {"change", "check", "cost", "duration_ms", "head", "repository", "result", "runner", "satisfied", "time", "version"}, |
| 46 | {"check", "duration_ms", "head", "repository", "result", "runner", "satisfied", "time"}, |
| 47 | } |
| 48 | for i, line := range lines { |
| 49 | var fields map[string]any |
| 50 | if err := json.Unmarshal([]byte(line), &fields); err != nil { |
| 51 | t.Fatalf("line %d is not JSON: %v: %s", i+1, err, line) |
| 52 | } |
| 53 | var keys []string |
| 54 | for k := range fields { |
| 55 | keys = append(keys, k) |
| 56 | } |
| 57 | sort.Strings(keys) |
| 58 | if !reflect.DeepEqual(keys, want[i]) { |
| 59 | t.Errorf("line %d has the fields %v, want %v", i+1, keys, want[i]) |
| 60 | } |
| 61 | } |
| 62 | var first checkTiming |
| 63 | if err := json.Unmarshal([]byte(lines[0]), &first); err != nil { |
| 64 | t.Fatal(err) |
| 65 | } |
| 66 | if first.DurationMS != 1234 || first.Version != 2 || first.Satisfied != "cached" { |
| 67 | t.Errorf("the first line reads back as %+v", first) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // A log that cannot be written says so once and goes on: the run it |
| 72 | // belongs to is not failed by it, and is not made to repeat the warning |
| 73 | // for every check after the first. |
| 74 | func TestAnUnwritableTimingLogOnlyWarns(t *testing.T) { |
| 75 | // A directory that is a regular file cannot be written under, whoever |
| 76 | // runs the test. |
| 77 | blocker := filepath.Join(t.TempDir(), "state") |
| 78 | if err := os.WriteFile(blocker, []byte("not a directory\n"), 0o644); err != nil { |
| 79 | t.Fatal(err) |
| 80 | } |
| 81 | var warned bytes.Buffer |
| 82 | log := &timingLog{path: filepath.Join(blocker, "valley", "timings.jsonl"), warn: &warned} |
| 83 | for _, check := range []string{"one", "two", "three"} { |
| 84 | log.append(checkTiming{Check: check, Result: "passed"}) |
| 85 | } |
| 86 | lines := strings.Split(strings.TrimSuffix(warned.String(), "\n"), "\n") |
| 87 | if len(lines) != 1 || !strings.HasPrefix(lines[0], "attest: warning: timings not logged: ") { |
| 88 | t.Fatalf("an unwritable log warned %q, want one warning line", warned.String()) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | func TestTimingsPathFollowsXDGStateHome(t *testing.T) { |
| 93 | home := t.TempDir() |
| 94 | t.Setenv("HOME", home) |
| 95 | fallback := filepath.Join(home, ".local", "state", "valley", "timings.jsonl") |
| 96 | for _, c := range []struct{ state, want string }{ |
| 97 | {"/var/lib/someone/state", "/var/lib/someone/state/valley/timings.jsonl"}, |
| 98 | {"", fallback}, |
| 99 | // The specification ignores a relative path. |
| 100 | {"relative/state", fallback}, |
| 101 | } { |
| 102 | t.Setenv("XDG_STATE_HOME", c.state) |
| 103 | got, err := timingsPath() |
| 104 | if err != nil { |
| 105 | t.Fatal(err) |
| 106 | } |
| 107 | if got != c.want { |
| 108 | t.Errorf("XDG_STATE_HOME=%q: %s, want %s", c.state, got, c.want) |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | // An https remote can carry a token in its URL, and the log is not the |
| 114 | // place for it. |
| 115 | func TestTheRepositoryNameCarriesNoCredentials(t *testing.T) { |
| 116 | for remote, want := range map[string]string{ |
| 117 | "https://someone:ghp_token@forge.example/project.git": "https://forge.example/project.git", |
| 118 | "ssh://git@valley.example/project.git": "ssh://valley.example/project.git", |
| 119 | "git@valley.example:project.git": "git@valley.example:project.git", |
| 120 | "/srv/git/project.git": "/srv/git/project.git", |
| 121 | } { |
| 122 | if got := withoutUserinfo(remote); got != want { |
| 123 | t.Errorf("%s: %s, want %s", remote, got, want) |
| 124 | } |
| 125 | } |
| 126 | } |