| 1 | package slides |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "amadan.net/oficina/slides/internal/slides/assist" |
| 12 | "amadan.net/oficina/slides/internal/slides/deck" |
| 13 | ) |
| 14 | |
| 15 | // testDeck is a two-slide deck: s1 with a heading and a list, s2 empty. |
| 16 | func testDeck() *deck.Deck { |
| 17 | return &deck.Deck{ |
| 18 | Meta: deck.Meta{Title: "Q3", Theme: "paper"}, |
| 19 | Slides: []deck.Slide{ |
| 20 | {ID: "slideoneaaaa", Layout: "flow", Template: "title-body", Transition: "none", Blocks: []deck.Block{ |
| 21 | {ID: "headingaaaaa", Type: "heading", Region: "heading", Props: map[string]any{"level": 1.0}, |
| 22 | Text: deck.Text{{{T: "Revenue "}, {T: "up", B: true}}}}, |
| 23 | {ID: "listaaaaaaaa", Type: "list", Region: "body", |
| 24 | Text: deck.Text{{{T: "One"}}, {{T: "Two *", I: true}}}}, |
| 25 | }}, |
| 26 | {ID: "slidetwoaaaa", Layout: "flow", Template: "section", Transition: "none"}, |
| 27 | }, |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | func ops(t *testing.T, batch string) []map[string]json.RawMessage { |
| 32 | t.Helper() |
| 33 | var out []map[string]json.RawMessage |
| 34 | if err := json.Unmarshal([]byte(batch), &out); err != nil { |
| 35 | t.Fatalf("test batch: %v", err) |
| 36 | } |
| 37 | return out |
| 38 | } |
| 39 | |
| 40 | // counter mints predictable ids so a test can name them. |
| 41 | func counter() func() (string, error) { |
| 42 | n := 0 |
| 43 | return func() (string, error) { |
| 44 | n++ |
| 45 | return fmt.Sprintf("minted%06d", n), nil |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | func encoded(t *testing.T, v any) string { |
| 50 | t.Helper() |
| 51 | b, err := json.Marshal(v) |
| 52 | if err != nil { |
| 53 | t.Fatal(err) |
| 54 | } |
| 55 | return string(b) |
| 56 | } |
| 57 | |
| 58 | func TestAssistOpsMintEveryNewIDAndResolvePlaceholders(t *testing.T) { |
| 59 | got, err := normalizeAssistOps(testDeck(), ops(t, `[ |
| 60 | {"op":"add_slide","id":"s1","layout":"flow","template":"title-body","after":"slideoneaaaa","colour":"red"}, |
| 61 | {"op":"add_block","slide":"s1","id":"b1","type":"heading","region":"heading","props":{"level":1},"markdown":"Hi"}, |
| 62 | {"op":"add_block","slide":"s1","type":"columns","region":"body","props":{"children":["b1"]}}, |
| 63 | {"op":"set_notes","id":"s1","markdown":"Say hi."}, |
| 64 | {"op":"duplicate_slide","id":"slideoneaaaa","new_id":"s2","new_block_ids":["x","y"]}, |
| 65 | {"op":"set_text","slide":"s2","id":"y","markdown":"- Three"}, |
| 66 | {"op":"move_slide","id":"s2","after":null} |
| 67 | ]`), assistScope{}, counter()) |
| 68 | if err != nil { |
| 69 | t.Fatal(err) |
| 70 | } |
| 71 | want := `[{"after":"slideoneaaaa","id":"minted000001","layout":"flow","op":"add_slide","template":"title-body"},` + |
| 72 | `{"id":"minted000002","markdown":"Hi","op":"add_block","props":{"level":1},"region":"heading","slide":"minted000001","type":"heading"},` + |
| 73 | `{"id":"minted000003","op":"add_block","props":{"children":["minted000002"]},"region":"body","slide":"minted000001","type":"columns"},` + |
| 74 | `{"id":"minted000001","markdown":"Say hi.","op":"set_notes"},` + |
| 75 | `{"id":"slideoneaaaa","new_block_ids":["minted000005","minted000006"],"new_id":"minted000004","op":"duplicate_slide"},` + |
| 76 | `{"id":"minted000006","markdown":"- Three","op":"set_text","slide":"minted000004"},` + |
| 77 | `{"after":null,"id":"minted000004","op":"move_slide"}]` |
| 78 | if g := encoded(t, got.ops); g != want { |
| 79 | t.Errorf("normalized batch:\n got %s\nwant %s", g, want) |
| 80 | } |
| 81 | if got.focus != "minted000001" { |
| 82 | t.Errorf("focus = %q, want the first slide added", got.focus) |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | // TestAssistOpsNeverSetFonts: set_meta's fonts are a font stack nothing |
| 87 | // here validates, so a model told by a deck's text to change them gets |
| 88 | // the title through and the fonts dropped (Claude Fable, #42). |
| 89 | func TestAssistOpsNeverSetFonts(t *testing.T) { |
| 90 | got, err := normalizeAssistOps(testDeck(), ops(t, `[ |
| 91 | {"op":"set_meta","title":"Renamed","fonts":{"heading":"Comic Sans MS","body":"x"}} |
| 92 | ]`), assistScope{}, counter()) |
| 93 | if err != nil { |
| 94 | t.Fatal(err) |
| 95 | } |
| 96 | if g, want := encoded(t, got.ops), `[{"op":"set_meta","title":"Renamed"}]`; g != want { |
| 97 | t.Errorf("normalized batch:\n got %s\nwant %s", g, want) |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // TestAssistOpsKeepStepsItemsTalkAndArrive: the prompt offers them, so |
| 102 | // normalizing keeps them for the core to check. |
| 103 | func TestAssistOpsKeepStepsItemsTalkAndArrive(t *testing.T) { |
| 104 | got, err := normalizeAssistOps(testDeck(), ops(t, `[ |
| 105 | {"op":"set_meta","arrive":"rise"}, |
| 106 | {"op":"set_slide","id":"slidetwoaaaa","talk":"parked"}, |
| 107 | {"op":"set_block","slide":"slideoneaaaa","id":"listaaaaaaaa","step":1.5,"items":"each"} |
| 108 | ]`), assistScope{}, counter()) |
| 109 | if err != nil { |
| 110 | t.Fatal(err) |
| 111 | } |
| 112 | want := `[{"arrive":"rise","op":"set_meta"},` + |
| 113 | `{"id":"slidetwoaaaa","op":"set_slide","talk":"parked"},` + |
| 114 | `{"id":"listaaaaaaaa","items":"each","op":"set_block","slide":"slideoneaaaa","step":1.5}]` |
| 115 | if g := encoded(t, got.ops); g != want { |
| 116 | t.Errorf("normalized batch:\n got %s\nwant %s", g, want) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | func TestAssistOpsRefuseWhatTheEditorWouldNotDo(t *testing.T) { |
| 121 | for _, tc := range []struct{ name, batch, want string }{ |
| 122 | {"an asset op", `[{"op":"register_asset","id":"a","ref":"x"}]`, `"register_asset" is not an operation`}, |
| 123 | {"no op at all", `[{"title":"x"}]`, `"" is not an operation`}, |
| 124 | {"a placeholder naming a real slide", `[{"op":"add_slide","id":"slideoneaaaa","layout":"flow"}]`, "already in use"}, |
| 125 | {"a placeholder used twice", `[{"op":"add_slide","id":"s","layout":"flow"},{"op":"add_block","slide":"s","id":"s","type":"text","region":"body"}]`, "already in use"}, |
| 126 | {"a theme that is not one", `[{"op":"set_meta","theme":"neon"}]`, "theme must be"}, |
| 127 | {"an accent that is not a colour", `[{"op":"set_meta","accent":"red; x"}]`, "accent must be"}, |
| 128 | {"too much text", `[{"op":"set_notes","id":"slideoneaaaa","markdown":"` + strings.Repeat("x", maxAssistMarkdown+1) + `"}]`, "longer than"}, |
| 129 | {"children that are not ids", `[{"op":"set_block","slide":"slideoneaaaa","id":"listaaaaaaaa","props":{"children":"x"}}]`, "children"}, |
| 130 | } { |
| 131 | t.Run(tc.name, func(t *testing.T) { |
| 132 | _, err := normalizeAssistOps(testDeck(), ops(t, tc.batch), assistScope{}, counter()) |
| 133 | var r *assistRefusal |
| 134 | if !errors.As(err, &r) || !strings.Contains(r.msg, tc.want) { |
| 135 | t.Fatalf("err = %v, want a refusal saying %q", err, tc.want) |
| 136 | } |
| 137 | }) |
| 138 | } |
| 139 | many := "[" + strings.TrimSuffix(strings.Repeat(`{"op":"set_meta","title":"x"},`, maxAssistOps+1), ",") + "]" |
| 140 | if _, err := normalizeAssistOps(testDeck(), ops(t, many), assistScope{}, counter()); err == nil { |
| 141 | t.Errorf("%d ops were accepted", maxAssistOps+1) |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | // TestAssistOpsKeepToTheSlideAskedAbout: "this slide" is that slide and |
| 146 | // what the answer adds, and nothing else — not the deck's meta, not |
| 147 | // another slide, not removing or moving the slide itself. |
| 148 | func TestAssistOpsKeepToTheSlideAskedAbout(t *testing.T) { |
| 149 | scope := assistScope{slide: "slideoneaaaa"} |
| 150 | ok := `[ |
| 151 | {"op":"set_text","slide":"slideoneaaaa","id":"headingaaaaa","markdown":"Punchier"}, |
| 152 | {"op":"set_slide","id":"slideoneaaaa","transition":"fade"}, |
| 153 | {"op":"add_slide","id":"half","layout":"flow","template":"title-body","after":"slideoneaaaa"}, |
| 154 | {"op":"add_block","slide":"half","type":"list","region":"body","markdown":"- B"}, |
| 155 | {"op":"remove_block","slide":"slideoneaaaa","id":"listaaaaaaaa"} |
| 156 | ]` |
| 157 | got, err := normalizeAssistOps(testDeck(), ops(t, ok), scope, counter()) |
| 158 | if err != nil { |
| 159 | t.Fatalf("splitting the slide was refused: %v", err) |
| 160 | } |
| 161 | if got.focus != "minted000001" { |
| 162 | t.Errorf("focus = %q, want the new half", got.focus) |
| 163 | } |
| 164 | for _, bad := range []string{ |
| 165 | `[{"op":"set_meta","title":"x"}]`, |
| 166 | `[{"op":"set_notes","id":"slidetwoaaaa","markdown":"x"}]`, |
| 167 | `[{"op":"set_slide","id":"slidetwoaaaa","transition":"fade"}]`, |
| 168 | `[{"op":"add_block","slide":"slidetwoaaaa","type":"text","region":"body"}]`, |
| 169 | `[{"op":"remove_slide","id":"slideoneaaaa"}]`, |
| 170 | `[{"op":"move_slide","id":"slideoneaaaa","after":null}]`, |
| 171 | `[{"op":"remove_slide","id":"slidetwoaaaa"}]`, |
| 172 | } { |
| 173 | if _, err := normalizeAssistOps(testDeck(), ops(t, bad), scope, counter()); err == nil { |
| 174 | t.Errorf("slide scope accepted %s", bad) |
| 175 | } |
| 176 | } |
| 177 | got, err = normalizeAssistOps(testDeck(), ops(t, `[{"op":"set_text","slide":"slideoneaaaa","id":"headingaaaaa","markdown":"x"}]`), scope, counter()) |
| 178 | if err != nil || got.focus != "slideoneaaaa" { |
| 179 | t.Errorf("an edit in place focuses the slide asked about: focus %q, err %v", got.focus, err) |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | func TestAssistResultNeverEmptiesADeck(t *testing.T) { |
| 184 | before := testDeck() |
| 185 | if err := checkAssistResult(before, &deck.Deck{}); err == nil { |
| 186 | t.Error("a batch removing every slide was accepted") |
| 187 | } |
| 188 | if err := checkAssistResult(&deck.Deck{}, &deck.Deck{}); err != nil { |
| 189 | t.Errorf("an empty deck left empty: %v", err) |
| 190 | } |
| 191 | big := &deck.Deck{Slides: make([]deck.Slide, maxAssistSlides+1)} |
| 192 | if err := checkAssistResult(before, big); err == nil { |
| 193 | t.Error("a deck past the slide ceiling was accepted") |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | func TestAssistContextIsTheMarkdownTheOpsTake(t *testing.T) { |
| 198 | raw, err := assistContext(testDeck(), assistScope{}) |
| 199 | if err != nil { |
| 200 | t.Fatal(err) |
| 201 | } |
| 202 | s := string(raw) |
| 203 | for _, want := range []string{ |
| 204 | `"markdown":"Revenue **up**"`, |
| 205 | `"markdown":"- One\n- *Two \\**"`, |
| 206 | `"template":"section"`, |
| 207 | } { |
| 208 | if !strings.Contains(s, want) { |
| 209 | t.Errorf("the deck context has no %s:\n%s", want, s) |
| 210 | } |
| 211 | } |
| 212 | // One slide's scope: the others are an outline. |
| 213 | raw, err = assistContext(testDeck(), assistScope{slide: "slidetwoaaaa"}) |
| 214 | if err != nil { |
| 215 | t.Fatal(err) |
| 216 | } |
| 217 | if s := string(raw); strings.Contains(s, "listaaaaaaaa") || !strings.Contains(s, `"heading":"Revenue up"`) { |
| 218 | t.Errorf("slide scope should outline the other slides:\n%s", s) |
| 219 | } |
| 220 | if got := blockMarkdown("code", nil, deck.Text{{{T: "a *b*"}}, {}, {{T: "c"}}}); got != "a *b*\n\nc" { |
| 221 | t.Errorf("code markdown = %q", got) |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | func TestAssistContextRefusesADeckTooLargeToSend(t *testing.T) { |
| 226 | d := &deck.Deck{} |
| 227 | for i := range 200 { |
| 228 | d.Slides = append(d.Slides, deck.Slide{ID: fmt.Sprintf("s%011d", i), Blocks: []deck.Block{ |
| 229 | {ID: fmt.Sprintf("b%011d", i), Type: "text", Text: deck.Text{{{T: strings.Repeat("words ", 300)}}}}, |
| 230 | }}) |
| 231 | } |
| 232 | if _, err := assistContext(d, assistScope{}); !errors.Is(err, errAssistTooLarge) { |
| 233 | t.Errorf("err = %v, want errAssistTooLarge", err) |
| 234 | } |
| 235 | if _, err := assistContext(d, assistScope{slide: "s00000000000"}); err != nil { |
| 236 | t.Errorf("one slide of the same deck: %v", err) |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | func TestAssistAdmitsOneAtATimeAndTwentyATenMinutes(t *testing.T) { |
| 241 | s := newAssistant(nil, []string{"m"}) |
| 242 | now := time.Date(2026, 11, 11, 9, 0, 0, 0, time.UTC) |
| 243 | s.now = func() time.Time { return now } |
| 244 | release, refusal := s.admit(7) |
| 245 | if refusal != "" { |
| 246 | t.Fatal(refusal) |
| 247 | } |
| 248 | if _, refusal := s.admit(7); refusal != assistErrBusy { |
| 249 | t.Errorf("a second request while one is out: %q, want busy", refusal) |
| 250 | } |
| 251 | if r, refusal := s.admit(8); refusal != "" { |
| 252 | t.Errorf("another person is not held up: %q", refusal) |
| 253 | } else { |
| 254 | r() |
| 255 | } |
| 256 | release() |
| 257 | for i := 1; i < assistPerWindow; i++ { |
| 258 | r, refusal := s.admit(7) |
| 259 | if refusal != "" { |
| 260 | t.Fatalf("request %d: %q", i+1, refusal) |
| 261 | } |
| 262 | r() |
| 263 | } |
| 264 | if _, refusal := s.admit(7); refusal != assistErrRate { |
| 265 | t.Errorf("request %d in the window: %q, want rate", assistPerWindow+1, refusal) |
| 266 | } |
| 267 | now = now.Add(assistWindow) |
| 268 | if r, refusal := s.admit(7); refusal != "" { |
| 269 | t.Errorf("after the window: %q", refusal) |
| 270 | } else { |
| 271 | r() |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | func TestAIConfigIsOnOnlyWhenSaidExactly(t *testing.T) { |
| 276 | env := func(kv map[string]string) func(string) string { return func(k string) string { return kv[k] } } |
| 277 | c, err := AIConfigFromEnv(env(map[string]string{"SLIDES_TENSORX_API_KEY": "k"})) |
| 278 | if err != nil || c.On { |
| 279 | t.Errorf("a key alone: on=%v err=%v, want off", c.On, err) |
| 280 | } |
| 281 | for _, typo := range []string{"On", "true", "1", "yes", "on "} { |
| 282 | if _, err := AIConfigFromEnv(env(map[string]string{"SLIDES_AI": typo})); err == nil { |
| 283 | t.Errorf("SLIDES_AI=%q did not refuse", typo) |
| 284 | } |
| 285 | } |
| 286 | c, err = AIConfigFromEnv(env(map[string]string{"SLIDES_AI": "on", "SLIDES_TENSORX_API_KEY": "k", "SLIDES_AI_MODELS": " zai/glm-5.3-flash, deepseek/deepseek-v4-flash-0731 ,,zai/glm-5.3-flash"})) |
| 287 | if err != nil { |
| 288 | t.Fatal(err) |
| 289 | } |
| 290 | resolved, err := resolveAI(Config{AI: c}) |
| 291 | if err != nil { |
| 292 | t.Fatal(err) |
| 293 | } |
| 294 | if got := strings.Join(resolved.Models, ","); got != "zai/glm-5.3-flash,deepseek/deepseek-v4-flash-0731" { |
| 295 | t.Errorf("models = %s", got) |
| 296 | } |
| 297 | if r, err := resolveAI(Config{AI: AIConfig{On: true, Key: "k"}}); err != nil || r.Models[0] != assist.DefaultModel { |
| 298 | t.Errorf("no models named: %v %v", r.Models, err) |
| 299 | } |
| 300 | for name, cfg := range map[string]Config{ |
| 301 | "on with no key": {AI: AIConfig{On: true}}, |
| 302 | "a model id with spaces": {AI: AIConfig{On: true, Key: "k", Models: []string{"a b"}}}, |
| 303 | "a dev base in prod": {Env: EnvProduction, AI: AIConfig{On: true, Key: "k", DevBaseURL: "http://127.0.0.1:9/v1"}}, |
| 304 | "a dev base, AI off": {Env: EnvProduction, AI: AIConfig{DevBaseURL: "http://127.0.0.1:9/v1"}}, |
| 305 | } { |
| 306 | if _, err := resolveAI(cfg); err == nil { |
| 307 | t.Errorf("%s: accepted", name) |
| 308 | } |
| 309 | } |
| 310 | if _, err := resolveAI(Config{Env: EnvDevelopment, AI: AIConfig{On: true, Key: "k", DevBaseURL: "http://127.0.0.1:9/v1"}}); err != nil { |
| 311 | t.Errorf("a dev base in development: %v", err) |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | func TestModelLabelsDropTheVendorUnlessThatMakesTwoAlike(t *testing.T) { |
| 316 | if got := strings.Join(modelLabels([]string{"deepseek/deepseek-v4-flash-0731", "zai/glm-5.3-flash", "plain"}), ","); got != "deepseek-v4-flash-0731,glm-5.3-flash,plain" { |
| 317 | t.Errorf("labels = %s", got) |
| 318 | } |
| 319 | if got := strings.Join(modelLabels([]string{"a/flash", "b/flash"}), ","); got != "a/flash,b/flash" { |
| 320 | t.Errorf("alike labels = %s", got) |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | // TestAssistOpsBuildScenes: the assistant may add, change and remove |
| 325 | // scenes and put scene and clip blocks on slides; a scene placeholder is |
| 326 | // resolved in a block's props.scene, any author is dropped, and a |
| 327 | // request for one slide cannot change the deck's scenes. |
| 328 | func TestAssistOpsBuildScenes(t *testing.T) { |
| 329 | code := strings.Repeat("ab", 32) |
| 330 | got, err := normalizeAssistOps(testDeck(), ops(t, `[ |
| 331 | {"op":"add_scene","id":"w1","name":"World","code":"`+code+`","states":[{"id":"a"},{"id":"b"}],"words":{"t":"x"},"data":{"n":1},"author":"person:9"}, |
| 332 | {"op":"add_block","slide":"slideoneaaaa","id":"b1","type":"scene","region":"media","props":{"scene":"w1","from":"a","to":"b"}}, |
| 333 | {"op":"set_scene","id":"w1","words":{"t":"y"},"stills":{"a":null},"data":null,"author":"x"}, |
| 334 | {"op":"add_block","slide":"slideoneaaaa","type":"clip","region":"media","props":{"asset":"v","play":"loop"}}, |
| 335 | {"op":"remove_scene","id":"w1"} |
| 336 | ]`), assistScope{}, counter()) |
| 337 | if err != nil { |
| 338 | t.Fatal(err) |
| 339 | } |
| 340 | want := `[{"code":"` + code + `","data":{"n":1},"id":"minted000001","name":"World","op":"add_scene","states":[{"id":"a"},{"id":"b"}],"words":{"t":"x"}},` + |
| 341 | `{"id":"minted000002","op":"add_block","props":{"from":"a","scene":"minted000001","to":"b"},"region":"media","slide":"slideoneaaaa","type":"scene"},` + |
| 342 | `{"data":null,"id":"minted000001","op":"set_scene","stills":{"a":null},"words":{"t":"y"}},` + |
| 343 | `{"id":"minted000003","op":"add_block","props":{"asset":"v","play":"loop"},"region":"media","slide":"slideoneaaaa","type":"clip"},` + |
| 344 | `{"id":"minted000001","op":"remove_scene"}]` |
| 345 | if g := encoded(t, got.ops); g != want { |
| 346 | t.Errorf("normalized batch:\n got %s\nwant %s", g, want) |
| 347 | } |
| 348 | scope := assistScope{slide: "slideoneaaaa"} |
| 349 | for _, bad := range []string{ |
| 350 | `[{"op":"add_scene","name":"W","code":"` + code + `","states":[{"id":"a"}]}]`, |
| 351 | `[{"op":"set_scene","id":"w","name":"x"}]`, |
| 352 | `[{"op":"remove_scene","id":"w"}]`, |
| 353 | } { |
| 354 | if _, err := normalizeAssistOps(testDeck(), ops(t, bad), scope, counter()); err == nil { |
| 355 | t.Errorf("slide scope accepted %s", bad) |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | |