Repository navigation
Expand file tree
/
Copy pathcell_branch_test.go
More file actions
163 lines (147 loc) · 5.11 KB
/
Copy pathcell_branch_test.go
File metadata and controls
163 lines (147 loc) · 5.11 KB
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
package main
import (
"bytes"
"context"
"errors"
"os"
"path/filepath"
"strings"
"testing"
"github.com/Agent-Field/codeaf/internal/blobstore"
"github.com/Agent-Field/codeaf/internal/cell"
"github.com/Agent-Field/codeaf/internal/cellsync"
"github.com/Agent-Field/codeaf/internal/directory"
"github.com/Agent-Field/codeaf/internal/directory/directorytest"
)
const (
parentID = "01J0000000000000000000000A"
branchID = "01J0000000000000000000000B"
)
// branchRig is device A driving a chat whose death branch device B made.
type branchRig struct {
t *testing.T
clock *directorytest.FakeClock
dir *directory.Memory
store *blobstore.Memory
engA *cellsync.FakeEngine
parent cell.Cell
drv *cellsync.Driving
door branchDoor
}
type fixedDriver struct{ d *cellsync.Driving }
func (f fixedDriver) Driving(context.Context, cell.Cell) (*cellsync.Driving, error) { return f.d, nil }
func newBranchRig(t *testing.T) *branchRig {
t.Helper()
ctx := context.Background()
r := &branchRig{t: t, clock: directorytest.NewFakeClock(), store: blobstore.NewMemory(), engA: cellsync.NewFakeEngine(t.TempDir())}
r.dir = directory.NewMemory(r.clock.Now)
dirA := r.dir.For("dev_a")
r.parent = cell.Cell{ID: parentID, Root: t.TempDir()}
if err := os.WriteFile(filepath.Join(r.parent.Root, "parent.txt"), []byte("parent"), 0o600); err != nil {
t.Fatal(err)
}
r.drv = &cellsync.Driving{Cell: r.parent}
pub := &cellsync.Publisher{Engine: r.engA, Store: r.store, Dir: dirA}
head := r.engA.Seal(r.parent, map[string]string{"parent.txt": "parent"})
if err := pub.Publish(ctx, r.drv, head, cellsync.PublishInfo{Class: "chat", Size: 1}); err != nil {
t.Fatal(err)
}
r.makeBranch(map[string]string{"branch.txt": "branch"})
r.door = branchDoor{Dir: dirA, Driver: fixedDriver{r.drv}}
return r
}
// makeBranch is device B's death branch: its objects reach the store, then the record.
func (r *branchRig) makeBranch(files map[string]string) {
r.t.Helper()
ctx := context.Background()
engB := cellsync.NewFakeEngine(r.t.TempDir())
bc := cell.Cell{ID: branchID, Root: r.t.TempDir()}
head := engB.Seal(bc, files)
ex, err := engB.Export(ctx, bc, head)
if err != nil {
r.t.Fatal(err)
}
for _, f := range ex.Frames {
raw, _ := os.ReadFile(f.Path)
if _, err := r.store.PutFrame(ctx, raw); err != nil {
r.t.Fatal(err)
}
}
_, err = r.dir.For("dev_b").Create(ctx, branchID, directory.CellInit{Head: head, Class: "chat", ParentCell: parentID, OrphanTurns: 1})
if err != nil {
r.t.Fatal(err)
}
}
func (r *branchRig) rec(id string) directory.Cell {
r.t.Helper()
v, err := r.dir.For("dev_a").Cell(context.Background(), id)
if err != nil {
r.t.Fatal(err)
}
return v.Cell
}
// loseLease lets A's lease lapse and gives it to device C.
func (r *branchRig) loseLease() {
r.t.Helper()
r.clock.Advance(directory.LeaseTTL + 1)
if _, err := r.dir.For("dev_c").Acquire(context.Background(), parentID, directory.AcquireOpts{}); err != nil {
r.t.Fatal(err)
}
}
func TestCellDiscard(t *testing.T) {
t.Run("discard archives", func(t *testing.T) {
r := newBranchRig(t)
before := r.rec(parentID).Head
if err := r.door.discard(context.Background(), r.parent, branchID, &bytes.Buffer{}); err != nil {
t.Fatal(err)
}
if !r.rec(branchID).Archived || r.rec(parentID).Head != before {
t.Fatal("discard must archive the branch and leave the parent alone")
}
})
t.Run("discard does not run without the parent's lease", func(t *testing.T) {
r := newBranchRig(t)
r.loseLease()
before := r.rec(parentID).Head
if err := r.door.discard(context.Background(), r.parent, branchID, &bytes.Buffer{}); !errors.Is(err, directory.ErrFenceStale) {
t.Errorf("discard: err = %v, want ErrFenceStale", err)
}
if r.rec(branchID).Archived || r.rec(parentID).Head != before {
t.Fatal("a device without the lease changed something")
}
})
t.Run("a stranger's branch is refused", func(t *testing.T) {
r := newBranchRig(t)
if _, err := r.dir.For("dev_b").Create(context.Background(), "other", directory.CellInit{Head: "h", Class: "chat"}); err != nil {
t.Fatal(err)
}
if err := r.door.discard(context.Background(), r.parent, "other", &bytes.Buffer{}); err == nil {
t.Fatal("discarded a cell that is not a branch of this chat")
}
})
}
func TestCellDiscardNeedsSyncSetUp(t *testing.T) {
t.Setenv("CODEAF_SYNC_URL", "off")
if err := runCellIn([]string{"discard", branchID, mustCellRoot(t)}, &bytes.Buffer{}, t.TempDir()); err == nil {
t.Error("discard ran on a device with no sync")
}
}
// TestThereIsNoCellMerge pins that the verb is absent, not broken: it is not in
// the usage line and asking for it is the usage line.
func TestThereIsNoCellMerge(t *testing.T) {
if strings.Contains(cellUsage, "merge") {
t.Errorf("the usage line still offers merge: %s", cellUsage)
}
err := runCellIn([]string{"merge", branchID, mustCellRoot(t)}, &bytes.Buffer{}, t.TempDir())
if err == nil || err.Error() != cellUsage {
t.Errorf("cell merge answered %v, want the usage line", err)
}
}
func mustCellRoot(t *testing.T) string {
t.Helper()
c, err := cell.CreateIn(t.TempDir(), cell.Options{Class: cell.FilesOnly})
if err != nil {
t.Fatal(err)
}
return c.Root
}