test(snapshot): cover content addressed publication
- commit
- ef93e42a5aa725cca841f8f7664611f3f4618a2f
- author
- brice · · 2026-07-21 19:52 UTC
- committer
- brice · · 2026-07-21 19:53 UTC
- parent
- e144d99
- signature
- GPG Verified
CD88F9E564E0102646FB6077A1A07C7C7C3E349D· arch-gpg Verified for the committer email
1 file · +71 -0
A internal/adapters/snapshot/repository_test.go +71 -0
| @@ -0,0 +1,71 @@ | |||
| 1 | + | package snapshot | |
| 2 | + | ||
| 3 | + | import ( | |
| 4 | + | "context" | |
| 5 | + | "testing" | |
| 6 | + | ||
| 7 | + | "github.com/bnema/vev/internal/ports" | |
| 8 | + | codec "github.com/bnema/vev/internal/usecase/snapshot" | |
| 9 | + | ) | |
| 10 | + | ||
| 11 | + | func TestRepositoryPublishesAndLoadsCompleteGeneration(t *testing.T) { | |
| 12 | + | t.Parallel() | |
| 13 | + | dir := privateDir(t) | |
| 14 | + | repo := NewRepository(dir) | |
| 15 | + | pub := repositoryPublication(t, "named", 1, []byte("state")) | |
| 16 | + | if err := repo.Publish(context.Background(), pub); err != nil { | |
| 17 | + | t.Fatalf("Publish: %v", err) | |
| 18 | + | } | |
| 19 | + | got, err := repo.Load(context.Background(), "named") | |
| 20 | + | if err != nil { | |
| 21 | + | t.Fatalf("Load: %v", err) | |
| 22 | + | } | |
| 23 | + | if got.Name != "named" || got.Generation != 1 || len(got.Objects) != 2 { | |
| 24 | + | t.Fatalf("Load = %#v", got) | |
| 25 | + | } | |
| 26 | + | } | |
| 27 | + | ||
| 28 | + | func TestRepositoryDoesNotRewriteVerifiedImmutableBlob(t *testing.T) { | |
| 29 | + | t.Parallel() | |
| 30 | + | dir := privateDir(t) | |
| 31 | + | repo := NewRepository(dir) | |
| 32 | + | pub := repositoryPublication(t, "named", 1, []byte("state")) | |
| 33 | + | if err := repo.Publish(context.Background(), pub); err != nil { | |
| 34 | + | t.Fatal(err) | |
| 35 | + | } | |
| 36 | + | writes := 0 | |
| 37 | + | repo.hooks.beforeBlobWrite = func(string) error { writes++; return nil } | |
| 38 | + | pub.Generation = 2 | |
| 39 | + | manifest, err := codec.UnmarshalManifest(pub.Manifest) | |
| 40 | + | if err != nil { | |
| 41 | + | t.Fatal(err) | |
| 42 | + | } | |
| 43 | + | manifest.Generation = 2 | |
| 44 | + | pub.Manifest, err = codec.MarshalManifest(manifest) | |
| 45 | + | if err != nil { | |
| 46 | + | t.Fatal(err) | |
| 47 | + | } | |
| 48 | + | if err := repo.Publish(context.Background(), pub); err != nil { | |
| 49 | + | t.Fatal(err) | |
| 50 | + | } | |
| 51 | + | if writes != 0 { | |
| 52 | + | t.Fatalf("blob writes = %d, want 0", writes) | |
| 53 | + | } | |
| 54 | + | } | |
| 55 | + | ||
| 56 | + | func repositoryPublication(t *testing.T, name string, generation uint64, payload []byte) ports.SnapshotPublication { | |
| 57 | + | t.Helper() | |
| 58 | + | tail, err := codec.MarshalObject(codec.HistoryTail, payload) | |
| 59 | + | if err != nil { | |
| 60 | + | t.Fatal(err) | |
| 61 | + | } | |
| 62 | + | visible, err := codec.MarshalObject(codec.Visible, []byte("visible")) | |
| 63 | + | if err != nil { | |
| 64 | + | t.Fatal(err) | |
| 65 | + | } | |
| 66 | + | manifest, err := codec.MarshalManifest(codec.Manifest{Generation: generation, Name: name, Tabs: []codec.ManifestTab{{Cols: 1, Rows: 1, Panes: []codec.ManifestPane{{ID: "p", Tail: codec.ObjectRef{Kind: codec.HistoryTail, Digest: tail.Digest, Size: uint32(len(tail.Data))}, Visible: codec.ObjectRef{Kind: codec.Visible, Digest: visible.Digest, Size: uint32(len(visible.Data))}}}}}}) | |
| 67 | + | if err != nil { | |
| 68 | + | t.Fatal(err) | |
| 69 | + | } | |
| 70 | + | return ports.SnapshotPublication{Name: name, Generation: generation, Manifest: manifest, Objects: []ports.SnapshotObject{tail, visible}} | |
| 71 | + | } | |
| @@ -0,0 +1,71 @@ | |||||
| 1 | + | package snapshot | |||
| 2 | + | ||||
| 3 | + | import ( | |||
| 4 | + | "context" | |||
| 5 | + | "testing" | |||
| 6 | + | ||||
| 7 | + | "github.com/bnema/vev/internal/ports" | |||
| 8 | + | codec "github.com/bnema/vev/internal/usecase/snapshot" | |||
| 9 | + | ) | |||
| 10 | + | ||||
| 11 | + | func TestRepositoryPublishesAndLoadsCompleteGeneration(t *testing.T) { | |||
| 12 | + | t.Parallel() | |||
| 13 | + | dir := privateDir(t) | |||
| 14 | + | repo := NewRepository(dir) | |||
| 15 | + | pub := repositoryPublication(t, "named", 1, []byte("state")) | |||
| 16 | + | if err := repo.Publish(context.Background(), pub); err != nil { | |||
| 17 | + | t.Fatalf("Publish: %v", err) | |||
| 18 | + | } | |||
| 19 | + | got, err := repo.Load(context.Background(), "named") | |||
| 20 | + | if err != nil { | |||
| 21 | + | t.Fatalf("Load: %v", err) | |||
| 22 | + | } | |||
| 23 | + | if got.Name != "named" || got.Generation != 1 || len(got.Objects) != 2 { | |||
| 24 | + | t.Fatalf("Load = %#v", got) | |||
| 25 | + | } | |||
| 26 | + | } | |||
| 27 | + | ||||
| 28 | + | func TestRepositoryDoesNotRewriteVerifiedImmutableBlob(t *testing.T) { | |||
| 29 | + | t.Parallel() | |||
| 30 | + | dir := privateDir(t) | |||
| 31 | + | repo := NewRepository(dir) | |||
| 32 | + | pub := repositoryPublication(t, "named", 1, []byte("state")) | |||
| 33 | + | if err := repo.Publish(context.Background(), pub); err != nil { | |||
| 34 | + | t.Fatal(err) | |||
| 35 | + | } | |||
| 36 | + | writes := 0 | |||
| 37 | + | repo.hooks.beforeBlobWrite = func(string) error { writes++; return nil } | |||
| 38 | + | pub.Generation = 2 | |||
| 39 | + | manifest, err := codec.UnmarshalManifest(pub.Manifest) | |||
| 40 | + | if err != nil { | |||
| 41 | + | t.Fatal(err) | |||
| 42 | + | } | |||
| 43 | + | manifest.Generation = 2 | |||
| 44 | + | pub.Manifest, err = codec.MarshalManifest(manifest) | |||
| 45 | + | if err != nil { | |||
| 46 | + | t.Fatal(err) | |||
| 47 | + | } | |||
| 48 | + | if err := repo.Publish(context.Background(), pub); err != nil { | |||
| 49 | + | t.Fatal(err) | |||
| 50 | + | } | |||
| 51 | + | if writes != 0 { | |||
| 52 | + | t.Fatalf("blob writes = %d, want 0", writes) | |||
| 53 | + | } | |||
| 54 | + | } | |||
| 55 | + | ||||
| 56 | + | func repositoryPublication(t *testing.T, name string, generation uint64, payload []byte) ports.SnapshotPublication { | |||
| 57 | + | t.Helper() | |||
| 58 | + | tail, err := codec.MarshalObject(codec.HistoryTail, payload) | |||
| 59 | + | if err != nil { | |||
| 60 | + | t.Fatal(err) | |||
| 61 | + | } | |||
| 62 | + | visible, err := codec.MarshalObject(codec.Visible, []byte("visible")) | |||
| 63 | + | if err != nil { | |||
| 64 | + | t.Fatal(err) | |||
| 65 | + | } | |||
| 66 | + | manifest, err := codec.MarshalManifest(codec.Manifest{Generation: generation, Name: name, Tabs: []codec.ManifestTab{{Cols: 1, Rows: 1, Panes: []codec.ManifestPane{{ID: "p", Tail: codec.ObjectRef{Kind: codec.HistoryTail, Digest: tail.Digest, Size: uint32(len(tail.Data))}, Visible: codec.ObjectRef{Kind: codec.Visible, Digest: visible.Digest, Size: uint32(len(visible.Data))}}}}}}) | |||
| 67 | + | if err != nil { | |||
| 68 | + | t.Fatal(err) | |||
| 69 | + | } | |||
| 70 | + | return ports.SnapshotPublication{Name: name, Generation: generation, Manifest: manifest, Objects: []ports.SnapshotObject{tail, visible}} | |||
| 71 | + | } | |||