package wire_test import ( "bytes" "encoding/json" "errors" "testing" "time" "amadan.net/oficina/equipo/internal/wire" ) // Protocol 33 (assistant attachments spec A6): the Assistant's own turns // carry files and the earlier files of the conversation. Below 33 (at // 32, unfinished work, too) an // Assistant message with files is refused, earlier and earlier_more are // dropped, and the Coordinator's and a job's frames go as they are. func TestProtocol33AssistantFiles(t *testing.T) { if wire.Protocol33 != 33 || wire.ProtocolAssistantFiles != wire.Protocol33 || wire.ProtocolUnfinishedWork != wire.Protocol32 || wire.ProtocolCurrent != wire.ProtocolDesign || wire.Spoken(99) != 38 { t.Fatalf("assistant files %d, current %d", wire.ProtocolAssistantFiles, wire.ProtocolCurrent) } if wire.RunnerKeepFiles != 14*24*time.Hour { t.Fatalf("keep %v", wire.RunnerKeepFiles) } file := wire.AttachmentRef{ID: "at_abcdefgh", Kind: wire.AttachmentFile, Name: "notes.md", MediaType: "text/markdown", Size: 4, SHA256: "aa"} old := wire.AttachmentRef{ID: "at_olderone", Kind: wire.AttachmentImage, Name: "a.png", MediaType: "image/png", Size: 9, SHA256: "bb"} msg := func(lane string, files []wire.AttachmentRef, earlier []wire.AttachmentRef, more int) wire.Frame { f, err := wire.NewFrame("m", wire.FrameMessage, wire.MessageCommand{Person: "p", TurnID: "t", Attempt: 1, Kind: wire.TurnPerson, Text: "hi", PersonText: []string{"hi"}, Tail: []wire.TailMessage{}, Attachments: files, Lane: lane, Earlier: earlier, EarlierMore: more}) if err != nil { t.Fatal(err) } return f } withFiles := msg(wire.LaneAssistant, []wire.AttachmentRef{file}, nil, 0) withEarlier := msg(wire.LaneAssistant, nil, []wire.AttachmentRef{old}, 3) coordinator := msg(wire.LaneCoordinator, []wire.AttachmentRef{file}, nil, 0) job := msg(wire.JobLane("h_abc"), []wire.AttachmentRef{file}, nil, 0) // At 33 everything goes out as it is. for _, f := range []wire.Frame{withFiles, withEarlier, coordinator, job} { if got, err := wire.Encode(f, wire.Protocol33); err != nil || !bytes.Equal(got.Data, f.Data) { t.Fatalf("changed at 33: %v %s", err, got.Data) } } // At 32 (unfinished work, no files) and below, an Assistant message // with files is refused, earlier files are dropped, and the // Coordinator's and a job's frames go as they are. for _, p := range []int{wire.Protocol32, wire.Protocol31} { if _, err := wire.Encode(withFiles, p); !errors.Is(err, wire.ErrProtocol) { t.Fatalf("assistant files at %d: %v", p, err) } got, err := wire.Encode(withEarlier, p) if err != nil { t.Fatal(err) } if want := msg(wire.LaneAssistant, nil, nil, 0); !bytes.Equal(got.Data, want.Data) { t.Fatalf("earlier at %d:\n got %s\nwant %s", p, got.Data, want.Data) } for _, f := range []wire.Frame{coordinator, job, msg(wire.LaneAssistant, nil, nil, 0)} { if got, err := wire.Encode(f, p); err != nil || !bytes.Equal(got.Data, f.Data) { t.Fatalf("frame changed at %d: %v %s", p, err, got.Data) } } } var fields map[string]json.RawMessage if err := json.Unmarshal(withEarlier.Data, &fields); err != nil || fields["earlier"] == nil || string(fields["earlier_more"]) != "3" { t.Fatalf("earlier at 33: %s", withEarlier.Data) } } // The snapshot says whether a send with files would be taken. func TestAssistantSnapshotCanAttach(t *testing.T) { _, got := jsonKeys(t, wire.AssistantSnapshot{CanAttach: true}) if string(got["can_attach"]) != "true" { t.Fatalf("can_attach %s", got["can_attach"]) } if _, got = jsonKeys(t, wire.AssistantSnapshot{}); string(got["can_attach"]) != "false" { t.Fatalf("can_attach when false %s", got["can_attach"]) } }