| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/wire" |
| 10 | ) |
| 11 | |
| 12 | // Task 19 (dynamic specialists slice 1): the task page and the direction |
| 13 | // card show Design's kept captures by option, each from this instance's |
| 14 | // task-file route; with more than one option, Build this takes the |
| 15 | // person's note, in scriptless forms. |
| 16 | |
| 17 | func designOptions() []wire.StepDesignOption { |
| 18 | return []wire.StepDesignOption{ |
| 19 | {Name: "Calm", Captures: []wire.StepCapture{{ID: "tf_calm1", CaptureID: "1-work-1.png", Size: 10, Width: 390, Height: 844}, {ID: "tf_calm2", CaptureID: "1-diff-1.png", Size: 10, Width: 390, Height: 844}}}, |
| 20 | {Name: "Bold", Captures: []wire.StepCapture{{ID: "tf_bold", CaptureID: "2-work-1.png", Size: 10, Width: 1440, Height: 900}}}, |
| 21 | {Name: "Plain", Captures: []wire.StepCapture{}}, |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | // galleryWants are what a rendered gallery of designOptions holds. |
| 26 | func galleryWants(task string) []string { |
| 27 | return []string{ |
| 28 | "Option 1: Calm", "Option 2: Bold", "Option 3: Plain", copyText("work.captures.none"), |
| 29 | `src="/tasks/` + task + `/files/tf_calm1"`, `src="/tasks/` + task + `/files/tf_bold"`, |
| 30 | `alt="Calm, screenshot 2"`, `width="1440" height="900"`, |
| 31 | "<figcaption>" + copyText("work.captures.tree.work") + "</figcaption>", "<figcaption>" + copyText("work.captures.tree.diff") + "</figcaption>", |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | func mustContain(t *testing.T, html string, wants ...string) { |
| 36 | t.Helper() |
| 37 | for _, w := range wants { |
| 38 | if !strings.Contains(html, w) { |
| 39 | t.Errorf("missing %q in:\n%s", w, html) |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | // TestTaskPageShowsDesignCaptures: a done Design attempt's timeline |
| 45 | // entry shows its kept captures by option. |
| 46 | func TestTaskPageShowsDesignCaptures(t *testing.T) { |
| 47 | now := time.Now() |
| 48 | task := wire.Task{ID: "tk1", Creator: "owner@example.com", Stage: wire.StageInReview, Kind: wire.KindExplore, Steps: []wire.Step{{ID: "s1", Specialist: wire.SpecialistEngineering, State: wire.StepDone, Attempt: 1, Pushed: true, CandidateSHA: strings.Repeat("c", 40), FinishedAt: &now, Design: &wire.StepDesign{Attempt: 1, Options: designOptions()}}}} |
| 49 | var item *timelineItem |
| 50 | for _, it := range timelineOf(task, taskLineOf(task, task.Creator)) { |
| 51 | if it.Design != nil { |
| 52 | item = &it |
| 53 | } |
| 54 | } |
| 55 | if item == nil { |
| 56 | t.Fatal("no timeline entry shows the captures") |
| 57 | } |
| 58 | var b bytes.Buffer |
| 59 | if err := pages["work_task"].ExecuteTemplate(&b, "design-gallery", item.Design); err != nil { |
| 60 | t.Fatal(err) |
| 61 | } |
| 62 | mustContain(t, b.String(), galleryWants("tk1")...) |
| 63 | if strings.Count(b.String(), "<section equipo-capture-option") != 3 { |
| 64 | t.Fatalf("options shown: %s", b.String()) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | func directionTask(note bool, opts []wire.StepDesignOption) (wire.Task, *wire.Card) { |
| 69 | card := &wire.Card{ID: "c1", Kind: wire.CardDirection, State: wire.CardOpen, Revision: 2, TaskID: "tk1", Options: []string{wire.ChoiceBuild, wire.ChoiceAgain, wire.ChoiceDrop}} |
| 70 | return wire.Task{ID: "tk1", Creator: "owner@example.com", Stage: wire.StageInReview, Kind: wire.KindExplore, NeedsYou: wire.NeedsDirection, Direction: &wire.TaskDirection{BranchURL: "https://amadan.net/x?branch=y", Options: opts, NoteNeeded: note}}, card |
| 71 | } |
| 72 | |
| 73 | // TestDirectionDecisionShowsOptions: the task page's direction card shows |
| 74 | // the captures; with more than one option its one note field is required |
| 75 | // (Build and Try again both take it) and Drop alone skips it. |
| 76 | func TestDirectionDecisionShowsOptions(t *testing.T) { |
| 77 | task, card := directionTask(true, designOptions()) |
| 78 | d := decisionOf(task, taskLine{Task: task, Card: card}) |
| 79 | if d == nil || d.Gallery == nil || !d.BuildNote || d.Ask != copyText("work.decide.direction_ask_options") { |
| 80 | t.Fatalf("decision %+v", d) |
| 81 | } |
| 82 | var b bytes.Buffer |
| 83 | if err := pages["work_task"].ExecuteTemplate(&b, "decision", map[string]any{"Task": taskLine{Task: task, Decision: d}, "Head": false, "From": "task"}); err != nil { |
| 84 | t.Fatal(err) |
| 85 | } |
| 86 | html := b.String() |
| 87 | mustContain(t, html, galleryWants("tk1")...) |
| 88 | mustContain(t, html, `name="note" required`, copyText("chat.card.direction_note_options"), `value="drop" data-merge-choice="drop" formnovalidate`) |
| 89 | if strings.Contains(html, `value="build" data-merge-choice="build" formnovalidate`) || strings.Contains(html, `value="again" data-merge-choice="again" formnovalidate`) { |
| 90 | t.Fatalf("Build or Try again skips the note: %s", html) |
| 91 | } |
| 92 | |
| 93 | one, card := directionTask(false, designOptions()[:1]) |
| 94 | d = decisionOf(one, taskLine{Task: one, Card: card}) |
| 95 | b.Reset() |
| 96 | if err := pages["work_task"].ExecuteTemplate(&b, "decision", map[string]any{"Task": taskLine{Task: one, Decision: d}, "Head": false, "From": "task"}); err != nil { |
| 97 | t.Fatal(err) |
| 98 | } |
| 99 | if html := b.String(); strings.Contains(html, `name="note" required`) || strings.Contains(html, "formnovalidate") || !strings.Contains(html, "Option 1: Calm") || d.Ask != copyText("work.decide.direction_ask") { |
| 100 | t.Fatalf("one option: %s", html) |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // TestConversationDirectionCardBuildNote: the conversation's direction |
| 105 | // card shows the captures; with more than one option Build this is a |
| 106 | // form of its own whose note is required, and the card has no bare |
| 107 | // Build button. |
| 108 | func TestConversationDirectionCardBuildNote(t *testing.T) { |
| 109 | render := func(task wire.Task, card *wire.Card) string { |
| 110 | t.Helper() |
| 111 | v := cardView(*card, nil, map[string]wire.Task{task.ID: task}) |
| 112 | var b bytes.Buffer |
| 113 | if err := pages["chat"].ExecuteTemplate(&b, "chat-card", v); err != nil { |
| 114 | t.Fatal(err) |
| 115 | } |
| 116 | return b.String() |
| 117 | } |
| 118 | html := render(directionTask(true, designOptions())) |
| 119 | mustContain(t, html, galleryWants("tk1")...) |
| 120 | mustContain(t, html, "data-direction-build", `<input type="hidden" name="choice" value="build">`, `<textarea name="note" required maxlength="1000" rows="1" aria-label="`+copyText("chat.card.build_note")+`"`) |
| 121 | if strings.Contains(html, `name="choice" value="build" type="submit"`) { |
| 122 | t.Fatalf("a Build button without the note: %s", html) |
| 123 | } |
| 124 | html = render(directionTask(false, designOptions()[:1])) |
| 125 | mustContain(t, html, `name="choice" value="build" type="submit"`, "Option 1: Calm") |
| 126 | if strings.Contains(html, "data-direction-build") { |
| 127 | t.Fatalf("one option asks for a note: %s", html) |
| 128 | } |
| 129 | // No Design: the card as it was. |
| 130 | html = render(directionTask(false, nil)) |
| 131 | if strings.Contains(html, "equipo-captures") || strings.Contains(html, "data-direction-build") { |
| 132 | t.Fatalf("no options: %s", html) |
| 133 | } |
| 134 | } |
| 135 | |