package adopt import ( "crypto/sha256" "encoding/base64" "testing" ) // productionVersions is what each app served in production on // 2026-11-17: the /api/version body and X-Carlos-Version, the same build // named two ways by four of the six. var productionVersions = map[string]struct{ body, header string }{ "docs": {"v0.1.0-80-g9de84e4", "v0.1.0-80-g9de84e4"}, "sheets": {"v0.1.0-53-gef9c313", "ef9c313"}, "slides": {"v0.1.0-42-gcb30df5", "cb30df5"}, "calendar": {"v0.1.0-53-g800406a", "800406a"}, "calls": {"ef2335e9df94bf9be1561fcbb1335c87e7c6e4ca", "ef2335e9df94bf9be1561fcbb1335c87e7c6e4ca"}, "memoria": {"v0.1.0-47-g174fb45", "174fb45"}, } func TestSameReleaseAcceptsProductionPairs(t *testing.T) { for app, v := range productionVersions { if !SameRelease(v.body, v.header) { t.Errorf("%s: body %q and header %q refused", app, v.body, v.header) } } } func TestSameRelease(t *testing.T) { full := "ef2335e9df94bf9be1561fcbb1335c87e7c6e4ca" cases := []struct { name, body, header string want bool }{ {"equal names", "v1.4.0", "v1.4.0", true}, {"equal commit ids", full, full, true}, {"describe, short header", "v0.1.0-53-gef9c313", "ef9c313", true}, {"describe, full header", "v0.1.0-53-gef2335e", full, true}, {"describe, longer abbreviation than header", "v0.1.0-53-gef2335e9df94", "ef2335e9", true}, {"describe, many-digit parts", "v12.40.3-1207-g800406a", "800406a", true}, {"different hex", "v0.1.0-53-gef9c313", "ef9c314", false}, {"different first digit", "v0.1.0-53-gef9c313", "af9c313", false}, {"header of six hex digits", "v0.1.0-53-gef9c313", "ef9c31", false}, {"describe of six hex digits", "v0.1.0-53-gef9c31", "ef9c313", false}, {"six in common, both longer", "v0.1.0-53-gef9c31a", "ef9c31b", false}, {"non-describe body, short header", "v1.4.0", "ef9c313", false}, {"bare-commit body, its own prefix as header", full, "ef2335e", false}, {"build-name body, short header", "rel-ef9c313", "ef9c313", false}, {"header not hex", "v0.1.0-53-gef9c313", "ef9c31z", false}, {"header a dirty describe", "v0.1.0-53-gef9c313", "v0.1.0-53-gef9c313-dirty", false}, {"header a tag", "v0.1.0-53-gef9c313", "v0.1.0", false}, {"body describes another commit", "v0.1.0-53-gcb30df5", "ef9c313", false}, {"uppercase header", "v0.1.0-53-gef9c313", "EF9C313", false}, {"uppercase describe hex", "v0.1.0-53-gEF9C313", "ef9c313", false}, {"dirty describe", "v0.1.0-53-gef9c313-dirty", "ef9c313", false}, {"describe without v", "0.1.0-53-gef9c313", "ef9c313", false}, {"describe without count", "v0.1.0-gef9c313", "ef9c313", false}, {"describe of a two-part tag", "v0.1-53-gef9c313", "ef9c313", false}, {"describe with a pre-release tag", "v0.1.0-rc1-53-gef9c313", "ef9c313", false}, {"describe with a newline", "v0.1.0-53-gef9c313\n", "ef9c313", false}, {"header with a newline", "v0.1.0-53-gef9c313", "ef9c313\n", false}, {"header with a space", "v0.1.0-53-gef9c313", " ef9c313", false}, {"empty header", "v0.1.0-53-gef9c313", "", false}, {"empty body", "", "ef9c313", false}, {"names swapped", "ef9c313", "v0.1.0-53-gef9c313", false}, {"different names", "v1.4.0", "v1.4.1", false}, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { if got := SameRelease(c.body, c.header); got != c.want { t.Fatalf("SameRelease(%q, %q) = %v, want %v", c.body, c.header, got, c.want) } }) } } // Each app serving its production names passes the CARLOS preflight, // configure, the delivery check and the activation gate, which sends the // body as app_version and hashes the header for artifact_digest. func TestObserverAcceptsProductionVersionNames(t *testing.T) { for app, v := range productionVersions { t.Run(app, func(t *testing.T) { x := newObserverFixture(t, app, func(w *writeFixture) { w.site.version, w.site.header = v.body, v.header }) if app != CanaryApp { canaryActive(t, x.s) } obs, err := x.observe() if err != nil { t.Fatalf("Observe: %v", err) } if obs.AppVersion != v.body { t.Fatalf("app_version = %q, want the body %q", obs.AppVersion, v.body) } sum := sha256.Sum256([]byte(v.header)) if obs.ArtifactDigest != base64.RawURLEncoding.EncodeToString(sum[:]) { t.Fatal("artifact_digest is not the SHA-256 of X-Carlos-Version") } }) } } func TestPreflightAcceptsProductionVersionNames(t *testing.T) { for app, v := range productionVersions { t.Run(app, func(t *testing.T) { x := newCarlosFixture(t, app) x.site.version, x.site.header = v.body, v.header _, codes := x.preflight(t, nil) wantCodes(t, codes) }) } } // A lookalike pair is refused at both guards: by the preflight before // anything is written, and by the activation gate when the app moved to // it afterwards (the journal rewritten to agree, so only the pair is // wrong). func TestLookalikeVersionNamesRefused(t *testing.T) { lookalikes := map[string]string{ "another commit": "ef9c314", "six hex digits": "ef9c31", "uppercase": "EF9C313", "not hex": "ef9c31g", "dirty describe": "v0.1.0-53-gef9c313-dirty", "other describe": "v0.1.0-54-gef9c313", "another app's": "cb30df5", "full id, other": "ef9c314" + "000000000000000000000000000000000", "seven, one off": "ef9c312", } const body = "v0.1.0-53-gef9c313" for name, header := range lookalikes { t.Run("preflight "+name, func(t *testing.T) { x := newCarlosFixture(t, "sheets") x.site.version, x.site.header = body, header _, codes := x.preflight(t, nil) wantCodes(t, codes, "version-header-mismatch") }) t.Run("gate "+name, func(t *testing.T) { x := newObserverFixture(t, "sheets") x.site.version, x.site.header = body, header x.o.Artifacts = map[string]string{"sheets": sha256Hex(header)} x.rejournal(t, func(e *evidence) { e.base.Delivery.Version, e.after.Delivery.Version, e.check.Delivery.Version = body, body, body e.base.Delivery.Header, e.after.Delivery.Header, e.check.Delivery.Header = header, header, header }) canaryActive(t, x.s) wantCodes(t, x.refused(t, "version-header-mismatch"), "version-header-mismatch") }) } }