| 1 | package query |
| 2 | |
| 3 | import "testing" |
| 4 | |
| 5 | // Broken by dropping the results > 0 term — red, then reverted. |
| 6 | // 2026-09-05. This test exists because the HTTP-level version of it |
| 7 | // could not fail: with no results the template renders no list items, so |
| 8 | // a wrongly-primed empty page and a correctly-unprimed one are the same |
| 9 | // bytes. |
| 10 | func TestPrimesFirstResult(t *testing.T) { |
| 11 | cases := []struct { |
| 12 | q string |
| 13 | results int |
| 14 | want bool |
| 15 | }{ |
| 16 | {"pass", 3, true}, |
| 17 | {"pass", 1, true}, |
| 18 | {"pass", 0, false}, // nothing to prime |
| 19 | {"what did we decide about passwords", 3, false}, |
| 20 | {"what did we decide about passwords", 0, false}, |
| 21 | {"", 0, false}, |
| 22 | } |
| 23 | for _, c := range cases { |
| 24 | if got := PrimesFirstResult(c.q, c.results); got != c.want { |
| 25 | t.Errorf("PrimesFirstResult(%q, %d) = %v, want %v", c.q, c.results, got, c.want) |
| 26 | } |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | // The whole point of the heuristic is which side of the line a real |
| 31 | // query falls on, so the table is real queries, not shapes. |
| 32 | func TestIsQuestion(t *testing.T) { |
| 33 | lookups := []string{ |
| 34 | "pass", |
| 35 | "password rotation policy", |
| 36 | "notes from the March offsite", |
| 37 | "Q3 board pack", |
| 38 | "revenue", |
| 39 | "DEK", |
| 40 | "hosting migration", |
| 41 | "whatever happened", // not "what" — a prefix must not match |
| 42 | "canary tokens", // not "can" |
| 43 | "willingness to pay", |
| 44 | "doe, jane", |
| 45 | "HOSTING", |
| 46 | "", |
| 47 | " ", |
| 48 | } |
| 49 | for _, q := range lookups { |
| 50 | if IsQuestion(q) { |
| 51 | t.Errorf("IsQuestion(%q) = true, want false: a lookup must prime the first result", q) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | questions := []string{ |
| 56 | "what did we decide about passwords", |
| 57 | "What did we decide about passwords?", |
| 58 | "who owns billing", |
| 59 | "when did hosting fall", |
| 60 | "where is the DEK stored", |
| 61 | "why did revenue fall", |
| 62 | "how do we rotate the key", |
| 63 | "which region grew", |
| 64 | "whose account is this", |
| 65 | "whom should I ask", |
| 66 | "is the DEK rotated", |
| 67 | "are the backups sealed", |
| 68 | "was the offsite in March", |
| 69 | "were costs down", |
| 70 | "do we have a runbook", |
| 71 | "does anyone own this", |
| 72 | "did we ship it", |
| 73 | "can I delete a document", |
| 74 | "could we index PDFs", |
| 75 | "should the index be rebuilt", |
| 76 | "would that break search", |
| 77 | "will we need SMTP", |
| 78 | "has anyone written this up", |
| 79 | "have we agreed the price", |
| 80 | "am I on the owners list", |
| 81 | "revenue?", // a bare term with a question mark is still a question |
| 82 | " why? ", |
| 83 | } |
| 84 | for _, q := range questions { |
| 85 | if !IsQuestion(q) { |
| 86 | t.Errorf("IsQuestion(%q) = false, want true: a question must not prime the first result", q) |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |