| 1 | package wire_test |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "errors" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | "unicode/utf8" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/wire" |
| 11 | ) |
| 12 | |
| 13 | // A glance is the Assistant's instant first line: protocol 23. Encode |
| 14 | // refuses the frame below it, so an older instance never sees one, and |
| 15 | // the frame is never durable (it is sent once, like a delta). |
| 16 | func TestEncodeGlanceBelow23(t *testing.T) { |
| 17 | if wire.ProtocolGlance != 23 || wire.ProtocolCurrent < wire.ProtocolGlance { |
| 18 | t.Fatalf("protocols %d %d", wire.ProtocolGlance, wire.ProtocolCurrent) |
| 19 | } |
| 20 | f, err := wire.NewFrame("g1", wire.FrameGlance, wire.GlanceReport{TurnID: "t_1", Attempt: 1, Text: "Looking into Tito"}) |
| 21 | if err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | if f.Durable() { |
| 25 | t.Fatal("a glance is durable") |
| 26 | } |
| 27 | body, err := f.Decode() |
| 28 | if g, ok := body.(*wire.GlanceReport); err != nil || !ok || g.TurnID != "t_1" || g.Attempt != 1 || g.Text != "Looking into Tito" { |
| 29 | t.Fatalf("decode %+v %v", body, err) |
| 30 | } |
| 31 | if _, err := (wire.Frame{ID: "g2", Type: wire.FrameGlance, Data: json.RawMessage(`{"turn_id":"t","attempt":1,"text":"x","more":1}`)}).Decode(); err == nil { |
| 32 | t.Fatal("an unknown glance field decoded") |
| 33 | } |
| 34 | for _, p := range []int{wire.Protocol1, wire.Protocol2, wire.ProtocolAssistant, wire.ProtocolJobs} { |
| 35 | if _, err := wire.Encode(f, p); !errors.Is(err, wire.ErrProtocol) { |
| 36 | t.Fatalf("a glance encoded at protocol %d: %v", p, err) |
| 37 | } |
| 38 | } |
| 39 | if got, err := wire.Encode(f, wire.ProtocolGlance); err != nil || string(got.Data) != string(f.Data) { |
| 40 | t.Fatalf("at 23 %s %v", got.Data, err) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | // GlanceText is the one guard both ends apply: one plain line of at most |
| 45 | // eight words and 60 characters, nothing that names the machinery, and |
| 46 | // nothing at all when nothing is left. |
| 47 | func TestGlanceText(t *testing.T) { |
| 48 | for _, c := range []struct{ in, want string }{ |
| 49 | {"Looking into Tito", "Looking into Tito"}, |
| 50 | {" Hi Paul… ", "Hi Paul…"}, |
| 51 | {"**Starting** on your `website`", "Starting on your website"}, |
| 52 | {"# Looking\ninto it\r\nnow", "Looking into it now"}, |
| 53 | {"- Checking your calendar", "Checking your calendar"}, |
| 54 | {"\"Reading your note\"", "Reading your note"}, |
| 55 | {"[Looking](https://x.example) at it", "Looking at it"}, |
| 56 | {"one two three four five six seven eight nine ten", "one two three four five six seven eight"}, |
| 57 | {"Considering extraordinarily comprehensive internationalisation requirements thoroughly", "Considering extraordinarily comprehensive"}, |
| 58 | {"Checking\tyour\x00 invoices", "Checking your invoices"}, |
| 59 | {"Reviewing your draft for Tito", "Reviewing your draft for Tito"}, |
| 60 | {"Checking your calendar for Friday", "Checking your calendar for Friday"}, |
| 61 | {"Reading the summary for Tito", "Reading the summary for Tito"}, |
| 62 | {"Planning the pre-launch party", "Planning the pre-launch party"}, |
| 63 | } { |
| 64 | got, ok := wire.GlanceText(c.in) |
| 65 | if !ok || got != c.want { |
| 66 | t.Fatalf("GlanceText(%q) = %q %v, want %q", c.in, got, ok, c.want) |
| 67 | } |
| 68 | if len(strings.Fields(got)) > wire.MaxGlanceWords || utf8.RuneCountInString(got) > wire.MaxGlanceChars { |
| 69 | t.Fatalf("%q is over the bounds", got) |
| 70 | } |
| 71 | if again, ok := wire.GlanceText(got); !ok || again != got { |
| 72 | t.Fatalf("a clean line changed: %q -> %q", got, again) |
| 73 | } |
| 74 | } |
| 75 | for _, in := range []string{"", " \n ", "**", "Asking the team now", "Starting a TASK for you", "Creating a branch", "Merge coming up", "Handing it to the coordinator", "Your runner is busy", "Equipo is on it", "Checking repository abc", "Opening the repo now", "Opening your repos", "Running the checks", "One check left", "Sending it for review", "Deploying your site", "Asking a specialist", "Looking at 1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f9a0b", "Looking at abc1234 now", "Opening deadbeefcafe0", "Opening https://tito.io/admin", "Reading src/main.go", "Opening www.tito.io", "Looking at C:\\work", "Running pre-checks now", "Opening repo:example", "Looking at deadbeef", "Starting the re-review", "Opening the repo's page", "Sending it_for_review", "Supercalifragilisticexpialidociousandmoreandmoreandmoreandmorewords"} { |
| 76 | if got, ok := wire.GlanceText(in); ok { |
| 77 | t.Fatalf("GlanceText(%q) kept %q", in, got) |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |