]> git.giacomolli.uk - onechat/blob - probe_log_helper_test.go
Check test assertions with testifylint
[onechat] / probe_log_helper_test.go
1 package main
2
3 import (
4 "encoding/json"
5 "fmt"
6 "math/rand/v2"
7 "strings"
8 )
9
10 // probeMessages is the length of the generated log for the live probe.
11 const probeMessages = 1000
12
13 // ProbeWriter writes readable made-up messages.
14 type ProbeWriter struct {
15 random *rand.Rand
16 }
17
18 func (g *ProbeWriter) message() (string, string) {
19 roll := g.random.IntN(100)
20 switch {
21 case roll < 5:
22 return "user", g.fill(g.userLine, 10+g.random.IntN(200))
23 case roll < 10:
24 return "reply", g.fill(g.replyLine, 100+g.random.IntN(1500))
25 case roll < 12:
26 return "note", g.fill(g.noteLine, 50+g.random.IntN(600))
27 case roll < 13:
28 return "work", "[" + g.pick(probePeople) + "] report: " + g.fill(g.replyLine, 300+g.random.IntN(3000))
29 case roll < 56:
30 return "tool", g.toolCall(60 + g.random.IntN(400))
31 case roll < 57:
32 return "echo", g.fill(g.outputLine, 30000+g.random.IntN(30000))
33 default:
34 return "echo", g.fill(g.outputLine, 20+g.random.IntN(4000))
35 }
36 }
37
38 // fill joins lines of write until the text has about size bytes.
39 func (g *ProbeWriter) fill(write func() string, size int) string {
40 var text strings.Builder
41 for text.Len() < size {
42 if text.Len() > 0 {
43 text.WriteByte(' ')
44 }
45
46 text.WriteString(write())
47 }
48
49 return text.String()
50 }
51
52 func (g *ProbeWriter) userLine() string {
53 return g.pick([]string{
54 fmt.Sprintf("Please check why the %s %s.", g.pick(probeThings), g.pick(probeProblems)),
55 fmt.Sprintf("Move the %s to %s before %s.", g.pick(probeThings), g.pick(probePlaces), g.pick(probeDays)),
56 fmt.Sprintf("Thanks, that works. Next, %s.", g.pick(probeTasks)),
57 fmt.Sprintf("No, keep the old %s. %s asked for it.", g.pick(probeThings), g.pick(probePeople)),
58 fmt.Sprintf("How many %s orders came in on %s?", g.pick(probeBreads), g.pick(probeDays)),
59 })
60 }
61
62 func (g *ProbeWriter) replyLine() string {
63 return g.pick([]string{
64 fmt.Sprintf("I looked at %s. The %s %s because %s.", g.pick(probeFiles), g.pick(probeThings), g.pick(probeProblems), g.pick(probeCauses)),
65 fmt.Sprintf("I changed %s, and the tests pass now.", g.pick(probeChanges)),
66 fmt.Sprintf("There were %d %s orders on %s, %d of them late.", 10+g.random.IntN(90), g.pick(probeBreads), g.pick(probeDays), g.random.IntN(10)),
67 fmt.Sprintf("Open question: should %s get %s?", g.pick(probePeople), g.pick(probeTasks)),
68 })
69 }
70
71 func (g *ProbeWriter) noteLine() string {
72 return g.pick([]string{
73 fmt.Sprintf("%s prefers %s.", g.pick(probePeople), g.pick(probePreferences)),
74 fmt.Sprintf("The bakery opens at %d and closes at %d on %s.", 5+g.random.IntN(3), 17+g.random.IntN(4), g.pick(probeDays)),
75 })
76 }
77
78 func (g *ProbeWriter) pick(words []string) string {
79 return words[g.random.IntN(len(words))]
80 }
81
82 // toolCall returns a tool name and its JSON input of about size bytes.
83 func (g *ProbeWriter) toolCall(size int) string {
84 name := g.pick([]string{"shell", "read", "write", "edit"})
85 var input map[string]string
86 switch name {
87 case "shell":
88 input = map[string]string{"command": "grep -n '" + g.pick(probeThings) + "' " + g.pick(probeFiles)}
89 case "read":
90 input = map[string]string{"path": g.pick(probeFiles)}
91 default:
92 input = map[string]string{
93 "path": g.pick(probeFiles),
94 "text": g.fill(g.replyLine, max(0, size-80)),
95 }
96 }
97
98 data, err := json.Marshal(input)
99 if err != nil {
100 panic(err)
101 }
102
103 return name + " " + string(data)
104 }
105
106 func (g *ProbeWriter) outputLine() string {
107 return g.pick([]string{
108 fmt.Sprintf("%02d:%02d order %d for %d %s, packed by %s.", 6+g.random.IntN(12), g.random.IntN(60), 1000+g.random.IntN(9000), 1+g.random.IntN(24), g.pick(probeBreads), g.pick(probePeople)),
109 fmt.Sprintf("%s:%d: %s %s.", g.pick(probeFiles), 1+g.random.IntN(400), g.pick(probeThings), g.pick(probeProblems)),
110 fmt.Sprintf("ok bakery/%s %d.%03ds", g.pick(probeModules), g.random.IntN(3), g.random.IntN(1000)),
111 })
112 }
113
114 // probeLog returns count messages of made-up work on a small bakery order
115 // system, with the mix of kinds and sizes of replayMessage. The same count
116 // always gives the same log.
117 func probeLog(count int) []DatedMessage {
118 g := ProbeWriter{
119 random: rand.New(rand.NewPCG(7, 11)),
120 }
121
122 messages := make([]DatedMessage, count)
123 for id := range messages {
124 kind, text := g.message()
125 messages[id] = DatedMessage{
126 Kind: kind,
127 Text: text,
128 }
129 }
130
131 return messages
132 }
133
134 var (
135 probePeople = []string{"Mara", "Joel", "Ines", "Tomas", "Priya", "Lena"}
136 probeBreads = []string{"rye", "sourdough", "baguette", "brioche", "spelt", "seed loaf"}
137 probeDays = []string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}
138 probeThings = []string{"order form", "price list", "delivery route", "stock count", "invoice export", "oven schedule"}
139 probeProblems = []string{"shows the wrong total", "skips the last row", "is slow at noon", "loses the customer note", "rounds the tax down"}
140 probePlaces = []string{"the front page", "the weekly report", "the shared folder", "the admin menu"}
141 probeTasks = []string{"add a field for allergies", "send the invoices on Friday", "split the route in two", "print labels for the shelf"}
142 probeCauses = []string{"the date has no time zone", "the cache keeps old prices", "the loop stops one row early", "the form trims the note"}
143 probeChanges = []string{"the total to use cents", "the route to sort by street", "the export to add the tax line", "the form to keep the note"}
144 probePreferences = []string{"short answers", "a call before a change to prices", "invoices as PDF", "reports in the morning"}
145 probeFiles = []string{"orders/form.go", "prices/list.go", "routes/plan.go", "stock/count.go", "export/invoice.go", "ovens/schedule.go"}
146 probeModules = []string{"orders", "prices", "routes", "stock", "export", "ovens"}
147 )