| 1 | package exporttest_test |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "flag" |
| 8 | "os" |
| 9 | "testing" |
| 10 | |
| 11 | "the-valley/webui/export" |
| 12 | "the-valley/webui/export/exporttest" |
| 13 | ) |
| 14 | |
| 15 | var update = flag.Bool("update", false, "rewrite testdata/export.json from the synthetic valley") |
| 16 | |
| 17 | // The export built from the synthetic valley is the contract's expected |
| 18 | // output, byte for byte once indented: a change to the projection, or to |
| 19 | // the valley's JSON, shows here as a change to testdata/export.json. |
| 20 | func TestExportIsTheExpectedOne(t *testing.T) { |
| 21 | f := exporttest.New(t) |
| 22 | var got bytes.Buffer |
| 23 | if err := json.Indent(&got, f.ExportJSON, "", " "); err != nil { |
| 24 | t.Fatal(err) |
| 25 | } |
| 26 | if *update { |
| 27 | if err := os.WriteFile("testdata/export.json", got.Bytes(), 0o644); err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | } |
| 31 | want, err := os.ReadFile("testdata/export.json") |
| 32 | if err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | if !bytes.Equal(got.Bytes(), want) { |
| 36 | t.Errorf("the export differs from testdata/export.json; go test -run %s -update rewrites it, and the diff is the change:\n%s", t.Name(), got.Bytes()) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | // The fixture is self-consistent: its manifest opens under its verifier |
| 41 | // and agrees with its export.json, its export directory serves main at the |
| 42 | // source tip, and it uploads through a store. |
| 43 | func TestFixtureIsConsistent(t *testing.T) { |
| 44 | f := exporttest.New(t) |
| 45 | ctx := context.Background() |
| 46 | m, err := export.VerifyManifest(bytes.NewReader(f.Signed), f.Admission()) |
| 47 | if err != nil { |
| 48 | t.Fatal(err) |
| 49 | } |
| 50 | if _, err := m.CheckExport(f.ExportJSON); err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | dir := export.Generation{Dir: f.Dir} |
| 54 | for _, p := range f.Export.Projects { |
| 55 | repo, _ := dir.Repo(p.Name) |
| 56 | if ok, err := export.Reachable(ctx, repo, p.SourceTip, p.SourceTip); err != nil || !ok { |
| 57 | t.Errorf("%s's tip is not reachable in the export directory: %v %v", p.Name, ok, err) |
| 58 | } |
| 59 | } |
| 60 | s, err := export.OpenStore(t.TempDir()) |
| 61 | if err != nil { |
| 62 | t.Fatal(err) |
| 63 | } |
| 64 | g := f.Upload(t, s) |
| 65 | if g.Sequence != exporttest.SignedAt.UnixNano() { |
| 66 | t.Errorf("activated sequence %d, want the fixture's", g.Sequence) |
| 67 | } |
| 68 | e, err := g.Export() |
| 69 | if err != nil { |
| 70 | t.Fatal(err) |
| 71 | } |
| 72 | if got, _ := e.Digest(); got != digest(t, f.Export) { |
| 73 | t.Errorf("the activated export's digest differs from the fixture's") |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | func digest(t *testing.T, e *export.Export) string { |
| 78 | t.Helper() |
| 79 | d, err := e.Digest() |
| 80 | if err != nil { |
| 81 | t.Fatal(err) |
| 82 | } |
| 83 | return d |
| 84 | } |