oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package equipo
2
3import (
4 "bytes"
5 "regexp"
6 "strings"
7 "testing"
8 "time"
9
10 "amadan.net/oficina/equipo/internal/equipo/setup"
11 "amadan.net/oficina/equipo/internal/wire"
12)
13
14// machinesFixture: the person's online Linux primary, their Mac that
15// went quiet, their Mac enrolled at 12:53 UTC that never connected
16// (staging's), a machine being removed, and two of other people's.
17func machinesFixture(now time.Time) []machineRecord {
18 enrolled := time.Date(2026, 11, 11, 12, 53, 0, 0, time.UTC)
19 removing := now
20 return []machineRecord{
21 {Runner: Runner{ID: "r-desk", Person: "me@example.com", Label: "Desk", Role: setup.RolePrimary, Revision: 3, MachineKeyHash: "abc123", Report: &setup.RunnerReport{Platform: "linux/amd64", Sandbox: wire.SandboxBubblewrap}},
22 Facts: machineFacts{Session: true, Connected: true, SeenAt: now.Add(-10 * time.Second), Protocol: 2, Versions: map[string]string{"runner": "v0.9.1"}, Tags: []string{"linux", "go"}, EnrolledAt: &enrolled, EnrolledBy: "me@example.com"}},
23 {Runner: Runner{ID: "r-studio", Person: "me@example.com", Label: "Headless runner", Role: setup.RoleWorker, Revision: 2, Report: &setup.RunnerReport{Platform: "darwin/arm64"}},
24 Facts: machineFacts{Session: true, Connected: true, SeenAt: now.Add(-3 * time.Hour), Protocol: 2, Label: "Tito Studio"}},
25 {Runner: Runner{ID: "r-air", Person: "me@example.com", Label: "Paul's MacBook Air", Role: setup.RoleWorker, Revision: 1},
26 Facts: machineFacts{EnrolledAt: &enrolled, EnrolledBy: "me@example.com"}},
27 {Runner: Runner{ID: "r-old", Person: "me@example.com", Label: "Old box", Role: setup.RoleWorker, Revision: 4, RemovingAt: &removing},
28 Facts: machineFacts{Session: true, SeenAt: now.Add(-time.Minute)}},
29 {Runner: Runner{ID: "r-ana", Person: "ana@example.com", Label: "Ana's desk", Role: setup.RolePrimary, Revision: 1}},
30 {Runner: Runner{ID: "r-bo", Person: "bo@example.com", Label: "Bo's laptop", Role: setup.RolePrimary, Revision: 1}},
31 }
32}
33
34func machinesHTML(t *testing.T, v machinesView) string {
35 t.Helper()
36 var buf bytes.Buffer
37 if err := pages["runners"].ExecuteTemplate(&buf, "content", pageData{Machines: v, Address: "me@example.com"}); err != nil {
38 t.Fatal(err)
39 }
40 return buf.String()
41}
42
43// TestMachinesList: Machines in Settings (Paul, 2026-11-11). The
44// person's own machines are compact rows (name, state, role, platform)
45// whose details open on a click; other people's are folded, closed,
46// with their count; Revoke and Remove now are quiet and ask once more;
47// a machine that never connected says so with what to check.
48func TestMachinesList(t *testing.T) {
49 now := time.Date(2026, 11, 11, 15, 0, 0, 0, time.UTC)
50 v := machinesPage(machinesFixture(now), "me@example.com", now, false, "")
51 if len(v.Mine) != 4 || len(v.Others) != 2 || len(v.New) != 0 {
52 t.Fatalf("mine %d others %d new %d", len(v.Mine), len(v.Others), len(v.New))
53 }
54 state := map[string]string{}
55 for _, m := range append(append([]machineRow{}, v.Mine...), v.Others...) {
56 state[m.ID] = m.State
57 }
58 for id, want := range map[string]string{"r-desk": "online", "r-studio": "offline", "r-air": "never", "r-old": "removing", "r-ana": "never"} {
59 if state[id] != want {
60 t.Fatalf("%s: state %q, want %q", id, state[id], want)
61 }
62 }
63 html := machinesHTML(t, v)
64 row := func(id string) string {
65 t.Helper()
66 m := regexp.MustCompile(`(?s)<details equipo-mach data-machine="` + id + `".*?</summary>`).FindString(html)
67 if m == "" {
68 t.Fatalf("no row %s:\n%s", id, html)
69 }
70 return m
71 }
72 // The row: name, state in words, role, platform; the details only
73 // behind the click.
74 for id, wants := range map[string][]string{
75 "r-desk": {">Desk<", `data-s="online" data-machine-state>Online<`, ">Main runner<", ">Linux<"},
76 "r-studio": {">Tito Studio<", `data-s="offline" data-machine-state>Offline<`, "Last seen 3 h ago", ">Runs checks<", ">macOS<"},
77 "r-air": {">Paul&#39;s MacBook Air<", `data-s="never" data-machine-state>Never connected<`, "data-never-hint>This machine was added but has never connected. Check that the runner is running on it.<"},
78 "r-old": {`data-machine-state>Removing<`},
79 } {
80 r := row(id)
81 for _, want := range wants {
82 if !strings.Contains(r, want) {
83 t.Fatalf("%s row lacks %q: %s", id, want, r)
84 }
85 }
86 if strings.Contains(r, " open") || strings.Contains(r, "Key fingerprint") || strings.Contains(r, "/revoke") {
87 t.Fatalf("%s row shows its details unasked: %s", id, r)
88 }
89 }
90 if strings.Contains(row("r-desk"), "data-never-hint") {
91 t.Fatal("an online machine reads never connected")
92 }
93 // The details.
94 for _, want := range []string{"<dt>Version</dt><dd>v0.9.1</dd>", "<dt>Protocol</dt><dd>2</dd>", "<dt>Tags</dt><dd>linux, go</dd>", "<dt>Added</dt><dd>11 Nov 2026, 12:53 UTC</dd>", "<dt>Added by</dt><dd>me@example.com</dd>", "abc123", `data-sandbox="bubblewrap"`, "Removing: waiting for it to stop its work"} {
95 if !strings.Contains(html, want) {
96 t.Fatalf("details lack %q", want)
97 }
98 }
99 // Quiet destructive actions, each behind a second click, posting the
100 // same forms as ever.
101 for _, want := range []string{
102 `<details equipo-mach-confirm><summary rst-btn data-quiet>Revoke machine</summary>`,
103 `action="/runners/r-desk/revoke"`, `<input type="hidden" name="revision" value="3">`, `data-revoke>Yes, revoke it<`,
104 `<details equipo-mach-confirm><summary rst-btn data-quiet>Remove now</summary>`,
105 `action="/runners/r-desk/remove-now"`, `data-danger data-remove-now>Yes, remove it now<`,
106 } {
107 if !strings.Contains(html, want) {
108 t.Fatalf("actions lack %q", want)
109 }
110 }
111 if strings.Contains(html, `rst-btn="primary"`) && strings.Contains(html, "revoke\" equipo") {
112 t.Fatal("a destructive action is a primary button")
113 }
114 if strings.Contains(regexp.MustCompile(`(?s)data-machine="r-old".*?</details>\n</div>\n</details>`).FindString(html), "/revoke") {
115 t.Fatal("a machine being removed offers Revoke again")
116 }
117 // Other people's machines: folded, closed, counted, each with whose.
118 others := regexp.MustCompile(`<details equipo-mach-others data-other-machines><summary><span>Other people&#39;s machines</span> <span equipo-conn-n>2</span>`)
119 if !others.MatchString(html) || strings.Contains(html, "data-other-machines open") {
120 t.Fatalf("others not folded with a count:\n%s", html)
121 }
122 if !strings.Contains(row("r-ana"), "<span equipo-conn-account>ana@example.com</span>") {
123 t.Fatal("another person's row does not say whose")
124 }
125 if strings.Index(html, "data-my-machines") > strings.Index(html, "data-other-machines") {
126 t.Fatal("other people's machines come before the person's own")
127 }
128 // A member sees only their own: no fold.
129 if html := machinesHTML(t, machinesPage(machinesFixture(now)[:4], "me@example.com", now, false, "")); strings.Contains(html, "data-other-machines") {
130 t.Fatal("a fold with nothing in it")
131 }
132 // No machines: one way forward.
133 if html := machinesHTML(t, machinesPage(nil, "me@example.com", now, false, "")); !strings.Contains(html, `data-empty-state="machines.empty_title"`) || !strings.Contains(html, `href="/conversation/settings/machines/add"`) {
134 t.Fatalf("empty state:\n%s", html)
135 }
136}
137
138// TestMachinesAdd: Add a machine is its own screen: the install line
139// /install.sh answers, the code, and the person's new machines (never
140// connected, or approved in the last half hour) until they connect.
141// With a machine's code it shows that machine and the same approval
142// form as ever.
143func TestMachinesAdd(t *testing.T) {
144 now := time.Date(2026, 11, 11, 13, 0, 0, 0, time.UTC)
145 install := "curl -fsSL https://equipo.example/install.sh | sh"
146 v := machinesPage(machinesFixture(now), "me@example.com", now, true, install)
147 var ids []string
148 for _, m := range v.New {
149 ids = append(ids, m.ID)
150 }
151 if strings.Join(ids, " ") != "r-desk r-air" {
152 t.Fatalf("new machines %v", ids)
153 }
154 html := machinesHTML(t, v)
155 for _, want := range []string{"<code equipo-conn-code data-command data-install>" + install + "</code>", `data-copy="` + install + `"`, `<form method="post" action="/runners/enrol"`, `name="action" value="inspect"`, `name="user_code"`, "data-new-machines", "data-never-hint", `data-src="/conversation/settings/machines/add"`} {
156 if !strings.Contains(html, want) {
157 t.Fatalf("add page lacks %q:\n%s", want, html)
158 }
159 }
160 if strings.Contains(html, "ana@example.com") {
161 t.Fatal("another person's machine on the person's Add page")
162 }
163 if html := machinesHTML(t, machinesPage(nil, "me@example.com", now, true, "")); !strings.Contains(html, "A versioned runner release is not available.") || !strings.Contains(html, "Recently approved machines appear here, along with any that have never connected.") {
164 t.Fatalf("no release, no machines:\n%s", html)
165 }
166 // With a code: the approval card and its form, unchanged fields.
167 var buf bytes.Buffer
168 data := pageData{Machines: v, Address: "me@example.com", UserCode: "ABCDEFGHJK", PairingConfirmation: "conf", Origin: "https://equipo.example", Enrolment: Runner{Label: "Tito Studio", MachineKeyHash: "abc123", RequestedAt: now}}
169 if err := pages["runners"].ExecuteTemplate(&buf, "content", data); err != nil {
170 t.Fatal(err)
171 }
172 card := buf.String()
173 for _, want := range []string{"Add Tito Studio to your team", "abc123", "https://equipo.example", `name="action" value="approve"`, `name="confirmation" value="conf"`, `name="user_code" value="ABCDEFGHJK"`, `rst-btn="primary" type="submit">Approve machine<`} {
174 if !strings.Contains(card, want) {
175 t.Fatalf("approval lacks %q", want)
176 }
177 }
178 if strings.Contains(card, `value="inspect"`) {
179 t.Fatal("the code form beside an approval")
180 }
181}
182