~bigbes/sr-ht-spec

ref: eaa2b65f2b205ccda00b2f09264f6fc966209373 sr-ht-spec/db/document_test.go -rw-r--r-- 5.7 KiB
eaa2b65f — bigbes fix(service): separate the read-plane rev guard from the review path 27 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package db

import (
	"context"
	"errors"
	"testing"
)

func TestDocumentRegistryLifecycle(t *testing.T) {
	s, _, cleanup := newTestStore(t)
	defer cleanup()
	ctx := context.Background()

	space := mkSpace(t, s, ctx, "bigbes", "rfcs")
	id := docID(t, "SPEC-0007")

	doc, err := s.RegisterDocID(ctx, space.ID, DocRef{ID: id, Path: "specs/0007-storage.md"}, "rev1")
	if err != nil {
		t.Fatalf("register: %v", err)
	}
	if doc.SpaceID != space.ID || doc.Path != "specs/0007-storage.md" || doc.UpdatedRev != "rev1" {
		t.Fatalf("unexpected registered doc: %+v", doc)
	}

	// The doc_id PRIMARY KEY makes a second claim unwritable, in this space or
	// any other.
	if _, err := s.RegisterDocID(ctx, space.ID, DocRef{ID: id, Path: "other.md"}, "rev2"); !errors.Is(err, ErrDocIDTaken) {
		t.Fatalf("re-register in the same space = %v, want ErrDocIDTaken", err)
	}
	other := mkSpace(t, s, ctx, "bigbes", "notes")
	if _, err := s.RegisterDocID(ctx, other.ID, DocRef{ID: id, Path: "n.md"}, "rev2"); !errors.Is(err, ErrDocIDTaken) {
		t.Fatalf("re-register in another space = %v, want ErrDocIDTaken", err)
	}

	got, err := s.DocByID(ctx, id)
	if err != nil {
		t.Fatalf("lookup: %v", err)
	}
	if got.ID != id || got.Path != "specs/0007-storage.md" {
		t.Fatalf("unexpected lookup: %+v", got)
	}
	if _, err := s.DocByID(ctx, docID(t, "SPEC-9999")); !errors.Is(err, ErrNotFound) {
		t.Fatalf("missing id = %v, want ErrNotFound", err)
	}

	// Rename: the ID is stable, the path moves.
	if err := s.SetDocPath(ctx, space.ID, DocRef{ID: id, Path: "specs/0007-proposals.md"}, "rev3"); err != nil {
		t.Fatalf("rename: %v", err)
	}
	got, err = s.DocByID(ctx, id)
	if err != nil {
		t.Fatalf("lookup after rename: %v", err)
	}
	if got.Path != "specs/0007-proposals.md" || got.UpdatedRev != "rev3" {
		t.Fatalf("rename did not stick: %+v", got)
	}

	// A rename scoped to the wrong space must not relocate the document.
	if err := s.SetDocPath(ctx, other.ID, DocRef{ID: id, Path: "stolen.md"}, "rev4"); !errors.Is(err, ErrNotFound) {
		t.Fatalf("cross-space rename = %v, want ErrNotFound", err)
	}
	if got, _ := s.DocByID(ctx, id); got.Path != "specs/0007-proposals.md" {
		t.Fatalf("cross-space rename moved the document: %+v", got)
	}

	docs, err := s.ListDocsBySpace(ctx, space.ID)
	if err != nil {
		t.Fatalf("list docs: %v", err)
	}
	if len(docs) != 1 || docs[0].ID != id {
		t.Fatalf("unexpected doc list: %+v", docs)
	}

	// Deletion (human push only) frees the ID again.
	if err := s.UnregisterDocID(ctx, other.ID, id); !errors.Is(err, ErrNotFound) {
		t.Fatalf("unregister from the wrong space = %v, want ErrNotFound", err)
	}
	if err := s.UnregisterDocID(ctx, space.ID, id); err != nil {
		t.Fatalf("unregister: %v", err)
	}
	if _, err := s.DocByID(ctx, id); !errors.Is(err, ErrNotFound) {
		t.Fatalf("after unregister = %v, want ErrNotFound", err)
	}
}

func TestDocIDCollisionCheck(t *testing.T) {
	s, _, cleanup := newTestStore(t)
	defer cleanup()
	ctx := context.Background()

	specs := mkSpace(t, s, ctx, "bigbes", "rfcs")
	notes := mkSpace(t, s, ctx, "bigbes", "notes")

	mine := docID(t, "SPEC-1")
	theirs := docID(t, "NOTE-1")
	if _, err := s.RegisterDocID(ctx, specs.ID, DocRef{ID: mine, Path: "a.md"}, "rev1"); err != nil {
		t.Fatalf("register: %v", err)
	}
	if _, err := s.RegisterDocID(ctx, notes.ID, DocRef{ID: theirs, Path: "b.md"}, "rev1"); err != nil {
		t.Fatalf("register: %v", err)
	}

	// Pushing into specs: SPEC-1 is ours (a rename at worst), NOTE-1 is not.
	batch := []DocRef{
		{ID: mine, Path: "a-renamed.md"},
		{ID: theirs, Path: "c.md"},
		{ID: docID(t, "SPEC-2"), Path: "d.md"},
	}
	collisions, err := s.CheckDocIDCollisions(ctx, specs.ID, batch)
	if err != nil {
		t.Fatalf("collision check: %v", err)
	}
	if len(collisions) != 1 {
		t.Fatalf("expected 1 collision, got %+v", collisions)
	}
	if collisions[0].DocID != theirs || collisions[0].Existing.SpaceID != notes.ID {
		t.Fatalf("unexpected collision: %+v", collisions[0])
	}
}

func TestUpsertDocIDsRejectsCrossSpaceClaim(t *testing.T) {
	s, _, cleanup := newTestStore(t)
	defer cleanup()
	ctx := context.Background()

	specs := mkSpace(t, s, ctx, "bigbes", "rfcs")
	notes := mkSpace(t, s, ctx, "bigbes", "notes")
	stolen := docID(t, "NOTE-1")
	if _, err := s.RegisterDocID(ctx, notes.ID, DocRef{ID: stolen, Path: "n.md"}, "rev1"); err != nil {
		t.Fatalf("register: %v", err)
	}

	// A fresh ID plus one owned by another space: the statement's ON CONFLICT
	// guard leaves the foreign row alone and the missing row surfaces as a
	// CollisionError.
	fresh := docID(t, "SPEC-1")
	err := s.UpsertDocIDs(ctx, specs.ID, []DocRef{
		{ID: fresh, Path: "a.md"},
		{ID: stolen, Path: "b.md"},
	}, "rev2")
	if !errors.Is(err, ErrDocIDTaken) {
		t.Fatalf("cross-space upsert = %v, want ErrDocIDTaken", err)
	}
	var ce *CollisionError
	if !errors.As(err, &ce) || len(ce.Collisions) != 1 || ce.Collisions[0].DocID != stolen {
		t.Fatalf("expected a CollisionError naming %s, got %v", stolen, err)
	}
	// The foreign row is untouched.
	got, err := s.DocByID(ctx, stolen)
	if err != nil {
		t.Fatalf("lookup foreign doc: %v", err)
	}
	if got.SpaceID != notes.ID || got.Path != "n.md" || got.UpdatedRev != "rev1" {
		t.Fatalf("foreign row was modified: %+v", got)
	}

	// A clean batch inserts and then updates in place.
	if err := s.UpsertDocIDs(ctx, specs.ID, []DocRef{
		{ID: fresh, Path: "a.md"},
		{ID: docID(t, "SPEC-2"), Path: "b.md"},
	}, "rev3"); err != nil {
		t.Fatalf("clean upsert: %v", err)
	}
	if err := s.UpsertDocIDs(ctx, specs.ID, []DocRef{
		{ID: fresh, Path: "a-moved.md"},
	}, "rev4"); err != nil {
		t.Fatalf("upsert rename: %v", err)
	}
	got, err = s.DocByID(ctx, fresh)
	if err != nil {
		t.Fatalf("lookup: %v", err)
	}
	if got.Path != "a-moved.md" || got.UpdatedRev != "rev4" {
		t.Fatalf("upsert rename did not stick: %+v", got)
	}
}