~bigbes/sr-ht-spec

ref: 692636b17c07db7feda1f954251ffe6df76368af sr-ht-spec/service/project_test.go -rw-r--r-- 10.6 KiB
692636b1 — Eugene Blikh chore(beads): spec-ejq.2 diagnosis resolved, the secret was never created 13 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package service

import (
	"context"
	"errors"
	"testing"

	"sourcecraft.dev/bigbes/sr-ht-spec/core"
)

var fxProject = core.ProjectRef{Owner: "bigbes", Name: "tarantool"}

func metaRef() core.ProjectRef {
	return core.ProjectRef{Owner: "bigbes", Name: core.MetaProjectName}
}

// The meta-project is a value, not a row: resolving it needs no database, which
// is exactly why it needs no sync job either.
func TestEverythingFilterNeedsNoStorage(t *testing.T) {
	svc, _ := newService(t) // its database handle cannot be reached

	f, err := svc.ResolveProject(context.Background(), metaRef())
	if err != nil {
		t.Fatalf("ResolveProject(meta): %v", err)
	}
	if !f.Everything() {
		t.Fatal("the meta-project must exclude nothing")
	}
	// Deliberately unenumerated: a frozen list would omit every space created
	// after the resolve.
	if len(f.Refs()) != 0 || len(f.IDs()) != 0 {
		t.Fatalf("the meta filter enumerated %d spaces; it must not", len(f.Refs()))
	}
	if f.MatchesNothing() {
		t.Fatal("the meta filter matches everything, not nothing")
	}
	if !f.Matches(fxSpace) || !f.MatchesID(1) {
		t.Fatal("the meta filter must match any space")
	}
}

// The trap this type exists to prevent: an empty project is not the corpus.
func TestEmptyFilterIsNotEverything(t *testing.T) {
	empty := core.SpacesFilter(nil, nil)
	if empty.Everything() {
		t.Fatal("a filter over no spaces must not be the meta-project")
	}
	if !empty.MatchesNothing() {
		t.Fatal("a filter with no terms selects nothing")
	}
	if empty.Matches(fxSpace) || empty.MatchesID(1) {
		t.Fatal("a filter with no terms must match no space")
	}

	// And the state that is neither answer: nobody said. It is not silently
	// one of the two — a query refuses it, which is what makes "I forgot to
	// set the scope" a failure instead of a wrong-scoped answer.
	var unset SpaceFilter
	if !unset.IsZero() || unset.Everything() {
		t.Fatal("the zero filter must be neither everything nor a set scope")
	}
}

func TestSpaceFilterMatches(t *testing.T) {
	other := core.SpaceRef{Owner: "bigbes", Name: "notes"}
	f := core.SpacesFilter([]core.SpaceRef{fxSpace}, []int{7})
	if !f.Matches(fxSpace) || !f.MatchesID(7) {
		t.Error("a member space must match")
	}
	if f.Matches(other) || f.MatchesID(8) {
		t.Error("a non-member space must not match")
	}
	if f.MatchesNothing() {
		t.Error("a filter with a term does not match nothing")
	}
}

func TestProjectLifecycle(t *testing.T) {
	svc, _ := newTestService(t)
	ctx := context.Background()

	p, err := svc.CreateProject(ctx, fxProject)
	if err != nil {
		t.Fatalf("CreateProject: %v", err)
	}
	if p.ID == 0 {
		t.Error("project has no row id")
	}
	if _, err := svc.CreateProject(ctx, fxProject); !errors.Is(err, ErrProjectExists) {
		t.Errorf("second CreateProject err = %v, want ErrProjectExists", err)
	}

	got, err := svc.GetProject(ctx, fxProject)
	if err != nil {
		t.Fatalf("GetProject: %v", err)
	}
	if got.ID != p.ID || got.Ref != fxProject {
		t.Errorf("GetProject = %+v", got)
	}
	if _, err := svc.GetProject(ctx, core.ProjectRef{Owner: "bigbes", Name: "absent"}); !errors.Is(err, ErrNotFound) {
		t.Errorf("GetProject of a missing project = %v, want ErrNotFound", err)
	}

	projects, err := svc.ListProjects(ctx)
	if err != nil {
		t.Fatalf("ListProjects: %v", err)
	}
	if len(projects) != 1 || projects[0].Ref != fxProject {
		t.Fatalf("projects = %+v", projects)
	}

	if err := svc.DeleteProject(ctx, fxProject); err != nil {
		t.Fatalf("DeleteProject: %v", err)
	}
	if err := svc.DeleteProject(ctx, fxProject); !errors.Is(err, ErrNotFound) {
		t.Errorf("second DeleteProject = %v, want ErrNotFound", err)
	}
}

// The key read: a project is a saved filter over one global index, and this is
// the filter.
func TestResolveProject(t *testing.T) {
	svc, _ := newTestService(t)
	ctx := context.Background()

	rfcs, err := svc.CreateSpace(ctx, fxSpace)
	if err != nil {
		t.Fatalf("CreateSpace: %v", err)
	}
	notes, err := svc.CreateSpace(ctx, core.SpaceRef{Owner: "bigbes", Name: "notes"})
	if err != nil {
		t.Fatalf("CreateSpace: %v", err)
	}
	if _, err := svc.CreateProject(ctx, fxProject); err != nil {
		t.Fatalf("CreateProject: %v", err)
	}

	// A project with no members selects nothing — never everything.
	f, err := svc.ResolveProject(ctx, fxProject)
	if err != nil {
		t.Fatalf("ResolveProject: %v", err)
	}
	if f.Everything() {
		t.Fatal("an empty project must not resolve to the meta-project")
	}
	if f.IsZero() {
		t.Fatal("a resolved project must carry a scope, not the zero filter")
	}
	if !f.MatchesNothing() {
		t.Fatalf("an empty project resolved to %+v", f)
	}

	if err := svc.AddSpaceToProject(ctx, fxProject, fxSpace); err != nil {
		t.Fatalf("AddSpaceToProject: %v", err)
	}
	// Membership is a set.
	if err := svc.AddSpaceToProject(ctx, fxProject, fxSpace); err != nil {
		t.Fatalf("re-adding a member: %v", err)
	}

	f, err = svc.ResolveProject(ctx, fxProject)
	if err != nil {
		t.Fatalf("ResolveProject: %v", err)
	}
	if len(f.Refs()) != 1 || f.Refs()[0] != fxSpace {
		t.Fatalf("filter refs = %+v", f.Refs())
	}
	if len(f.IDs()) != 1 || f.IDs()[0] != rfcs.ID {
		t.Fatalf("filter ids = %+v, want [%d]", f.IDs(), rfcs.ID)
	}
	if !f.Matches(fxSpace) || !f.MatchesID(rfcs.ID) {
		t.Error("the member space must match")
	}
	if f.Matches(notes.Ref) || f.MatchesID(notes.ID) {
		t.Error("a space outside the project must not match")
	}

	// Membership listing, the counterpart of the filter.
	spaces, err := svc.ProjectSpaces(ctx, fxProject)
	if err != nil {
		t.Fatalf("ProjectSpaces: %v", err)
	}
	if len(spaces) != 1 || spaces[0].Ref != fxSpace {
		t.Fatalf("ProjectSpaces = %+v", spaces)
	}
	if spaces[0].Repo != nil {
		t.Error("ProjectSpaces opened repositories; listing must stay cheap")
	}

	if err := svc.RemoveSpaceFromProject(ctx, fxProject, fxSpace); err != nil {
		t.Fatalf("RemoveSpaceFromProject: %v", err)
	}
	if err := svc.RemoveSpaceFromProject(ctx, fxProject, fxSpace); !errors.Is(err, ErrNotFound) {
		t.Errorf("removing a non-member = %v, want ErrNotFound", err)
	}
	// The space survives its removal from a filter.
	if _, err := svc.OpenSpace(ctx, fxSpace); err != nil {
		t.Errorf("removing a space from a project broke the space: %v", err)
	}

	if _, err := svc.ResolveProject(ctx, core.ProjectRef{Owner: "bigbes", Name: "absent"}); !errors.Is(err, ErrNotFound) {
		t.Errorf("ResolveProject of a missing project = %v, want ErrNotFound", err)
	}
}

// The meta-project unifies what the service owns, with no membership to
// maintain: a space created after it was last looked at is in it already.
func TestMetaProjectListsEverySpaceIncludingNewOnes(t *testing.T) {
	svc, _ := newTestService(t)
	ctx := context.Background()

	if _, err := svc.CreateSpace(ctx, fxSpace); err != nil {
		t.Fatalf("CreateSpace: %v", err)
	}
	spaces, err := svc.ProjectSpaces(ctx, metaRef())
	if err != nil {
		t.Fatalf("ProjectSpaces(meta): %v", err)
	}
	if len(spaces) != 1 {
		t.Fatalf("meta-project lists %d spaces, want 1", len(spaces))
	}

	if _, err := svc.CreateSpace(ctx, core.SpaceRef{Owner: "bigbes", Name: "notes"}); err != nil {
		t.Fatalf("CreateSpace: %v", err)
	}
	spaces, err = svc.ProjectSpaces(ctx, metaRef())
	if err != nil {
		t.Fatalf("ProjectSpaces(meta): %v", err)
	}
	if len(spaces) != 2 {
		t.Fatalf("meta-project lists %d spaces after a new one was created, want 2", len(spaces))
	}
}

// The meta-project has no row, so every operation on a row refuses it — and the
// one that creates rows refuses to make one that would shadow it.
func TestMetaProjectHasNoRow(t *testing.T) {
	svc, _ := newTestService(t)
	ctx := context.Background()

	if _, err := svc.CreateSpace(ctx, fxSpace); err != nil {
		t.Fatalf("CreateSpace: %v", err)
	}
	if _, err := svc.CreateProject(ctx, metaRef()); !errors.Is(err, core.ErrReservedName) {
		t.Errorf("CreateProject(meta) = %v, want ErrReservedName", err)
	}
	if _, err := svc.GetProject(ctx, metaRef()); !errors.Is(err, ErrNotFound) {
		t.Errorf("GetProject(meta) = %v, want ErrNotFound", err)
	}
	if err := svc.DeleteProject(ctx, metaRef()); !errors.Is(err, ErrNotFound) {
		t.Errorf("DeleteProject(meta) = %v, want ErrNotFound", err)
	}
	if err := svc.AddSpaceToProject(ctx, metaRef(), fxSpace); !errors.Is(err, ErrNotFound) {
		t.Errorf("AddSpaceToProject(meta) = %v, want ErrNotFound", err)
	}
	if err := svc.RemoveSpaceFromProject(ctx, metaRef(), fxSpace); !errors.Is(err, ErrNotFound) {
		t.Errorf("RemoveSpaceFromProject(meta) = %v, want ErrNotFound", err)
	}
	// And it is not in the project list, because the list is of rows.
	projects, err := svc.ListProjects(ctx)
	if err != nil {
		t.Fatalf("ListProjects: %v", err)
	}
	if len(projects) != 0 {
		t.Fatalf("ListProjects = %+v, want none", projects)
	}
}

func TestProjectMembershipRequiresBothSides(t *testing.T) {
	svc, _ := newTestService(t)
	ctx := context.Background()

	if _, err := svc.CreateProject(ctx, fxProject); err != nil {
		t.Fatalf("CreateProject: %v", err)
	}
	if err := svc.AddSpaceToProject(ctx, fxProject, fxSpace); !errors.Is(err, ErrNotFound) {
		t.Errorf("adding a space that does not exist = %v, want ErrNotFound", err)
	}
	if _, err := svc.CreateSpace(ctx, fxSpace); err != nil {
		t.Fatalf("CreateSpace: %v", err)
	}
	missing := core.ProjectRef{Owner: "bigbes", Name: "absent"}
	if err := svc.AddSpaceToProject(ctx, missing, fxSpace); !errors.Is(err, ErrNotFound) {
		t.Errorf("adding to a project that does not exist = %v, want ErrNotFound", err)
	}
}

// A space belongs to any number of projects; a project is a filter, not an
// owner, so nothing about the space changes when one contains it.
func TestProjectsForSpace(t *testing.T) {
	svc, _ := newTestService(t)
	ctx := context.Background()

	if _, err := svc.CreateSpace(ctx, fxSpace); err != nil {
		t.Fatalf("CreateSpace: %v", err)
	}
	projects, err := svc.ProjectsForSpace(ctx, fxSpace)
	if err != nil {
		t.Fatalf("ProjectsForSpace: %v", err)
	}
	if len(projects) != 0 {
		t.Fatalf("a space in no project reports %+v", projects)
	}

	for _, name := range []string{"tarantool", "all-docs"} {
		ref := core.ProjectRef{Owner: "bigbes", Name: name}
		if _, err := svc.CreateProject(ctx, ref); err != nil {
			t.Fatalf("CreateProject %s: %v", ref, err)
		}
		if err := svc.AddSpaceToProject(ctx, ref, fxSpace); err != nil {
			t.Fatalf("AddSpaceToProject %s: %v", ref, err)
		}
	}
	projects, err = svc.ProjectsForSpace(ctx, fxSpace)
	if err != nil {
		t.Fatalf("ProjectsForSpace: %v", err)
	}
	if len(projects) != 2 {
		t.Fatalf("projects = %+v, want 2", projects)
	}
	if projects[0].Ref.Name != "all-docs" || projects[1].Ref.Name != "tarantool" {
		t.Errorf("projects are not ordered by name: %+v", projects)
	}

	if _, err := svc.ProjectsForSpace(ctx, core.SpaceRef{Owner: "bigbes", Name: "absent"}); !errors.Is(err, ErrNotFound) {
		t.Errorf("ProjectsForSpace of a missing space = %v, want ErrNotFound", err)
	}
}