vole
Site
Clone
Read only, no credentials:
https://rodent.software/vole.git
Write over SSH with a registered key:
git clone ssh://git@rodent.software/vole.git
Self-hosted forge that lives in your Git repo.
| 1 | package channel |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | "time" |
| 9 | ) |
| 10 | |
| 11 | // newDB is the instance database a store is opened on. The channel table lives in |
| 12 | // the file the roster owns, so this package does not create it: a test creates the |
| 13 | // file the way an initialised instance already has it. |
| 14 | func newDB(t *testing.T) string { |
| 15 | t.Helper() |
| 16 | path := filepath.Join(t.TempDir(), "instance.db") |
| 17 | if err := os.WriteFile(path, nil, 0o600); err != nil { |
| 18 | t.Fatal(err) |
| 19 | } |
| 20 | return path |
| 21 | } |
| 22 | |
| 23 | func openTest(t *testing.T) *Store { |
| 24 | t.Helper() |
| 25 | s, err := Open(newDB(t)) |
| 26 | if err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | t.Cleanup(func() { s.Close() }) |
| 30 | return s |
| 31 | } |
| 32 | |
| 33 | func TestOpenRequiresTheInstanceDatabase(t *testing.T) { |
| 34 | t.Parallel() |
| 35 | if _, err := Open(filepath.Join(t.TempDir(), "absent.db")); err == nil { |
| 36 | t.Fatal("Open created a database; the instance database is not this package's to create") |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func TestValidName(t *testing.T) { |
| 41 | t.Parallel() |
| 42 | for _, ok := range []string{"stable", "latest", "beta", "lts-2026", "a"} { |
| 43 | if err := ValidName(ok); err != nil { |
| 44 | t.Errorf("ValidName(%q) = %v", ok, err) |
| 45 | } |
| 46 | } |
| 47 | for _, bad := range []string{"", "Stable", "-stable", "stable/", "stable.md", "a b", strings.Repeat("a", MaxNameLen+1), ".", ".."} { |
| 48 | if err := ValidName(bad); err == nil { |
| 49 | t.Errorf("ValidName(%q) = nil", bad) |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | func testChannel(name, tag string) Channel { |
| 55 | return Channel{ |
| 56 | Name: name, |
| 57 | Release: tag, |
| 58 | ReleaseObject: strings.Repeat("a", 40), |
| 59 | UpdatedBy: "keenan", |
| 60 | UpdatedAt: time.Date(2026, 11, 14, 12, 0, 0, 0, time.UTC), |
| 61 | Description: "Recommended.", |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | func TestSetGetListClear(t *testing.T) { |
| 66 | t.Parallel() |
| 67 | s := openTest(t) |
| 68 | ch := testChannel("stable", "v1.0.0") |
| 69 | if err := s.Set("repo1", ch); err != nil { |
| 70 | t.Fatal(err) |
| 71 | } |
| 72 | got, ok, err := s.Get("repo1", "stable") |
| 73 | if err != nil || !ok { |
| 74 | t.Fatalf("Get = %+v, %v, %v", got, ok, err) |
| 75 | } |
| 76 | if got.Release != ch.Release || got.ReleaseObject != ch.ReleaseObject || |
| 77 | got.UpdatedBy != ch.UpdatedBy || !got.UpdatedAt.Equal(ch.UpdatedAt) || |
| 78 | got.Description != ch.Description { |
| 79 | t.Fatalf("Get = %+v, want %+v", got, ch) |
| 80 | } |
| 81 | list, err := s.List("repo1") |
| 82 | if err != nil || len(list) != 1 || list[0].Name != "stable" { |
| 83 | t.Fatalf("List = %+v, %v", list, err) |
| 84 | } |
| 85 | if names, err := s.ForTag("repo1", "v1.0.0"); err != nil || len(names) != 1 { |
| 86 | t.Fatalf("ForTag = %v, %v", names, err) |
| 87 | } |
| 88 | |
| 89 | // A move replaces the target and removes the old tag association. |
| 90 | ch.Release = "v2.0.0" |
| 91 | ch.ReleaseObject = strings.Repeat("b", 40) |
| 92 | ch.Description = "New stable." |
| 93 | if err := s.Set("repo1", ch); err != nil { |
| 94 | t.Fatal(err) |
| 95 | } |
| 96 | if names, _ := s.ForTag("repo1", "v1.0.0"); len(names) != 0 { |
| 97 | t.Fatalf("old tag still names channels: %v", names) |
| 98 | } |
| 99 | if list, _ := s.List("repo1"); len(list) != 1 || list[0].Description != "New stable." { |
| 100 | t.Fatalf("moved channel = %+v", list) |
| 101 | } |
| 102 | |
| 103 | // Another repository cannot see it. |
| 104 | if _, ok, _ := s.Get("repo2", "stable"); ok { |
| 105 | t.Fatal("channel leaked across repositories") |
| 106 | } |
| 107 | if ok, err := s.Clear("repo1", "stable"); err != nil || !ok { |
| 108 | t.Fatalf("Clear = %v, %v", ok, err) |
| 109 | } |
| 110 | if ok, _ := s.Clear("repo1", "stable"); ok { |
| 111 | t.Fatal("clearing a missing channel reported a row") |
| 112 | } |
| 113 | if list, _ := s.List("repo1"); len(list) != 0 { |
| 114 | t.Fatalf("after clear = %+v", list) |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | func TestSetValidates(t *testing.T) { |
| 119 | t.Parallel() |
| 120 | s := openTest(t) |
| 121 | valid := testChannel("stable", "v1.0.0") |
| 122 | for name, mutate := range map[string]func(*Channel){ |
| 123 | "name": func(c *Channel) { c.Name = "Stable" }, |
| 124 | "tag": func(c *Channel) { c.Release = "" }, |
| 125 | "long tag": func(c *Channel) { c.Release = strings.Repeat("v", MaxTagLen+1) }, |
| 126 | "object": func(c *Channel) { c.ReleaseObject = "nope" }, |
| 127 | "updated by": func(c *Channel) { c.UpdatedBy = "" }, |
| 128 | "updated at": func(c *Channel) { c.UpdatedAt = time.Time{} }, |
| 129 | "description": func(c *Channel) { c.Description = strings.Repeat("x", MaxDescription+1) }, |
| 130 | } { |
| 131 | ch := valid |
| 132 | mutate(&ch) |
| 133 | if err := s.Set("repo1", ch); err == nil { |
| 134 | t.Errorf("%s: Set accepted the channel", name) |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | func TestOpenPersists(t *testing.T) { |
| 140 | t.Parallel() |
| 141 | path := newDB(t) |
| 142 | s, err := Open(path) |
| 143 | if err != nil { |
| 144 | t.Fatal(err) |
| 145 | } |
| 146 | if err := s.Set("repo1", testChannel("stable", "v1.0.0")); err != nil { |
| 147 | t.Fatal(err) |
| 148 | } |
| 149 | if err := s.Close(); err != nil { |
| 150 | t.Fatal(err) |
| 151 | } |
| 152 | s, err = Open(path) |
| 153 | if err != nil { |
| 154 | t.Fatal(err) |
| 155 | } |
| 156 | defer s.Close() |
| 157 | if _, ok, err := s.Get("repo1", "stable"); err != nil || !ok { |
| 158 | t.Fatalf("after reopen = %v, %v", ok, err) |
| 159 | } |
| 160 | } |
| 161 |