| 1 | package shipped |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "amadan.net/oficina/estilo.git/colophon" |
| 12 | ) |
| 13 | |
| 14 | // The Colophon and what Docs ships cannot differ (Paul, 2026-10-26: "I'd |
| 15 | // be happy if that were exhaustive"). Each test regenerates its Colophon |
| 16 | // from the builds themselves (shipped.go) and fails unless it is the |
| 17 | // committed one. estilo's colophon.Build, inside Generate, fails on a |
| 18 | // shipped package the catalog does not know, a project without a usage |
| 19 | // note for the app, a usage note for something no longer shipped, and a |
| 20 | // shipped package without its licence text. So a new dependency, a |
| 21 | // dropped one, a rebuilt bundle or an upgraded crate all fail here until |
| 22 | // `go run ./cmd/docs-colophon` is run and the catalog has its words. |
| 23 | // |
| 24 | // This builds on, rather than repeats, the Go-module check other Oficina |
| 25 | // apps have (TestCreditsMatchProductionDependencies): the Go part is the |
| 26 | // same go list -deps of the production entry point through estilo's |
| 27 | // credits/catalog.Collect. |
| 28 | func TestColophonsMatchWhatShips(t *testing.T) { |
| 29 | root := filepath.Join("..", "..") |
| 30 | web, native, err := Generate(root) |
| 31 | if err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | for _, c := range []struct { |
| 35 | path string |
| 36 | want []byte |
| 37 | }{{WebPath, web}, {NativePath, native}} { |
| 38 | got, err := os.ReadFile(filepath.Join(root, c.path)) |
| 39 | if err != nil { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | if !bytes.Equal(got, c.want) { |
| 43 | t.Errorf("%s differs from what Docs ships; run `go run ./cmd/docs-colophon`", c.path) |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | // The parts are each non-empty, so a collector that silently found |
| 49 | // nothing (a bundle list that moved, a manifest renamed) cannot pass. |
| 50 | func TestEveryCollectorFindsSomething(t *testing.T) { |
| 51 | root := filepath.Join("..", "..") |
| 52 | web, err := Web(root) |
| 53 | if err != nil { |
| 54 | t.Fatal(err) |
| 55 | } |
| 56 | native, err := Native(root) |
| 57 | if err != nil { |
| 58 | t.Fatal(err) |
| 59 | } |
| 60 | count := func(packages []colophon.Package, ecosystem string, tool bool) int { |
| 61 | n := 0 |
| 62 | for _, p := range packages { |
| 63 | if p.Ecosystem == ecosystem && p.Tool == tool { |
| 64 | n++ |
| 65 | } |
| 66 | } |
| 67 | return n |
| 68 | } |
| 69 | for _, c := range []struct { |
| 70 | name string |
| 71 | packages []colophon.Package |
| 72 | ecosystem string |
| 73 | tool bool |
| 74 | atLeast int |
| 75 | }{ |
| 76 | {"server Go modules", web, colophon.Go, false, 15}, |
| 77 | {"bundled npm packages", web, colophon.NPM, false, 10}, |
| 78 | {"core crates", web, colophon.Cargo, false, 20}, |
| 79 | {"server typefaces", web, colophon.Font, false, 17}, |
| 80 | {"npm tools", web, colophon.NPM, true, 1}, |
| 81 | {"Go tools", web, colophon.Go, true, 1}, |
| 82 | {"pip tools", web, colophon.Pip, true, 1}, |
| 83 | {"app crates", native, colophon.Cargo, false, 40}, |
| 84 | {"app typefaces", native, colophon.Font, false, 16}, |
| 85 | {"Swift packages", native, colophon.Swift, false, 2}, |
| 86 | } { |
| 87 | if n := count(c.packages, c.ecosystem, c.tool); n < c.atLeast { |
| 88 | t.Errorf("%s: %d, want at least %d", c.name, n, c.atLeast) |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | func TestHTMLTextKeepsLinesAndDecodesEntities(t *testing.T) { |
| 94 | got := htmlText("<h1>Notices</h1><p>A & B</p>\n\n\n\n<pre>x</pre><a href=\"y\">link</a>") |
| 95 | if got != "Notices\n\nA & B\n\nx\nlink\n" { |
| 96 | t.Fatalf("%q", got) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | // Rastrillo Native's server half is linked into cmd/docs (internal/docs |
| 101 | // imports it as nativeauth), so it is on the page: it is Rastrillo's code |
| 102 | // under its own licence, not Oficina's. |
| 103 | func TestTheNativeServerBridgeIsListed(t *testing.T) { |
| 104 | if FirstParty["amadan.net/rastrillo/native/server"] { |
| 105 | t.Fatal("amadan.net/rastrillo/native/server is excluded as first-party, but it ships") |
| 106 | } |
| 107 | web, err := Web(filepath.Join("..", "..")) |
| 108 | if err != nil { |
| 109 | t.Fatal(err) |
| 110 | } |
| 111 | for _, p := range web { |
| 112 | if p.Ecosystem == colophon.Go && p.Name == "amadan.net/rastrillo/native/server" && !p.Tool && len(p.Notices) > 0 { |
| 113 | return |
| 114 | } |
| 115 | } |
| 116 | t.Fatal("the native server bridge is not among the server's packages") |
| 117 | } |
| 118 | |
| 119 | // The toolchains the build and check scripts invoke are tools, each found |
| 120 | // in the script that invokes it. |
| 121 | func TestTheToolchainsTheScriptsRunAreTools(t *testing.T) { |
| 122 | root := filepath.Join("..", "..") |
| 123 | web, err := Web(root) |
| 124 | if err != nil { |
| 125 | t.Fatal(err) |
| 126 | } |
| 127 | native, err := Native(root) |
| 128 | if err != nil { |
| 129 | t.Fatal(err) |
| 130 | } |
| 131 | has := func(ps []colophon.Package, name string) bool { |
| 132 | for _, p := range ps { |
| 133 | if p.Ecosystem == colophon.Toolchain && p.Name == name { |
| 134 | return true |
| 135 | } |
| 136 | } |
| 137 | return false |
| 138 | } |
| 139 | for _, name := range []string{"go", "rust", "rustup", "node", "npm", "python", "docker", "bwrap", "chromium"} { |
| 140 | if !has(web, name) { |
| 141 | t.Errorf("the server's tools lack %s", name) |
| 142 | } |
| 143 | } |
| 144 | for _, name := range []string{"rust", "npm", "python", "xcodegen", "swift"} { |
| 145 | if !has(native, name) { |
| 146 | t.Errorf("the apps' tools lack %s", name) |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | // The five Apple targets' crates are marked with the platforms they build |
| 152 | // for, so a crate on one target only would show on that app only. |
| 153 | func TestAppleCratesAreMarkedByPlatform(t *testing.T) { |
| 154 | native, err := Native(filepath.Join("..", "..")) |
| 155 | if err != nil { |
| 156 | t.Fatal(err) |
| 157 | } |
| 158 | marked := 0 |
| 159 | for _, p := range native { |
| 160 | if p.Ecosystem == colophon.Cargo && p.Name == "wasmi" && len(p.Platforms) == 1 { |
| 161 | marked++ |
| 162 | } |
| 163 | } |
| 164 | if marked != 5 { |
| 165 | t.Fatalf("wasmi is marked for one platform in %d target graphs, want all five", marked) |
| 166 | } |
| 167 | built, err := colophon.Build("docs-native", native) |
| 168 | if err != nil { |
| 169 | t.Fatal(err) |
| 170 | } |
| 171 | e, ok := built.Entry("wasmi") |
| 172 | if !ok || strings.Join(e.Packages[0].Platforms, ",") != "ios,mac" { |
| 173 | t.Fatalf("built, wasmi is on %v, want ios and mac", e.Packages) |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | // Every dependency fixtures/package.json declares is read by a shipped |
| 178 | // bundle: one that is not is dead weight the Colophon would not list. |
| 179 | func TestEveryFixturesDependencyIsBundled(t *testing.T) { |
| 180 | root := filepath.Join("..", "..") |
| 181 | raw, err := os.ReadFile(filepath.Join(root, "fixtures", "package.json")) |
| 182 | if err != nil { |
| 183 | t.Fatal(err) |
| 184 | } |
| 185 | var p struct{ Dependencies map[string]string } |
| 186 | if err := json.Unmarshal(raw, &p); err != nil { |
| 187 | t.Fatal(err) |
| 188 | } |
| 189 | web, err := Web(root) |
| 190 | if err != nil { |
| 191 | t.Fatal(err) |
| 192 | } |
| 193 | bundled := map[string]bool{} |
| 194 | for _, pkg := range web { |
| 195 | if pkg.Ecosystem == colophon.NPM && !pkg.Tool { |
| 196 | bundled[pkg.Name] = true |
| 197 | } |
| 198 | } |
| 199 | for name := range p.Dependencies { |
| 200 | if !bundled[name] { |
| 201 | t.Errorf("fixtures/package.json depends on %s, which no shipped bundle reads", name) |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | // Nothing in the Apple apps draws a Lucide icon (their icons are Oficina's |
| 207 | // own and the system's symbols), so they carry no Lucide notice and their |
| 208 | // Colophon does not list it. |
| 209 | func TestTheAppsCarryNoLucide(t *testing.T) { |
| 210 | root := filepath.Join("..", "..") |
| 211 | native, err := Native(root) |
| 212 | if err != nil { |
| 213 | t.Fatal(err) |
| 214 | } |
| 215 | for _, p := range native { |
| 216 | if p.Name == "lucide" { |
| 217 | t.Error("the apps' Colophon lists Lucide") |
| 218 | } |
| 219 | } |
| 220 | // Rastrillo Native copies the notice into the Mac bundle only when the |
| 221 | // app has the file (rastrillo/native #10, pinned past c24df35), so the |
| 222 | // file itself is gone. |
| 223 | if _, err := os.Stat(filepath.Join(root, "native", "LICENSE.lucide")); err == nil { |
| 224 | t.Error("native/LICENSE.lucide is still here, so the Mac bundle carries it") |
| 225 | } |
| 226 | for _, spec := range []string{"native/ios/project.yml", "native/macos/project.yml"} { |
| 227 | raw, err := os.ReadFile(filepath.Join(root, spec)) |
| 228 | if err != nil { |
| 229 | t.Fatal(err) |
| 230 | } |
| 231 | if strings.Contains(string(raw), "LICENSE.lucide") { |
| 232 | t.Errorf("%s still bundles LICENSE.lucide", spec) |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | // Each toolchain is found by its invocation, never by a comment that |
| 238 | // mentions it: a script that stops running the tool but keeps the comment |
| 239 | // must fail TestTheToolchainsTheScriptsRunAreTools. |
| 240 | func TestToolchainNeedlesAreInvocations(t *testing.T) { |
| 241 | root := filepath.Join("..", "..") |
| 242 | for _, tc := range append(append([]toolchain{}, webToolchains...), nativeToolchains...) { |
| 243 | raw, err := os.ReadFile(filepath.Join(root, tc.file)) |
| 244 | if err != nil { |
| 245 | t.Fatal(err) |
| 246 | } |
| 247 | found := false |
| 248 | for _, line := range strings.Split(string(raw), "\n") { |
| 249 | at := strings.Index(line, tc.needle) |
| 250 | if at < 0 { |
| 251 | continue |
| 252 | } |
| 253 | if comment := strings.Index(line, "#"); comment >= 0 && comment < at { |
| 254 | continue |
| 255 | } |
| 256 | if strings.HasPrefix(strings.TrimSpace(line), "//") { |
| 257 | continue |
| 258 | } |
| 259 | found = true |
| 260 | } |
| 261 | if !found { |
| 262 | t.Errorf("%s: %q is only in a comment in %s, not an invocation", tc.name, tc.needle, tc.file) |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | |