package thintest import ( "testing" "time" "amadan.net/oficina/equipo/internal/wire" ) // warmCommands counts the warm commands the instance wrote for runners. func (r *rig) warmCommands() int { r.t.Helper() var n int if err := r.inst.DB.Writer().QueryRow("SELECT count(*) FROM runner_commands WHERE kind = ?", wire.FrameWarm).Scan(&n); err != nil { r.t.Fatal(err) } return n } // TestContractAssistantWarmOnOpen: Base reading the Assistant's // snapshot, or opening its stream, asks the person's primary at // protocol 24 to warm their Assistant: one warm command {person}, then // none for five minutes however often Base reads. A primary below 24 is // sent none and none is written for it. func TestContractAssistantWarmOnOpen(t *testing.T) { r := newRig(t) runner := r.dialAt(wire.ProtocolWarm) a := r.client.Assistant() if _, err := a.Snapshot(r.ctx); err != nil { t.Fatal(err) } f, body, err := runner.Expect(r.ctx, wire.FrameWarm) if err != nil { t.Fatal(err) } if w, ok := body.(*wire.WarmCommand); !ok || w.Person != Owner { t.Fatalf("warm %+v", body) } runner.Send(r.ctx, "ack-warm", wire.FrameAck, wire.Ack{ID: f.ID}) // Within the window: more reads and a stream warm nothing; the next // frame the runner sees is the message. if _, err := a.Snapshot(r.ctx); err != nil { t.Fatal(err) } s, err := a.Open(r.ctx, 0) if err != nil { t.Fatal(err) } s.Close() r.assistantTurn(runner, a, "fix my website", "w-1") if n := r.warmCommands(); n != 1 { t.Fatalf("%d warm commands within the window", n) } // Five minutes on (the runner still heard from), the stream's // opening warms again. var rev int64 r.wait("the runner's revision", func(c *Conversation) bool { rev = c.Runner.Revision; return true }) later := time.Now().UTC().Add(warmWindow + time.Second) r.inst.Store.Chat().SetClock(func() time.Time { return later }) t.Cleanup(func() { r.inst.Store.Chat().SetClock(func() time.Time { return time.Now().UTC() }) }) if err := runner.Send(r.ctx, "h-later", wire.FrameHello, primaryAt(wire.Hello{Generation: runner.Welcome.Generation, Repos: []string{}, Connections: []wire.HeldConnection{}, Versions: map[string]string{"runner": "test"}}, wire.ProtocolWarm)); err != nil { t.Fatal(err) } // Offline by the later clock until this hello: a new revision. r.wait("runner heard later", func(c *Conversation) bool { return c.Runner.Online && c.Runner.Revision > rev }) s, err = a.Open(r.ctx, 0) if err != nil { t.Fatal(err) } defer s.Close() if _, body, err := runner.Expect(r.ctx, wire.FrameWarm); err != nil || body.(*wire.WarmCommand).Person != Owner { t.Fatalf("no second warm: %v", err) } // A primary at protocol 23 is sent none. old := newRig(t) oldRunner := old.dialAt(wire.ProtocolGlance) oa := old.client.Assistant() if _, err := oa.Snapshot(old.ctx); err != nil { t.Fatal(err) } old.assistantTurn(oldRunner, oa, "fix my website", "o-1") if n := old.warmCommands(); n != 0 { t.Fatalf("%d warm commands for a runner at 23", n) } } // warmWindow is the instance's warmEvery. const warmWindow = 5 * time.Minute