Repository navigation
Expand file tree
/
Copy pathchatv3_host_teams_test.go
More file actions
278 lines (266 loc) · 11.2 KB
/
Copy pathchatv3_host_teams_test.go
File metadata and controls
278 lines (266 loc) · 11.2 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
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
package main
import (
"errors"
"github.com/Agent-Field/codeaf/internal/config"
"sync/atomic"
"testing"
"github.com/Agent-Field/codeaf/internal/remote"
teamstore "github.com/Agent-Field/codeaf/internal/teams"
)
// A WRITE THAT MEETS ANOTHER WRITER IS MADE AGAIN ON TOP OF IT. The window made
// its change to the list it held; the far session set a manager in between, so
// the engine answers Stale. The seam reads the file again, makes the change
// again on the fresh list, and the second write carries both: the rename and
// the manager nobody here saw.
func TestHostTeamsRetriesAStaleWriteOnTopOfTheOtherWriter(t *testing.T) {
far := []teamstore.Team{{ID: "0a0a0a0a0a0a", Name: "harbor", Members: []teamstore.Member{{Key: "k1"}}}}
stamp, reads := "1.1", 0
var wrote [][]teamstore.Team
h := &hostTeams{
read: func(since string, _ []float64) (remote.TeamsReading, error) {
reads++
if since == stamp {
return remote.TeamsReading{Stamp: stamp, Same: true}, nil
}
return remote.TeamsReading{Stamp: stamp, Teams: cloneTeams(far)}, nil
},
write: func(base string, teams []teamstore.Team) (remote.TeamsReading, error) {
wrote = append(wrote, cloneTeams(teams))
if base != stamp {
return remote.TeamsReading{Stamp: stamp, Stale: true}, nil
}
far, stamp = cloneTeams(teams), "2.2"
return remote.TeamsReading{Stamp: stamp, Teams: cloneTeams(far)}, nil
},
}
if _, _, known := h.load(nil); known {
t.Fatal("a seam that has read nothing says it holds something")
}
if _, _, _, err := h.readSince("", nil); err != nil {
t.Fatal(err)
}
// The far session writes after the window read.
far[0].Manager, stamp = "k1", "1.5"
calls := 0
teams, at, err := h.update(func(f *teamstore.File) error {
calls++
f.Teams[0].Name = "dock"
return nil
})
if err != nil {
t.Fatal(err)
}
if calls != 2 || len(wrote) != 2 || reads != 2 {
t.Fatalf("the change was made %d times, written %d times, read %d times", calls, len(wrote), reads)
}
if at != "2.2" || teams[0].Name != "dock" || teams[0].Manager != "k1" {
t.Fatalf("the second write lost something: %+v at %q", teams, at)
}
if held, heldAt, known := h.load(nil); !known || heldAt != "2.2" || held[0].Manager != "k1" {
t.Fatalf("the seam holds %+v at %q", held, heldAt)
}
}
// A WRITER THAT NEVER STOPS WINS, AND THE WINDOW IS TOLD. Three stale answers
// in a row are an error the surface says, not a write made over the top.
func TestHostTeamsGivesUpAfterThreeStaleWrites(t *testing.T) {
writes := 0
h := &hostTeams{
read: func(string, []float64) (remote.TeamsReading, error) {
return remote.TeamsReading{Stamp: "1.1"}, nil
},
write: func(string, []teamstore.Team) (remote.TeamsReading, error) {
writes++
return remote.TeamsReading{Stamp: "9.9", Stale: true}, nil
},
}
if _, _, err := h.update(func(*teamstore.File) error { return nil }); !errors.Is(err, errHostTeamsBusy) || writes != hostTeamsTries {
t.Fatalf("after %d stale writes: %v", writes, err)
}
}
// AN ENGINE WITHOUT THE TEAMS DOORS GETS NO SEAM. The welcome of an older
// engine carries no Teams, and the door hands the surface the zero seam, which
// over --host is teams off; it never hands one onto this laptop's profile.
func TestAnOlderEngineGetsNoTeamsSeam(t *testing.T) {
loop, err := remote.Loopback(remote.Hello{Version: remote.Version}, remote.Options{Boot: func(remote.Hello) (*remote.Engine, error) {
return &remote.Engine{Agent: &quietAgent{}, ProfileDir: t.TempDir()}, nil
}})
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = loop.Close() })
far := hostFar{client: loop.Client}
welcome := loop.Client.Welcome()
if seam := hostTeamsSeam(far, welcome); seam.Load == nil || seam.Update == nil || seam.Traffic == nil || seam.ReadSince == nil {
t.Fatal("an engine with the teams doors got no seam")
}
welcome.Teams = false
if seam := hostTeamsSeam(far, welcome); seam.Load != nil || seam.Update != nil {
t.Fatal("an older engine got a teams seam")
}
if seam := hostTeamsSeam(hostFar{}, remote.Welcome{Teams: true}); seam.Load != nil {
t.Fatal("no connection got a teams seam")
}
}
// THE DELEGATION DOORS CROSS --host, AND AN ENGINE WITHOUT THEM GETS A SEAM
// WITHOUT THEM. This build's engine says Delegation: the seam carries every
// door, and a packet raised and a decision made through it land in the
// ENGINE's profile, not this machine's. An engine that has the teams doors
// and not these (Teams true, Delegation false) gets the teams seam with the
// delegation doors nil, which the surface reads as "not over this
// connection"; it is never handed doors onto this laptop's files.
func TestTheDelegationDoorsCrossHostOnlyWhenTheEngineSaysSo(t *testing.T) {
far := t.TempDir()
loop, err := remote.Loopback(remote.Hello{Version: remote.Version}, remote.Options{Boot: func(remote.Hello) (*remote.Engine, error) {
return &remote.Engine{Agent: &quietAgent{}, ProfileDir: far}, nil
}})
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = loop.Close() })
if err := teamstore.Save(far, []teamstore.Team{{ID: "0a0a0a0a0a0a", Name: "harbor", Manager: "hm",
Members: []teamstore.Member{{Key: "hm", Handle: "boss"}, {Key: "k1", Handle: "web"}}}}); err != nil {
t.Fatal(err)
}
welcome := loop.Client.Welcome()
seam := hostTeamsSeam(hostFar{client: loop.Client}, welcome)
if seam.Defaults == nil || seam.Packets == nil || seam.Raise == nil || seam.Decide == nil ||
seam.Escalate == nil || seam.Spend == nil || seam.Delete == nil {
t.Fatal("an engine with the delegation doors got a seam without them")
}
p, err := seam.Raise(teamstore.Packet{Team: "0a0a0a0a0a0a", Kind: teamstore.PacketQuestion,
RaisedBy: "web", Question: "which port?"})
if err != nil {
t.Fatal(err)
}
open, stamp, same, err := seam.Packets("0a0a0a0a0a0a", "")
if err != nil || same || len(open) != 1 || open[0].ID != p.ID {
t.Fatalf("packets over the seam: %+v %v %v", open, same, err)
}
if _, _, same, _ := seam.Packets("0a0a0a0a0a0a", stamp); !same {
t.Fatal("a quiet packet read over the seam was not same")
}
if _, err := seam.Decide(p.ID, "boss", "8080", "the default"); err != nil {
t.Fatal(err)
}
if got, err := teamstore.PacketByID(far, p.ID); err != nil || got.Decision != "8080" {
t.Fatalf("the decision is not in the engine's profile: %+v %v", got, err)
}
welcome.Delegation = false
older := hostTeamsSeam(hostFar{client: loop.Client}, welcome)
if older.Load == nil || older.Update == nil {
t.Fatal("an engine with the teams doors and not the delegation doors lost its teams seam")
}
if older.Defaults != nil || older.Packets != nil || older.Raise != nil || older.Decide != nil ||
older.Escalate != nil || older.Spend != nil || older.Delete != nil {
t.Fatal("an engine without the delegation doors was handed them")
}
}
// THE WRAP-UP'S TWO DOORS CROSS --host WHEN THE ENGINE SAYS SO (DESIGN.md
// 8.8): `Wrap up first` lands the one request line in the ENGINE's Traffic,
// and accepting a closing report closes the team in the engine's profile. An
// engine that does not say WrapUp hands no such doors, and the close card
// offers `Close now` only.
func TestTheWrapUpDoorsCrossHostOnlyWhenTheEngineSaysSo(t *testing.T) {
far := t.TempDir()
loop, err := remote.Loopback(remote.Hello{Version: remote.Version}, remote.Options{Boot: func(remote.Hello) (*remote.Engine, error) {
return &remote.Engine{Agent: &quietAgent{}, ProfileDir: far}, nil
}})
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = loop.Close() })
const harbor = "0a0a0a0a0a0a"
if err := teamstore.Save(far, []teamstore.Team{{ID: harbor, Name: "harbor", Manager: "hm",
Members: []teamstore.Member{{Key: "hm", Handle: "boss"}, {Key: "k1", Handle: "web"}}}}); err != nil {
t.Fatal(err)
}
welcome := loop.Client.Welcome()
seam := hostTeamsSeam(hostFar{client: loop.Client}, welcome)
if seam.WrapUp == nil || seam.AcceptClosing == nil {
t.Fatal("an engine with the wrap-up doors got a seam without them")
}
if err := seam.WrapUp(harbor, ""); err != nil {
t.Fatal(err)
}
log, err := teamstore.ReadTraffic(far, harbor, "", 10)
if err != nil || len(log) != 1 || !teamstore.IsWrapUp(log[0]) {
t.Fatalf("the engine's Traffic after a wrap-up: %+v %v", log, err)
}
p, err := seam.Raise(teamstore.Packet{Team: teamstore.Person, Origin: harbor, Kind: teamstore.PacketClosing,
RaisedBy: teamstore.FromManager, Question: "close harbor?",
Options: []teamstore.Option{{ID: teamstore.OptionClose, Label: "Close", Consequence: "the team closes"},
{ID: teamstore.OptionKeepGoing, Label: "Keep going", Consequence: "the team goes on"}},
Report: &teamstore.ClosingReport{Done: "the parser"}})
if err != nil {
t.Fatal(err)
}
if _, err := seam.Decide(p.ID, teamstore.Person, teamstore.OptionClose, ""); err != nil {
t.Fatal(err)
}
if closed, err := seam.AcceptClosing(p.ID); err != nil || !closed {
t.Fatalf("accepting the report over --host: %v %v", closed, err)
}
f, err := teamstore.Load(far)
if err != nil {
t.Fatal(err)
}
if got, ok := f.Team(harbor); !ok || !got.Closed() {
t.Fatalf("the engine's harbor is not closed: %+v", got)
}
welcome.WrapUp = false
older := hostTeamsSeam(hostFar{client: loop.Client}, welcome)
if older.Decide == nil || older.WrapUp != nil || older.AcceptClosing != nil {
t.Fatal("an engine without the wrap-up doors was handed them, or lost the others")
}
}
// Contract 6.2: The host wire never lends the laptop a closed team's packet files.
func TestHostTeamsKeepsClosedHistoryOffTheWire(t *testing.T) {
loop, err := remote.Loopback(remote.Hello{Version: remote.Version}, remote.Options{Boot: func(remote.Hello) (*remote.Engine, error) {
return &remote.Engine{Agent: &quietAgent{}, ProfileDir: t.TempDir()}, nil
}})
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = loop.Close() })
welcome := loop.Client.Welcome()
if !welcome.Teams || !welcome.Delegation || !welcome.WrapUp {
t.Fatalf("the engine welcome has no complete teams road: %+v", welcome)
}
if seam := hostTeamsSeam(hostFar{client: loop.Client}, welcome); seam.History != nil {
t.Fatal("the host seam offered to read closed history from this laptop")
}
}
// R4/F1/F7 carries effective sharing from the actual engine response through the hosted seam.
func TestHostedTeamsSeamReadsEngineEffectiveSharing(t *testing.T) {
dir := t.TempDir()
var paired atomic.Bool
loop, err := remote.Loopback(remote.Hello{Version: remote.Version}, remote.Options{Boot: func(remote.Hello) (*remote.Engine, error) {
return &remote.Engine{Agent: &quietAgent{}, ProfileDir: dir, TeamsPaired: paired.Load}, nil
}})
if err != nil {
t.Fatal(err)
}
defer loop.Close()
seam := hostTeamsSeam(hostFar{client: loop.Client}, loop.Client.Welcome())
if seam.Sharing == nil {
t.Fatal("hosted seam has no engine sharing read")
}
if _, _, _, err := seam.ReadSince("", nil); err != nil {
t.Fatal(err)
}
row, _ := config.NewSettings(config.SettingsOptions{ProfileDir: dir}).Row(config.KeyTeamsShare)
for _, tc := range []struct {
paired bool
choice string
want bool
}{{true, "on", true}, {false, "on", false}, {true, "off", false}, {true, "on", true}} {
paired.Store(tc.paired)
if err := row.Apply(tc.choice); err != nil {
t.Fatal(err)
}
got, err := seam.Sharing()
if err != nil || got != tc.want {
t.Fatalf("engine paired=%v preference=%s: sharing=%v err=%v", tc.paired, tc.choice, got, err)
}
}
}