gunk-dev/the-valley

changes_test.go

source28abc5737165main's published tip history
5.6 KiB
1package valley
2
3import (
4 "strings"
5 "testing"
6)
7
8const sampleChanges = `{
9 "schema": "valley-changes/v1",
10 "host": "valley-host",
11 "served_by": "valley 0.9",
12 "repos": [
13 { "repo": "site", "url": "git@valley-host:site.git", "error": null, "mirror": true,
14 "changes": [
15 { "change_id": "nxvpkqlmzrtwoyus", "handle": "nxvp", "target": "main", "owner": "operator",
16 "state": "ready", "record": "1111111111111111111111111111111111111111", "seq": 3, "active": 2,
17 "priority": "high",
18 "versions": [
19 { "n": 1, "head": "aaaa", "title": "Add a health check", "branch": "agent/a", "uploader": "operator",
20 "landing": "not-landed", "landed_commit": null, "request": null, "outcome": null, "signed": false },
21 { "n": 2, "head": "bbbb", "title": "Add a health check", "branch": "agent/a", "uploader": "operator",
22 "landing": "landed", "landed_commit": "cccc",
23 "request": { "ref": "refs/valley/requests/nxvp", "commit": "dddd", "lease": 1 },
24 "outcome": { "decision": "landed", "head": "bbbb", "tip": "cccc" } }
25 ],
26 "includes": ["zzzz"] }
27 ],
28 "branches": [ { "branch": "agent/a", "head": "bbbb" } ] },
29 { "repo": "broken", "url": "git@valley-host:broken.git", "error": "fetch failed: permission denied", "changes": [] },
30 { "repo": "odd", "url": "x", "error": { "code": 7 }, "changes": [] }
31 ]
32}`
33
34func TestDecodeToleratesUnknownFields(t *testing.T) {
35 c, err := Decode([]byte(sampleChanges))
36 if err != nil {
37 t.Fatalf("Decode: %v", err)
38 }
39 if c.Host != "valley-host" || len(c.Repos) != 3 {
40 t.Fatalf("decoded %+v", c)
41 }
42 site := c.Repos[0]
43 if site.Err() != "" {
44 t.Errorf("site error = %q, want none", site.Err())
45 }
46 ch := site.Changes[0]
47 if ch.Handle != "nxvp" || ch.State != StateReady || ch.Active != 2 || len(ch.Versions) != 2 || ch.Includes[0] != "zzzz" {
48 t.Errorf("change = %+v", ch)
49 }
50 v1, v2 := ch.Versions[0], ch.Versions[1]
51 if v1.Request != nil || v1.Outcome != nil || v1.LandedCommit != nil || v1.Landing != NotLanded {
52 t.Errorf("v1 = %+v", v1)
53 }
54 if v2.Landing != Landed || *v2.LandedCommit != "cccc" || v2.Request.Ref != "refs/valley/requests/nxvp" || v2.Outcome.Decision != "landed" {
55 t.Errorf("v2 = %+v", v2)
56 }
57 if got := c.Repos[1].Err(); got != "fetch failed: permission denied" {
58 t.Errorf("broken repo error = %q", got)
59 }
60 if got := c.Repos[2].Err(); got == "" {
61 t.Error("a repo whose error is an object should still read as failed")
62 }
63}
64
65// TestDecodeLandedVersion: a change's landed_version is read whether it is
66// a version, null, or absent, as from a valley older than the field, and
67// absent stays distinct from null.
68func TestDecodeLandedVersion(t *testing.T) {
69 c, err := Decode([]byte(`{"schema":"valley-changes/v1","repos":[{"repo":"r","changes":[
70 {"change_id":"k1","landed_version":2,"landed_commit":"cccc"},
71 {"change_id":"k2","landed_version":null,"landed_commit":null},
72 {"change_id":"k3"}]}]}`))
73 if err != nil {
74 t.Fatalf("Decode: %v", err)
75 }
76 chs := c.Repos[0].Changes
77 if l := chs[0].LandedVersion; !l.Listed || l.N == nil || *l.N != 2 || *chs[0].LandedCommit != "cccc" {
78 t.Errorf("landed: %+v, commit %v", l, chs[0].LandedCommit)
79 }
80 if l := chs[1].LandedVersion; !l.Listed || l.N != nil || chs[1].LandedCommit != nil {
81 t.Errorf("not landed: %+v, commit %v", l, chs[1].LandedCommit)
82 }
83 if l := chs[2].LandedVersion; l.Listed || l.N != nil {
84 t.Errorf("absent: %+v", l)
85 }
86}
87
88func TestDecodeRefusesOtherSchemas(t *testing.T) {
89 for name, schema := range map[string]string{
90 "later version": `"valley-changes/v2"`,
91 "other view": `"valley-status/v1"`,
92 "missing": ``,
93 } {
94 t.Run(name, func(t *testing.T) {
95 data := `{"host":"h","repos":[]}`
96 if schema != "" {
97 data = `{"schema":` + schema + `,"host":"h","repos":[]}`
98 }
99 _, err := Decode([]byte(data))
100 if err == nil || !strings.Contains(err.Error(), "schema") {
101 t.Errorf("Decode(%s) error = %v, want a schema error", data, err)
102 }
103 })
104 }
105 if _, err := Decode([]byte("valley: no such verb changes")); err == nil {
106 t.Error("Decode should refuse output that isn't JSON")
107 }
108}
109
110func TestParseReady(t *testing.T) {
111 r, err := parseReady([]byte("note: fetching origin\nsite nxvp v2 ready\n"))
112 if err != nil || r != (Readied{Repo: "site", Handle: "nxvp", Version: 2}) {
113 t.Errorf("parseReady = %+v, %v", r, err)
114 }
115 if _, err := parseReady([]byte("ok\n")); err == nil {
116 t.Error("parseReady should refuse output without a ready line")
117 }
118}
119
120// TestParseUnready: valley unready's draft line, with the version that was
121// ready or without one, and its notes that the change wasn't ready.
122func TestParseUnready(t *testing.T) {
123 for _, tc := range []struct {
124 out string
125 want Unreadied
126 }{
127 {"the-valley vqmw v1 draft\n", Unreadied{Repo: "the-valley", Handle: "vqmw", Draft: true}},
128 {"note: fetching origin\nsite nxvp v12 draft\n", Unreadied{Repo: "site", Handle: "nxvp", Draft: true}},
129 {"site nxvp draft\n", Unreadied{Repo: "site", Handle: "nxvp", Draft: true}},
130 {"nxvp is already a draft\n", Unreadied{Note: "nxvp is already a draft"}},
131 {"valley: change nxvp is not ready\n", Unreadied{Note: "valley: change nxvp is not ready"}},
132 {"valley: no change record for nxvp-id\n", Unreadied{Note: "valley: no change record for nxvp-id"}},
133 } {
134 if got, err := parseUnready([]byte(tc.out)); err != nil || got != tc.want {
135 t.Errorf("parseUnready(%q) = %+v, %v; want %+v", tc.out, got, err, tc.want)
136 }
137 }
138 for _, out := range []string{"site nxvp v2 ready\n", "site nxvp vx draft\n", "ok\n", ""} {
139 if got, err := parseUnready([]byte(out)); err == nil {
140 t.Errorf("parseUnready(%q) = %+v; want an error", out, got)
141 }
142 }
143}