| 1 | // Package exporttest is the web UI's shared fixture: a complete, valid |
| 2 | // export built from a small synthetic valley, with its signed manifest and |
| 3 | // the verifier key that opens it. The publisher's, the receiver's and the |
| 4 | // server's tests all build it, so they test against the same bytes. |
| 5 | // |
| 6 | // The synthetic valley has two projects. app is public; site is hidden. |
| 7 | // Both belong to one group, whose identifier is core and whose name, which |
| 8 | // its web URLs open with, is tools; t is its alias. Each project's web |
| 9 | // name is its identifier, and its short alias too. app was renamed from |
| 10 | // old-app, and site from old-site, which each keeps as a former name. The |
| 11 | // export carries app's names alone. |
| 12 | // app has a milestone with five children, among them a claimed knot, an |
| 13 | // unclaimed knot in progress, a knot blocked by a local knot and by one of |
| 14 | // site's, a bug whose body and comment hold HTML, and comments by mapped |
| 15 | // and unmapped principals, one of them edited. It has one ready change, whose earlier version, |
| 16 | // draft, branch and repository URL the export drops. Each project has a |
| 17 | // real bare repository whose main has a few commits. |
| 18 | // |
| 19 | // The valley's JSON is literal, under valley/, in the shapes valley plan, |
| 20 | // valley pending and valley knot show print. testdata/export.json is the |
| 21 | // export built from it, kept as the contract's expected output. |
| 22 | package exporttest |
| 23 | |
| 24 | import ( |
| 25 | "bytes" |
| 26 | "context" |
| 27 | "crypto/ed25519" |
| 28 | "embed" |
| 29 | "encoding/json" |
| 30 | "errors" |
| 31 | "fmt" |
| 32 | "io/fs" |
| 33 | "net/http/httptest" |
| 34 | "os" |
| 35 | "os/exec" |
| 36 | "path/filepath" |
| 37 | "sort" |
| 38 | "strings" |
| 39 | "testing" |
| 40 | "time" |
| 41 | |
| 42 | "the-valley/note" |
| 43 | "the-valley/web/export" |
| 44 | ) |
| 45 | |
| 46 | //go:embed valley |
| 47 | var valley embed.FS |
| 48 | |
| 49 | const ( |
| 50 | // Instance and Destination are what the fixture's manifest is signed |
| 51 | // for, and what a receiver admitting it is configured with. |
| 52 | Instance = "valley-host" |
| 53 | Destination = "example.org" |
| 54 | // SignerName is the export key's name in the manifest's signature. |
| 55 | SignerName = "valley-host/web-export" |
| 56 | ) |
| 57 | |
| 58 | var ( |
| 59 | // Collected is the synthetic valley's plan time, and SignedAt the |
| 60 | // manifest's signing time, whose Unix nanoseconds are its sequence. |
| 61 | Collected = time.Date(2026, 10, 25, 12, 0, 0, 0, time.UTC) |
| 62 | SignedAt = time.Date(2026, 10, 25, 12, 0, 30, 0, time.UTC) |
| 63 | |
| 64 | // Principals is the publisher's display mapping. drive-by, who wrote |
| 65 | // a bug and a comment, is not in it. |
| 66 | Principals = export.Principals{"app-operator": "operator", "app-agent": "agent"} |
| 67 | |
| 68 | // Identities export app alone, with its publication identity. |
| 69 | Identities = map[string]string{"app": "9da6809f038df92138c131cd16bed76d"} |
| 70 | |
| 71 | // Groups are the instance's one group, and Placements both projects' |
| 72 | // places in it. |
| 73 | Groups = []export.Group{{ID: "core", Name: "tools", Aliases: []string{"t"}}} |
| 74 | Placements = map[string]export.Placement{ |
| 75 | "app": {Group: "core", Name: "app", FormerNames: []string{"old-app"}, Aliases: []string{"app"}}, |
| 76 | "site": {Group: "core", Name: "site", FormerNames: []string{"old-site"}, Aliases: []string{"site"}}, |
| 77 | } |
| 78 | ) |
| 79 | |
| 80 | // Key is the fixture's export key. It is derived from a fixed seed, so its |
| 81 | // verifier is the same in every run. |
| 82 | func Key() ed25519.PrivateKey { |
| 83 | return ed25519.NewKeyFromSeed([]byte("the-valley web export test key!!")) |
| 84 | } |
| 85 | |
| 86 | // Verifier is the key a receiver pins to admit the fixture's manifest. |
| 87 | func Verifier() note.VerifierKey { |
| 88 | return note.NewVerifierKey(SignerName, Key().Public().(ed25519.PublicKey)) |
| 89 | } |
| 90 | |
| 91 | // Fixture is the synthetic valley and everything built from it. |
| 92 | type Fixture struct { |
| 93 | // Inputs are the synthetic valley's JSON, its visibility, its source |
| 94 | // tips and the display mapping: what the projection reads. |
| 95 | Inputs export.Inputs |
| 96 | // Repos are both projects' bare repositories, by project name. |
| 97 | Repos map[string]string |
| 98 | |
| 99 | // Export is the export built from Inputs, and ExportJSON its bytes. |
| 100 | Export *export.Export |
| 101 | ExportJSON []byte |
| 102 | // Bundles are each exported project's bundle of main, by project. |
| 103 | Bundles map[string]string |
| 104 | // Manifest is the export's manifest, and Signed the manifest signed |
| 105 | // with Key. |
| 106 | Manifest *export.Manifest |
| 107 | Signed []byte |
| 108 | |
| 109 | // Dir is a complete export directory, laid out as a generation is: |
| 110 | // export.json and repos/<project>.git. |
| 111 | Dir string |
| 112 | } |
| 113 | |
| 114 | // New builds the fixture under a temporary directory of t's. |
| 115 | func New(t testing.TB) *Fixture { |
| 116 | t.Helper() |
| 117 | root := t.TempDir() |
| 118 | f := &Fixture{Repos: map[string]string{}, Bundles: map[string]string{}} |
| 119 | ctx := context.Background() |
| 120 | |
| 121 | in := export.Inputs{ |
| 122 | Plan: read(t, "valley/plan.json"), |
| 123 | Pending: read(t, "valley/pending.json"), |
| 124 | Identities: Identities, |
| 125 | SourceTips: map[string]string{}, |
| 126 | Principals: Principals, |
| 127 | Groups: Groups, |
| 128 | Placements: Placements, |
| 129 | } |
| 130 | details, err := fs.Glob(valley, "valley/details/*/*.json") |
| 131 | if err != nil { |
| 132 | t.Fatal(err) |
| 133 | } |
| 134 | for _, path := range details { |
| 135 | in.Details = append(in.Details, read(t, path)) |
| 136 | } |
| 137 | |
| 138 | for name, commits := range history { |
| 139 | repo := filepath.Join(root, "valley", name+".git") |
| 140 | git(t, "", nil, "init", "--quiet", "--bare", "--template=", "--initial-branch=main", repo) |
| 141 | git(t, repo, fastImport(commits), "fast-import", "--quiet") |
| 142 | f.Repos[name] = repo |
| 143 | in.SourceTips[name] = strings.TrimSpace(string(git(t, repo, nil, "rev-parse", "refs/heads/main"))) |
| 144 | } |
| 145 | // The source tip of a project that is not exported is never read. |
| 146 | for name := range in.SourceTips { |
| 147 | if _, ok := Identities[name]; !ok { |
| 148 | delete(in.SourceTips, name) |
| 149 | } |
| 150 | } |
| 151 | f.Inputs = in |
| 152 | |
| 153 | f.Export, err = export.Build(in) |
| 154 | if err != nil { |
| 155 | t.Fatal(err) |
| 156 | } |
| 157 | f.ExportJSON, err = f.Export.Encode() |
| 158 | if err != nil { |
| 159 | t.Fatal(err) |
| 160 | } |
| 161 | |
| 162 | bodies := map[string]export.Body{} |
| 163 | for _, p := range f.Export.Projects { |
| 164 | path := filepath.Join(root, "bundles", p.Name+".bundle") |
| 165 | if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { |
| 166 | t.Fatal(err) |
| 167 | } |
| 168 | git(t, f.Repos[p.Name], nil, "bundle", "create", "--quiet", path, "main") |
| 169 | f.Bundles[p.Name] = path |
| 170 | if bodies[p.Name], err = export.FileBody(p.Name, path); err != nil { |
| 171 | t.Fatal(err) |
| 172 | } |
| 173 | } |
| 174 | f.Manifest, err = export.NewManifest(Instance, Destination, SignedAt.UnixNano(), f.Export, f.ExportJSON, bodies) |
| 175 | if err != nil { |
| 176 | t.Fatal(err) |
| 177 | } |
| 178 | f.Signed = f.Sign(t, f.Manifest) |
| 179 | |
| 180 | f.Dir = filepath.Join(root, "export") |
| 181 | if err := os.MkdirAll(filepath.Join(f.Dir, "repos"), 0o755); err != nil { |
| 182 | t.Fatal(err) |
| 183 | } |
| 184 | if err := os.WriteFile(filepath.Join(f.Dir, export.ExportFile), f.ExportJSON, 0o644); err != nil { |
| 185 | t.Fatal(err) |
| 186 | } |
| 187 | for _, p := range f.Export.Projects { |
| 188 | repo := filepath.Join(f.Dir, "repos", p.Name+".git") |
| 189 | if err := export.AcceptBundle(ctx, f.Bundles[p.Name], repo, p.SourceTip); err != nil { |
| 190 | t.Fatal(err) |
| 191 | } |
| 192 | } |
| 193 | return f |
| 194 | } |
| 195 | |
| 196 | // Sign signs a manifest with the fixture's key, so a test can sign a |
| 197 | // manifest it changed. |
| 198 | func (f *Fixture) Sign(t testing.TB, m *export.Manifest) []byte { |
| 199 | t.Helper() |
| 200 | signed, err := m.Sign(SignerName, Key()) |
| 201 | if err != nil { |
| 202 | t.Fatal(err) |
| 203 | } |
| 204 | return signed |
| 205 | } |
| 206 | |
| 207 | // The publication identities Private gives site, which New does not |
| 208 | // export, and docs, a copy of site. |
| 209 | const ( |
| 210 | SiteIdentity = "273d5668702918ea7ae079de374db96b" |
| 211 | DocsIdentity = "5b1e0c7a9f3d2e4c6a8b0d1f3e5c7a92" |
| 212 | ) |
| 213 | |
| 214 | // Private is the fixture's valley exported as for an edge where named |
| 215 | // readers read projects anyone may not: app, site, and docs, a copy of |
| 216 | // site under an identity of its own, in a group of its own, pages. Each of |
| 217 | // site's knots has a body naming it. The export carries no visibility, so |
| 218 | // an authorization alone says who may read which. Its manifest is signed |
| 219 | // at SignedAt, and its Dir is not made. |
| 220 | func (f *Fixture) Private(t testing.TB) *Fixture { |
| 221 | t.Helper() |
| 222 | var plan struct { |
| 223 | Projects []struct { |
| 224 | Project string `json:"project"` |
| 225 | Tip string `json:"tip"` |
| 226 | Knots []struct { |
| 227 | ID string `json:"id"` |
| 228 | } `json:"knots"` |
| 229 | } `json:"projects"` |
| 230 | } |
| 231 | if err := json.Unmarshal(f.Inputs.Plan, &plan); err != nil { |
| 232 | t.Fatal(err) |
| 233 | } |
| 234 | in := f.Inputs |
| 235 | in.Identities = map[string]string{"app": Identities["app"], "site": SiteIdentity} |
| 236 | in.SourceTips = map[string]string{"app": f.Inputs.SourceTips["app"], |
| 237 | "site": strings.TrimSpace(string(git(t, f.Repos["site"], nil, "rev-parse", "refs/heads/main")))} |
| 238 | in.Details = append([][]byte(nil), f.Inputs.Details...) |
| 239 | for _, p := range plan.Projects { |
| 240 | if p.Project != "site" { |
| 241 | continue |
| 242 | } |
| 243 | for _, k := range p.Knots { |
| 244 | detail, err := json.Marshal(map[string]any{"schema": "valley-knot/v1", "project": "site", "tip": p.Tip, |
| 245 | "knot": map[string]any{"id": k.ID, "body": "What site says of " + k.ID + ".\n", "comments": []any{}}}) |
| 246 | if err != nil { |
| 247 | t.Fatal(err) |
| 248 | } |
| 249 | in.Details = append(in.Details, detail) |
| 250 | } |
| 251 | } |
| 252 | e, err := export.Build(in) |
| 253 | if err != nil { |
| 254 | t.Fatal(err) |
| 255 | } |
| 256 | for _, p := range e.Projects { |
| 257 | if p.Name == "site" { |
| 258 | docs := p |
| 259 | docs.Name, docs.Identity, docs.Group, docs.WebName = "docs", DocsIdentity, "web", "docs" |
| 260 | docs.FormerNames, docs.Aliases = []string{"old-docs"}, []string{"d"} |
| 261 | e.Projects = append(e.Projects, docs) |
| 262 | break |
| 263 | } |
| 264 | } |
| 265 | sort.Slice(e.Projects, func(i, j int) bool { return e.Projects[i].Name < e.Projects[j].Name }) |
| 266 | e.Groups = append(e.Groups, export.Group{ID: "web", Name: "pages", Aliases: []string{"p"}}) |
| 267 | data, err := e.Encode() |
| 268 | if err != nil { |
| 269 | t.Fatal(err) |
| 270 | } |
| 271 | site := filepath.Join(t.TempDir(), "site.bundle") |
| 272 | git(t, f.Repos["site"], nil, "bundle", "create", "--quiet", site, "main") |
| 273 | p := &Fixture{Inputs: in, Repos: f.Repos, Export: e, ExportJSON: data, |
| 274 | Bundles: map[string]string{"app": f.Bundles["app"], "site": site, "docs": site}} |
| 275 | bodies := map[string]export.Body{} |
| 276 | for name, path := range p.Bundles { |
| 277 | if bodies[name], err = export.FileBody(name, path); err != nil { |
| 278 | t.Fatal(err) |
| 279 | } |
| 280 | } |
| 281 | if p.Manifest, err = export.NewManifest(Instance, Destination, SignedAt.UnixNano(), e, data, bodies); err != nil { |
| 282 | t.Fatal(err) |
| 283 | } |
| 284 | p.Signed = p.Sign(t, p.Manifest) |
| 285 | return p |
| 286 | } |
| 287 | |
| 288 | // StorePath is the path of a new store's descriptor, in a directory of |
| 289 | // its own. |
| 290 | func StorePath(t testing.TB) string { |
| 291 | t.Helper() |
| 292 | return filepath.Join(t.TempDir(), "store") |
| 293 | } |
| 294 | |
| 295 | // Authorization is an authorization with sequence seq that lets anyone |
| 296 | // read each project given, under its identity in Identities. |
| 297 | func Authorization(seq int64, projects ...string) *export.Authorization { |
| 298 | a := &export.Authorization{Instance: Instance, Destination: Destination, Sequence: seq, Checked: Collected} |
| 299 | for _, name := range projects { |
| 300 | a.Projects = append(a.Projects, export.AuthorizedProject{Name: name, Identity: Identities[name], Readers: []string{export.Anyone}}) |
| 301 | } |
| 302 | return a |
| 303 | } |
| 304 | |
| 305 | // SignAuthorization is the authorization signed by the fixture's key. |
| 306 | func SignAuthorization(t testing.TB, a *export.Authorization) []byte { |
| 307 | t.Helper() |
| 308 | signed, err := a.Sign(SignerName, Key()) |
| 309 | if err != nil { |
| 310 | t.Fatal(err) |
| 311 | } |
| 312 | return signed |
| 313 | } |
| 314 | |
| 315 | // Authorize stores in s, as a receiver does, an authorization one |
| 316 | // sequence past the stored one's that lets anyone read each project given. |
| 317 | func Authorize(t testing.TB, s *export.Store, projects ...string) *export.Authorization { |
| 318 | t.Helper() |
| 319 | hw, err := s.AuthorizationHighWater() |
| 320 | if err != nil { |
| 321 | t.Fatal(err) |
| 322 | } |
| 323 | a := Authorization(hw+1, projects...) |
| 324 | if err := s.Authorize(context.Background(), a, SignAuthorization(t, a)); err != nil { |
| 325 | t.Fatal(err) |
| 326 | } |
| 327 | return a |
| 328 | } |
| 329 | |
| 330 | // Admission is what a receiver admits the fixture's manifest with: the |
| 331 | // pinned verifier, the instance and destination, no high-water mark, and a |
| 332 | // clock a second past the signing time. |
| 333 | func (f *Fixture) Admission() export.Admission { |
| 334 | return export.Admission{ |
| 335 | Verifiers: []note.VerifierKey{Verifier()}, |
| 336 | Instance: Instance, |
| 337 | Destination: Destination, |
| 338 | Now: SignedAt.Add(time.Second), |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | // BodyBytes is the content of the body the manifest declares under name: |
| 343 | // export.json, or a project's bundle. |
| 344 | func (f *Fixture) BodyBytes(t testing.TB, name string) []byte { |
| 345 | t.Helper() |
| 346 | if name == export.ExportFile { |
| 347 | return f.ExportJSON |
| 348 | } |
| 349 | path, ok := f.Bundles[name] |
| 350 | if !ok { |
| 351 | t.Fatalf("the fixture has no body %s", name) |
| 352 | } |
| 353 | data, err := os.ReadFile(path) |
| 354 | if err != nil { |
| 355 | t.Fatal(err) |
| 356 | } |
| 357 | return data |
| 358 | } |
| 359 | |
| 360 | // Upload runs the upload's three steps against the store, as a receiver |
| 361 | // does: it admits the signed manifest, stages it, stages every body the |
| 362 | // inventory needs, and activates it. It returns the active generation. A |
| 363 | // store that holds no authorization is first given one that lets anyone |
| 364 | // read every project the fixture exports, as a publisher sends one before |
| 365 | // content. |
| 366 | func (f *Fixture) Upload(t testing.TB, s *export.Store) export.Generation { |
| 367 | t.Helper() |
| 368 | if _, err := s.ReadAuthorization(); errors.Is(err, export.ErrNoAuthorization) { |
| 369 | var names []string |
| 370 | for _, p := range f.Export.Projects { |
| 371 | names = append(names, p.Name) |
| 372 | } |
| 373 | Authorize(t, s, names...) |
| 374 | } |
| 375 | hw, err := s.HighWater() |
| 376 | if err != nil { |
| 377 | t.Fatal(err) |
| 378 | } |
| 379 | a := f.Admission() |
| 380 | a.HighWater = hw |
| 381 | m, err := export.VerifyManifest(bytes.NewReader(f.Signed), a) |
| 382 | if err != nil { |
| 383 | t.Fatal(err) |
| 384 | } |
| 385 | st, err := s.Stage(context.Background(), m) |
| 386 | if err != nil { |
| 387 | t.Fatal(err) |
| 388 | } |
| 389 | inv, err := s.Inventory(st) |
| 390 | if err != nil { |
| 391 | t.Fatal(err) |
| 392 | } |
| 393 | for _, need := range inv.Needs { |
| 394 | body := f.BodyBytes(t, need.Name) |
| 395 | req := httptest.NewRequest("PUT", export.BodyPath(m.Sequence, need.SHA256), bytes.NewReader(body)) |
| 396 | want := export.Body{Name: need.Name, SHA256: need.SHA256, Size: need.Size} |
| 397 | if _, err := export.StageBody(st.Bodies(), want, 64<<20, req); err != nil { |
| 398 | t.Fatal(err) |
| 399 | } |
| 400 | } |
| 401 | g, err := s.Activate(context.Background(), st) |
| 402 | if err != nil { |
| 403 | t.Fatal(err) |
| 404 | } |
| 405 | return g |
| 406 | } |
| 407 | |
| 408 | func read(t testing.TB, path string) []byte { |
| 409 | t.Helper() |
| 410 | data, err := valley.ReadFile(path) |
| 411 | if err != nil { |
| 412 | t.Fatal(err) |
| 413 | } |
| 414 | return data |
| 415 | } |
| 416 | |
| 417 | // commit is one commit of a fixture repository's main: its time, its |
| 418 | // message, and the files it writes. |
| 419 | type commit struct { |
| 420 | at time.Time |
| 421 | message string |
| 422 | files map[string]string |
| 423 | } |
| 424 | |
| 425 | // history is each project's main, oldest commit first. Every commit's |
| 426 | // author, committer and time are fixed, so the tips are the same in every |
| 427 | // run. |
| 428 | var history = map[string][]commit{ |
| 429 | "app": { |
| 430 | {time.Date(2026, 10, 1, 9, 0, 0, 0, time.UTC), "app: the first README", map[string]string{ |
| 431 | "README.md": "# app\n\nA synthetic project for the web UI's tests.\n", |
| 432 | }}, |
| 433 | {time.Date(2026, 10, 2, 9, 0, 0, 0, time.UTC), "app: a program", map[string]string{ |
| 434 | "src/main.go": "package main\n\nfunc main() {}\n", |
| 435 | }}, |
| 436 | {time.Date(2026, 10, 3, 9, 0, 0, 0, time.UTC), "docs: a guide, and the README names it", map[string]string{ |
| 437 | "README.md": "# app\n\nA synthetic project for the web UI's tests. See docs/guide.md.\n\n<script>alert(\"readme\")</script>\n", |
| 438 | "docs/guide.md": "# Guide\n\nRead the plan, then the source.\n", |
| 439 | }}, |
| 440 | }, |
| 441 | "site": { |
| 442 | {time.Date(2026, 10, 1, 9, 0, 0, 0, time.UTC), "site: hidden", map[string]string{ |
| 443 | "README.md": "# site\n\nThis project is not exported.\n", |
| 444 | }}, |
| 445 | }, |
| 446 | } |
| 447 | |
| 448 | // fastImport is the git fast-import stream that writes commits to main. |
| 449 | func fastImport(commits []commit) []byte { |
| 450 | var b bytes.Buffer |
| 451 | data := func(s string) { fmt.Fprintf(&b, "data %d\n%s\n", len(s), s) } |
| 452 | for _, c := range commits { |
| 453 | who := fmt.Sprintf("Example Author <author@example.org> %d +0000", c.at.Unix()) |
| 454 | fmt.Fprintf(&b, "commit refs/heads/main\nauthor %s\ncommitter %s\n", who, who) |
| 455 | data(c.message + "\n") |
| 456 | var paths []string |
| 457 | for path := range c.files { |
| 458 | paths = append(paths, path) |
| 459 | } |
| 460 | sort.Strings(paths) |
| 461 | for _, path := range paths { |
| 462 | fmt.Fprintf(&b, "M 100644 inline %s\n", path) |
| 463 | data(c.files[path]) |
| 464 | } |
| 465 | } |
| 466 | return b.Bytes() |
| 467 | } |
| 468 | |
| 469 | // gitTimeout is how long the fixture waits for one git command. |
| 470 | const gitTimeout = 2 * time.Minute |
| 471 | |
| 472 | // git runs git as the export package does: in dir unless it is "", with no |
| 473 | // GIT_ variable of the caller's and no system or global configuration. It |
| 474 | // runs git with Run, so git and anything it started are killed once git |
| 475 | // exits, or once gitTimeout has passed, which fails the test. |
| 476 | func git(t testing.TB, dir string, stdin []byte, args ...string) []byte { |
| 477 | t.Helper() |
| 478 | if dir != "" { |
| 479 | args = append([]string{"-C", dir}, args...) |
| 480 | } |
| 481 | cmd := exec.Command(export.GitProgram, args...) |
| 482 | for _, kv := range os.Environ() { |
| 483 | if !strings.HasPrefix(kv, "GIT_") { |
| 484 | cmd.Env = append(cmd.Env, kv) |
| 485 | } |
| 486 | } |
| 487 | cmd.Env = append(cmd.Env, "GIT_CONFIG_NOSYSTEM=1", "GIT_CONFIG_GLOBAL=/dev/null") |
| 488 | if stdin != nil { |
| 489 | cmd.Stdin = bytes.NewReader(stdin) |
| 490 | } |
| 491 | var out, stderr bytes.Buffer |
| 492 | cmd.Stdout, cmd.Stderr = &out, &stderr |
| 493 | if err := Run(t, cmd, gitTimeout); err != nil { |
| 494 | t.Fatalf("git %s: %v: %s", strings.Join(args, " "), err, stderr.String()) |
| 495 | } |
| 496 | return out.Bytes() |
| 497 | } |