| 1 | package wire_test |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/wire" |
| 11 | ) |
| 12 | |
| 13 | // Protocol 33 (assistant attachments spec A6): the Assistant's own turns |
| 14 | // carry files and the earlier files of the conversation. Below 33 (at |
| 15 | // 32, unfinished work, too) an |
| 16 | // Assistant message with files is refused, earlier and earlier_more are |
| 17 | // dropped, and the Coordinator's and a job's frames go as they are. |
| 18 | func TestProtocol33AssistantFiles(t *testing.T) { |
| 19 | if wire.Protocol33 != 33 || wire.ProtocolAssistantFiles != wire.Protocol33 || wire.ProtocolUnfinishedWork != wire.Protocol32 || wire.ProtocolCurrent != wire.ProtocolDesign || wire.Spoken(99) != 38 { |
| 20 | t.Fatalf("assistant files %d, current %d", wire.ProtocolAssistantFiles, wire.ProtocolCurrent) |
| 21 | } |
| 22 | if wire.RunnerKeepFiles != 14*24*time.Hour { |
| 23 | t.Fatalf("keep %v", wire.RunnerKeepFiles) |
| 24 | } |
| 25 | file := wire.AttachmentRef{ID: "at_abcdefgh", Kind: wire.AttachmentFile, Name: "notes.md", MediaType: "text/markdown", Size: 4, SHA256: "aa"} |
| 26 | old := wire.AttachmentRef{ID: "at_olderone", Kind: wire.AttachmentImage, Name: "a.png", MediaType: "image/png", Size: 9, SHA256: "bb"} |
| 27 | msg := func(lane string, files []wire.AttachmentRef, earlier []wire.AttachmentRef, more int) wire.Frame { |
| 28 | 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}) |
| 29 | if err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | return f |
| 33 | } |
| 34 | withFiles := msg(wire.LaneAssistant, []wire.AttachmentRef{file}, nil, 0) |
| 35 | withEarlier := msg(wire.LaneAssistant, nil, []wire.AttachmentRef{old}, 3) |
| 36 | coordinator := msg(wire.LaneCoordinator, []wire.AttachmentRef{file}, nil, 0) |
| 37 | job := msg(wire.JobLane("h_abc"), []wire.AttachmentRef{file}, nil, 0) |
| 38 | |
| 39 | // At 33 everything goes out as it is. |
| 40 | for _, f := range []wire.Frame{withFiles, withEarlier, coordinator, job} { |
| 41 | if got, err := wire.Encode(f, wire.Protocol33); err != nil || !bytes.Equal(got.Data, f.Data) { |
| 42 | t.Fatalf("changed at 33: %v %s", err, got.Data) |
| 43 | } |
| 44 | } |
| 45 | // At 32 (unfinished work, no files) and below, an Assistant message |
| 46 | // with files is refused, earlier files are dropped, and the |
| 47 | // Coordinator's and a job's frames go as they are. |
| 48 | for _, p := range []int{wire.Protocol32, wire.Protocol31} { |
| 49 | if _, err := wire.Encode(withFiles, p); !errors.Is(err, wire.ErrProtocol) { |
| 50 | t.Fatalf("assistant files at %d: %v", p, err) |
| 51 | } |
| 52 | got, err := wire.Encode(withEarlier, p) |
| 53 | if err != nil { |
| 54 | t.Fatal(err) |
| 55 | } |
| 56 | if want := msg(wire.LaneAssistant, nil, nil, 0); !bytes.Equal(got.Data, want.Data) { |
| 57 | t.Fatalf("earlier at %d:\n got %s\nwant %s", p, got.Data, want.Data) |
| 58 | } |
| 59 | for _, f := range []wire.Frame{coordinator, job, msg(wire.LaneAssistant, nil, nil, 0)} { |
| 60 | if got, err := wire.Encode(f, p); err != nil || !bytes.Equal(got.Data, f.Data) { |
| 61 | t.Fatalf("frame changed at %d: %v %s", p, err, got.Data) |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | var fields map[string]json.RawMessage |
| 66 | if err := json.Unmarshal(withEarlier.Data, &fields); err != nil || fields["earlier"] == nil || string(fields["earlier_more"]) != "3" { |
| 67 | t.Fatalf("earlier at 33: %s", withEarlier.Data) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // The snapshot says whether a send with files would be taken. |
| 72 | func TestAssistantSnapshotCanAttach(t *testing.T) { |
| 73 | _, got := jsonKeys(t, wire.AssistantSnapshot{CanAttach: true}) |
| 74 | if string(got["can_attach"]) != "true" { |
| 75 | t.Fatalf("can_attach %s", got["can_attach"]) |
| 76 | } |
| 77 | if _, got = jsonKeys(t, wire.AssistantSnapshot{}); string(got["can_attach"]) != "false" { |
| 78 | t.Fatalf("can_attach when false %s", got["can_attach"]) |
| 79 | } |
| 80 | } |
| 81 | |