pasta_lua 0.2.3

Pasta Lua - Lua integration for Pasta DSL
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
-- sakura_builder module tests
-- Tests for pasta.shiori.sakura_builder module - grouped token to sakura script conversion
local describe = require("lua_test.test").describe
local test = require("lua_test.test").test
local expect = require("lua_test.test").expect

-- ============================================================================
-- ヘルパー: モックアクター作成
-- ============================================================================

local function create_mock_actors()
    return {
        sakura = { name = "さくら", spot = 0 },
        kero = { name = "うにゅう", spot = 1 },
    }
end

-- ヘルパー: テストごとの共通セットアップ(BUILDER と actors を返す)
-- リロード規約(先行スイートの package.loaded 操作)を保存するため、
-- require はファイル先頭ではなくテスト実行時に行う
local function setup()
    return require("pasta.shiori.sakura_builder"), create_mock_actors()
end

-- ============================================================================
-- Requirement 6: sakura_builderモジュール(グループ化形式)
-- ============================================================================

describe("SAKURA_BUILDER - talk token", function()
    test("talkトークンがtalk_to_scriptで変換される", function()
        local BUILDER, actors = setup()

        local tokens = {
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "talk", actor = actors.sakura, text = "Hello" },
                }
            },
        }
        local result = BUILDER.build(tokens, {})

        -- デフォルト設定(script_wait_normal=50)では
        -- effective_wait = 50 - 50 = 0 なのでウェイトタグは挿入されない
        -- 文字列がそのまま含まれることを確認
        expect(result:find("Hello")):toBeTruthy()
    end)

    test("句点にはウェイトタグが挿入される", function()
        local BUILDER, actors = setup()

        local tokens = {
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "talk", actor = actors.sakura, text = "あ。" },
                }
            },
        }
        local result = BUILDER.build(tokens, {})

        -- 句点(。)にはデフォルトでウェイトタグが挿入される
        -- script_wait_period=1000 → effective=950
        expect(result:find("\\_w%[950%]")):toBeTruthy()
    end)

    test("読点にはウェイトタグが挿入される", function()
        local BUILDER, actors = setup()

        local tokens = {
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "talk", actor = actors.sakura, text = "あ、" },
                }
            },
        }
        local result = BUILDER.build(tokens, {})

        -- 読点(、)にはデフォルトでウェイトタグが挿入される
        -- script_wait_comma=500 → effective=450
        expect(result:find("\\_w%[450%]")):toBeTruthy()
    end)
end)

describe("SAKURA_BUILDER - surface token", function()
    test("surfaceトークンを \\s[id] に変換する", function()
        local BUILDER, actors = setup()

        local tokens = {
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "talk",    actor = actors.sakura, text = "" },
                    { type = "surface", id = 5 },
                }
            },
        }
        local result = BUILDER.build(tokens, {})

        expect(result:find("\\s%[5%]")):toBeTruthy()
    end)

    test("文字列IDをサポートする", function()
        local BUILDER, actors = setup()

        local tokens = {
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "talk",    actor = actors.sakura, text = "" },
                    { type = "surface", id = "smile" },
                }
            },
        }
        local result = BUILDER.build(tokens, {})

        expect(result:find("\\s%[smile%]")):toBeTruthy()
    end)
end)

describe("SAKURA_BUILDER - wait token", function()
    test("waitトークンを \\w[ms] に変換する", function()
        local BUILDER, actors = setup()

        local tokens = {
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "talk", actor = actors.sakura, text = "" },
                    { type = "wait", ms = 500 },
                }
            },
        }
        local result = BUILDER.build(tokens, {})

        expect(result:find("\\w%[500%]")):toBeTruthy()
    end)
end)

describe("SAKURA_BUILDER - newline token", function()
    test("newlineトークンを \\n に変換する(n=1)", function()
        local BUILDER, actors = setup()

        local tokens = {
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "talk",    actor = actors.sakura, text = "" },
                    { type = "newline", n = 1 },
                }
            },
        }
        local result = BUILDER.build(tokens, {})

        expect(result:find("\\n")):toBeTruthy()
    end)

    test("複数改行を連続出力する(n=3)", function()
        local BUILDER, actors = setup()

        local tokens = {
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "talk",    actor = actors.sakura, text = "" },
                    { type = "newline", n = 3 },
                }
            },
        }
        local result = BUILDER.build(tokens, {})

        expect(result:find("\\n\\n\\n")):toBeTruthy()
    end)
end)

describe("SAKURA_BUILDER - clear token", function()
    test("clearトークンを \\c に変換する", function()
        local BUILDER, actors = setup()

        local tokens = {
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "talk", actor = actors.sakura, text = "" },
                    { type = "clear" },
                }
            },
        }
        local result = BUILDER.build(tokens, {})

        expect(result:find("\\c")):toBeTruthy()
    end)
end)

describe("SAKURA_BUILDER - raw_script token", function()
    test("raw_scriptトークンをそのまま出力する(エスケープなし)", function()
        local BUILDER, actors = setup()

        local tokens = {
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "talk",       actor = actors.sakura,      text = "" },
                    { type = "raw_script", text = "\\![open,calendar]" },
                }
            },
        }
        local result = BUILDER.build(tokens, {})

        expect(result:find("\\!%[open,calendar%]")):toBeTruthy()
    end)
end)

describe("SAKURA_BUILDER - yield token", function()
    test("yieldトークンは無視される", function()
        local BUILDER, actors = setup()

        local tokens = {
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "surface", id = 5 },
                    { type = "yield" },
                }
            },
        }
        local result = BUILDER.build(tokens, {})

        expect(result:find("\\s%[5%]")):toBeTruthy()
        -- yieldは出力されない
    end)
end)

describe("SAKURA_BUILDER - \\e終端", function()
    test("出力末尾に \\e を付与する", function()
        local BUILDER, actors = setup()

        local tokens = {
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "talk", actor = actors.sakura, text = "Hello" },
                }
            },
        }
        local result = BUILDER.build(tokens, {})

        expect(result:sub(-2)):toBe("\\e")
    end)

    test("空トークン配列でも \\e を付与する", function()
        local BUILDER = setup()

        local result = BUILDER.build({}, {})

        expect(result):toBe("\\e")
    end)
end)

describe("SAKURA_BUILDER - 複合シナリオ", function()
    test("複数トークンを正しく連結する", function()
        local BUILDER, actors = setup()

        local tokens = {
            { type = "spot", actor = actors.sakura, spot = 0 },
            { type = "spot", actor = actors.kero,   spot = 1 },
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "talk",    actor = actors.sakura, text = "Hello" },
                    { type = "surface", id = 5 },
                    { type = "wait",    ms = 100 },
                }
            },
            {
                type = "actor",
                actor = actors.kero,
                tokens = {
                    { type = "talk",    actor = actors.kero, text = "Hi" },
                    { type = "newline", n = 1 },
                    { type = "clear" },
                }
            },
        }
        local result = BUILDER.build(tokens, { spot_newlines = 1.5 })

        expect(result:find("\\p%[0%]")):toBeTruthy()
        expect(result:find("Hello")):toBeTruthy()
        expect(result:find("\\s%[5%]")):toBeTruthy()
        expect(result:find("\\w%[100%]")):toBeTruthy()
        expect(result:find("\\n%[150%]")):toBeTruthy() -- spot変更時の段落改行
        expect(result:find("\\p%[1%]")):toBeTruthy()
        expect(result:find("Hi")):toBeTruthy()
        expect(result:find("\\n")):toBeTruthy()
        expect(result:find("\\c")):toBeTruthy()
        expect(result:sub(-2)):toBe("\\e")
    end)
end)

describe("SAKURA_BUILDER - talk_to_script変換", function()
    test("テキストがtalk_to_scriptで変換される", function()
        local BUILDER, actors = setup()

        local tokens = {
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "talk", actor = actors.sakura, text = "50 off sale" },
                }
            },
        }
        local result = BUILDER.build(tokens, {})

        -- デフォルト設定ではウェイトタグは挿入されない
        -- 文字列がそのまま含まれることを確認
        expect(result:find("50 off sale")):toBeTruthy()
    end)
end)

-- ============================================================================
-- actor-spot-refactoring: spotトークン処理 (Task 2.1)
-- ============================================================================

describe("SAKURA_BUILDER - spotトークン処理", function()
    test("spotトークン処理でactor_spots[actor.name]が正しく更新される", function()
        local BUILDER, actors = setup()

        local tokens = {
            { type = "spot", actor = actors.sakura, spot = 0 },
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "talk", actor = actors.sakura, text = "Hello" },
                }
            },
        }
        local result = BUILDER.build(tokens, {})

        -- spot設定後のtalk → \p[0]が出力される
        expect(result:find("\\p%[0%]")):toBeTruthy()
        expect(result:find("Hello")):toBeTruthy()
    end)

    test("複数actorのspot独立管理を確認", function()
        local BUILDER, actors = setup()

        local tokens = {
            { type = "spot", actor = actors.sakura, spot = 0 },
            { type = "spot", actor = actors.kero,   spot = 1 },
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "talk", actor = actors.sakura, text = "Hi Sakura" },
                }
            },
            {
                type = "actor",
                actor = actors.kero,
                tokens = {
                    { type = "talk", actor = actors.kero, text = "Hi Kero" },
                }
            },
        }
        local result = BUILDER.build(tokens, {})

        -- 両方のスポットタグが出力される
        expect(result:find("\\p%[0%]")):toBeTruthy()
        expect(result:find("\\p%[1%]")):toBeTruthy()
    end)
end)

-- ============================================================================
-- actor-spot-refactoring: clear_spotトークン処理 (Task 2.2)
-- ============================================================================

describe("SAKURA_BUILDER - clear_spotトークン処理", function()
    test("clear_spotトークン処理でactor_spots={}にリセットされる", function()
        local BUILDER, actors = setup()

        local tokens = {
            { type = "spot",      actor = actors.sakura, spot = 5 },
            { type = "clear_spot" },
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "talk", actor = actors.sakura, text = "Reset" },
                }
            },
        }
        local result = BUILDER.build(tokens, {})

        -- clear_spot後はデフォルトspot(0)を使用
        expect(result:find("\\p%[0%]")):toBeTruthy()
        expect(result:find("Reset")):toBeTruthy()
    end)

    test("clear_spotトークン処理でlast_actor=nilにリセットされる", function()
        local BUILDER, actors = setup()

        local tokens = {
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "talk", actor = actors.sakura, text = "Before" },
                }
            },
            { type = "clear_spot" },
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "talk", actor = actors.sakura, text = "After" },
                }
            },
        }
        local result = BUILDER.build(tokens, {})

        -- clear_spot後に同じactorでもスポットタグが再出力される
        -- \p[0]が2回出現するはず
        local count = 0
        for _ in result:gmatch("\\p%[0%]") do
            count = count + 1
        end
        expect(count):toBe(2)
    end)
end)

-- ============================================================================
-- actor-spot-refactoring: actorトークンのactor切り替え検出 (Task 2.3)
-- ============================================================================

describe("SAKURA_BUILDER - actorトークンのactor切り替え検出", function()
    test("actor_spots未設定時にデフォルト値0を使用する", function()
        local BUILDER, actors = setup()

        local tokens = {
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "talk", actor = actors.sakura, text = "Hello" },
                }
            },
        }
        local result = BUILDER.build(tokens, {})

        -- デフォルトspot(0)が使用される
        expect(result:find("\\p%[0%]")):toBeTruthy()
        expect(result:find("Hello")):toBeTruthy()
    end)

    test("last_actor != token.actor時に\\p[spot]を出力する", function()
        local BUILDER, actors = setup()

        local tokens = {
            { type = "spot", actor = actors.sakura, spot = 0 },
            { type = "spot", actor = actors.kero,   spot = 1 },
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "talk", actor = actors.sakura, text = "First" },
                }
            },
            {
                type = "actor",
                actor = actors.kero,
                tokens = {
                    { type = "talk", actor = actors.kero, text = "Second" },
                }
            },
        }
        local result = BUILDER.build(tokens, {})

        -- actor切り替え時にスポットタグが出力される
        expect(result:find("\\p%[0%].*First")):toBeTruthy()
        expect(result:find("\\p%[1%].*Second")):toBeTruthy()
    end)

    test("spot変更時に\\n[N]を出力する(Nはconfig.spot_newlines * 100)", function()
        local BUILDER, actors = setup()

        local tokens = {
            { type = "spot", actor = actors.sakura, spot = 0 },
            { type = "spot", actor = actors.kero,   spot = 1 },
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "talk", actor = actors.sakura, text = "First" },
                }
            },
            {
                type = "actor",
                actor = actors.kero,
                tokens = {
                    { type = "talk", actor = actors.kero, text = "Second" },
                }
            },
        }
        local result = BUILDER.build(tokens, { spot_newlines = 1.5 })

        -- spot変更時に段落改行が出力される
        expect(result:find("\\n%[150%]")):toBeTruthy()
    end)

    test("同じactorの連続actorトークンではスポットタグを出力しない", function()
        local BUILDER, actors = setup()

        local tokens = {
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "talk", actor = actors.sakura, text = "First" },
                }
            },
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "talk", actor = actors.sakura, text = "Second" },
                }
            },
        }
        local result = BUILDER.build(tokens, {})

        -- スポットタグは1回のみ
        local count = 0
        for _ in result:gmatch("\\p%[0%]") do
            count = count + 1
        end
        expect(count):toBe(1)
        expect(result:find("FirstSecond")):toBeTruthy()
    end)
end)

-- ============================================================================
-- actor-spot-refactoring: 統合シナリオ (Task 4.4)
-- ============================================================================

describe("SAKURA_BUILDER - 統合シナリオ(グループ化トークン構造)", function()
    test("set_spot()なしでのtalk() → デフォルトspot(0)使用を確認", function()
        local BUILDER, actors = setup()

        local tokens = {
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "talk", actor = actors.sakura, text = "No spot set" },
                }
            },
        }
        local result = BUILDER.build(tokens, {})

        expect(result:find("\\p%[0%]")):toBeTruthy()
        expect(result:find("No spot set")):toBeTruthy()
    end)

    test("set_spot() → talk() → spot切り替えとスポットタグ出力を確認", function()
        local BUILDER, actors = setup()

        local tokens = {
            { type = "spot", actor = actors.sakura, spot = 0 },
            { type = "spot", actor = actors.kero,   spot = 1 },
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "talk", actor = actors.sakura, text = "Sakura speaks" },
                }
            },
            {
                type = "actor",
                actor = actors.kero,
                tokens = {
                    { type = "talk", actor = actors.kero, text = "Kero speaks" },
                }
            },
        }
        local result = BUILDER.build(tokens, { spot_newlines = 1.5 })

        expect(result:find("\\p%[0%]Sakura speaks")):toBeTruthy()
        expect(result:find("\\n%[150%]\\p%[1%]Kero speaks")):toBeTruthy()
    end)

    test("clear_spot() → talk() → デフォルトspot(0)への復帰を確認", function()
        local BUILDER, actors = setup()

        local tokens = {
            { type = "spot",      actor = actors.sakura, spot = 5 },
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "talk", actor = actors.sakura, text = "At spot 5" },
                }
            },
            { type = "clear_spot" },
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "talk", actor = actors.sakura, text = "Back to default" },
                }
            },
        }
        local result = BUILDER.build(tokens, {})

        expect(result:find("\\p%[5%]")):toBeTruthy()
        expect(result:find("\\p%[0%]")):toBeTruthy() -- clear後はデフォルト
    end)
end)

-- ============================================================================
-- persist-spot-position: build()シグネチャ拡張テスト (Task 5.3, 5.4, 5.5)
-- ============================================================================

describe("SAKURA_BUILDER - persist-spot-position: 純粋関数性テスト (Task 5.3)", function()
    test("入力テーブルがspotトークンで直接変更されることを確認", function()
        local BUILDER, actors = setup()

        local input_spots = {}
        local tokens = {
            { type = "spot", actor = actors.sakura, spot = 0 },
            { type = "spot", actor = actors.kero,   spot = 1 },
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "talk", actor = actors.sakura, text = "Hello" },
                }
            },
        }

        BUILDER.build(tokens, {}, input_spots)

        -- 入力テーブルが直接変更されている
        expect(input_spots["さくら"]):toBe(0)
        expect(input_spots["うにゅう"]):toBe(1)
    end)

    test("nil入力時に空テーブルとして扱われることを確認", function()
        local BUILDER, actors = setup()

        local tokens = {
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "talk", actor = actors.sakura, text = "Hello" },
                }
            },
        }

        -- actor_spots = nil で呼び出し
        local result = BUILDER.build(tokens, {}, nil)

        expect(result:find("\\p%[0%]")):toBeTruthy()
    end)

    test("spotトークンでinput_spotsが直接変更される", function()
        local BUILDER, actors = setup()

        local input_spots = {}
        local tokens = {
            { type = "spot", actor = actors.sakura, spot = 0 },
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "talk", actor = actors.sakura, text = "Hello" },
                }
            },
        }

        local result = BUILDER.build(tokens, {}, input_spots)

        expect(type(result)):toBe("string")
        expect(input_spots["さくら"]):toBe(0)
    end)

    test("後方互換性: actor_spots省略時も正常動作", function()
        local BUILDER, actors = setup()

        local tokens = {
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "talk", actor = actors.sakura, text = "Hello" },
                }
            },
        }

        -- 第3引数を省略しても動作
        local result = BUILDER.build(tokens, {})

        expect(result:find("Hello")):toBeTruthy()
    end)
end)

describe("SAKURA_BUILDER - persist-spot-position: clear_spotトークン処理 (Task 5.4)", function()
    test("clear_spotトークンで入力のactor_spotsがリセットされる", function()
        local BUILDER, actors = setup()

        local input_spots = { ["さくら"] = 5, ["うにゅう"] = 3 }
        local tokens = {
            { type = "clear_spot" },
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "talk", actor = actors.sakura, text = "Reset" },
                }
            },
        }

        local result = BUILDER.build(tokens, {}, input_spots)

        -- clear_spot後はデフォルトspot(0)を使用
        expect(result:find("\\p%[0%]")):toBeTruthy()
        -- 入力テーブルのエントリが直接クリアされている
        expect(input_spots["さくら"]):toBe(nil)
        expect(input_spots["うにゅう"]):toBe(nil)
    end)

    test("直接変更: clear_spotで入力テーブルのエントリがクリアされる", function()
        local BUILDER = setup()

        local input_spots = { ["さくら"] = 5, ["うにゅう"] = 3 }
        local tokens = {
            { type = "clear_spot" },
        }

        BUILDER.build(tokens, {}, input_spots)

        -- 入力テーブルが直接変更される(直接変更方式)
        expect(input_spots["さくら"]):toBe(nil)
        expect(input_spots["うにゅう"]):toBe(nil)
    end)
end)

describe("SAKURA_BUILDER - persist-spot-position: spotトークン処理 (Task 5.5)", function()
    test("spotトークンで入力のactor_spotsが正しく更新される", function()
        local BUILDER, actors = setup()

        local input_spots = { ["さくら"] = 0 }
        local tokens = {
            { type = "spot", actor = actors.sakura, spot = 0 },
            { type = "spot", actor = actors.kero,   spot = 1 },
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "talk", actor = actors.sakura, text = "Hi" },
                }
            },
        }

        BUILDER.build(tokens, {}, input_spots)

        expect(input_spots["さくら"]):toBe(0)
        expect(input_spots["うにゅう"]):toBe(1)
    end)

    test("入力actor_spotsの値を引き継いでスポットタグが出力される", function()
        local BUILDER, actors = setup()

        -- 前回のスポット状態を入力として渡す
        local input_spots = { ["さくら"] = 0, ["うにゅう"] = 1 }
        local tokens = {
            -- spotトークンなし(前回の値を引き継ぐ)
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "talk", actor = actors.sakura, text = "Still here" },
                }
            },
            {
                type = "actor",
                actor = actors.kero,
                tokens = {
                    { type = "talk", actor = actors.kero, text = "Me too" },
                }
            },
        }

        local result = BUILDER.build(tokens, { spot_newlines = 1.5 }, input_spots)

        -- 前回のスポット値が引き継がれている
        expect(result:find("\\p%[0%]")):toBeTruthy()
        expect(result:find("\\p%[1%]")):toBeTruthy()
        expect(result:find("\\n%[150%]")):toBeTruthy() -- spot変更時の段落改行

        -- 入力テーブルも前回の値を保持(直接変更だが値は変わらない)
        expect(input_spots["さくら"]):toBe(0)
        expect(input_spots["うにゅう"]):toBe(1)
    end)
end)

-- ============================================================================
-- act-sakura-script-method: sakura_scriptトークン処理 (Task 4.3)
-- ============================================================================

describe("SAKURA_BUILDER - sakura_scriptトークン処理", function()
    test("sakura_scriptトークンがtalk_to_scriptで変換される", function()
        local BUILDER, actors = setup()

        local tokens = {
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "sakura_script", actor = actors.sakura, text = "\\n" },
                }
            },
        }
        local result = BUILDER.build(tokens, {})

        -- さくらスクリプトタグがそのまま出力に含まれる
        expect(result:find("\\n")):toBeTruthy()
        expect(result:sub(-2)):toBe("\\e")
    end)

    test("talk + sakura_script混合がそれぞれ処理される", function()
        local BUILDER, actors = setup()

        local tokens = {
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "talk",          actor = actors.sakura, text = "Hello" },
                    { type = "sakura_script", actor = actors.sakura, text = "\\w9" },
                    { type = "talk",          actor = actors.sakura, text = "World" },
                }
            },
        }
        local result = BUILDER.build(tokens, {})

        expect(result:find("Hello")):toBeTruthy()
        expect(result:find("\\w9")):toBeTruthy()
        expect(result:find("World")):toBeTruthy()
    end)

    test("raw_scriptトークンの既存動作が変化していない", function()
        local BUILDER, actors = setup()

        local tokens = {
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "raw_script", text = "\\![open,notepad]" },
                }
            },
        }
        local result = BUILDER.build(tokens, {})

        expect(result:find("\\!%[open,notepad%]")):toBeTruthy()
    end)
end)

-- ============================================================================
-- choice-definition-dsl: choice/choice_timeoutトークン処理 (Task 4.1)
-- ============================================================================

describe("SAKURA_BUILDER - choiceトークン処理", function()
    test("choiceトークンを \\![*]\\q[display,target] に変換する", function()
        local BUILDER, actors = setup()

        local tokens = {
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "choice", display = "こんにちは", target = "挨拶" },
                }
            },
        }
        local result = BUILDER.build(tokens, {})

        expect(result:find("\\!%[%*%]\\q%[こんにちは,挨拶%]")):toBeTruthy()
        expect(result:sub(-2)):toBe("\\e")
    end)

    test("displayテキスト内の ] をエスケープする", function()
        local BUILDER, actors = setup()

        local tokens = {
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "choice", display = "yes]no", target = "分岐" },
                }
            },
        }
        local result = BUILDER.build(tokens, {})

        expect(result:find("\\!%[%*%]\\q%[yes\\]no,分岐%]")):toBeTruthy()
    end)

    test("displayテキスト内の , をエスケープする", function()
        local BUILDER, actors = setup()

        local tokens = {
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "choice", display = "A,B", target = "分岐" },
                }
            },
        }
        local result = BUILDER.build(tokens, {})

        expect(result:find("\\!%[%*%]\\q%[A\\,B,分岐%]")):toBeTruthy()
    end)

    test("displayテキスト内の \\ をエスケープする", function()
        local BUILDER, actors = setup()

        local tokens = {
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "choice", display = "a\\b", target = "分岐" },
                }
            },
        }
        local result = BUILDER.build(tokens, {})

        expect(result:find("\\!%[%*%]\\q%[a\\\\b,分岐%]")):toBeTruthy()
    end)

    test("displayテキスト内の複合デリミタをエスケープする(], ,, \\", function()
        local BUILDER, actors = setup()

        local tokens = {
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "choice", display = "yes]or,no\\maybe", target = "挨拶" },
                }
            },
        }
        local result = BUILDER.build(tokens, {})

        -- \ → \\, ] → \], , → \,
        expect(result:find("\\!%[%*%]\\q%[yes\\]or\\,no\\\\maybe,挨拶%]")):toBeTruthy()
    end)

    test("targetテキスト内のデリミタもエスケープされる", function()
        local BUILDER, actors = setup()

        local tokens = {
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "choice", display = "はい", target = "a],b\\c" },
                }
            },
        }
        local result = BUILDER.build(tokens, {})

        expect(result:find("\\!%[%*%]\\q%[はい,a\\]\\,b\\\\c%]")):toBeTruthy()
    end)
end)

describe("SAKURA_BUILDER - choice_timeoutトークン処理", function()
    test("choice_timeoutトークンを \\![set,choicetimeout,<ms>] に変換する", function()
        local BUILDER, actors = setup()

        local tokens = {
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "choice_timeout", seconds = 5 },
                }
            },
        }
        local result = BUILDER.build(tokens, {})

        expect(result:find("\\!%[set,choicetimeout,5000%]")):toBeTruthy()
        expect(result:sub(-2)):toBe("\\e")
    end)

    test("choice_timeout seconds=nil の場合は 0 を出力する", function()
        local BUILDER, actors = setup()

        local tokens = {
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "choice_timeout", seconds = nil },
                }
            },
        }
        local result = BUILDER.build(tokens, {})

        expect(result:find("\\!%[set,choicetimeout,0%]")):toBeTruthy()
    end)

    test("choice_timeout 小数秒をミリ秒に変換し floor する", function()
        local BUILDER, actors = setup()

        local tokens = {
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "choice_timeout", seconds = 2.5 },
                }
            },
        }
        local result = BUILDER.build(tokens, {})

        expect(result:find("\\!%[set,choicetimeout,2500%]")):toBeTruthy()
    end)
end)

describe("SAKURA_BUILDER - choice統合シナリオ", function()
    test("talk + choice_timeout + choice の複合トークンが正しく連結される", function()
        local BUILDER, actors = setup()

        local tokens = {
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "talk",           actor = actors.sakura, text = "選んでね" },
                    { type = "choice_timeout", seconds = 10 },
                    { type = "choice",         display = "はい",  target = "yes_route" },
                    { type = "choice",         display = "いいえ", target = "no_route" },
                }
            },
        }
        local result = BUILDER.build(tokens, {})

        expect(result:find("選んでね")):toBeTruthy()
        expect(result:find("\\!%[set,choicetimeout,10000%]")):toBeTruthy()
        expect(result:find("\\!%[%*%]\\q%[はい,yes_route%]")):toBeTruthy()
        expect(result:find("\\!%[%*%]\\q%[いいえ,no_route%]")):toBeTruthy()
        expect(result:sub(-2)):toBe("\\e")
    end)
end)

-- ============================================================================
-- sakura-builder-string-buffer: バッファ化検証 (Task 3.1)
-- 設計 Testing Strategy「Integration Tests」項2-3 / 要件 2.4, 3.4, 3.5, 4.1, 4.2
-- 既に Task 2.1 で実装済みの挙動(バッファ化・buffer_factory 注入)を
-- 実行パスで検証する追加テスト。
-- ============================================================================

describe("SAKURA_BUILDER - string-buffer: 空入力 (Task 3.1)", function()
    -- 要件 3.4: 空の grouped_tokens に対しては \e のみを返す
    test("空の grouped_tokens で build() が \\e のみを返す", function()
        local BUILDER = setup()

        local result = BUILDER.build({}, {})

        expect(result):toBe("\\e")
    end)

    test("空入力はネイティブ/フォールバック両経路で \\e のみを返す", function()
        local BUILDER = setup()
        local buf = require("pasta.buf")

        local native = BUILDER.build({}, {})
        local fallback = BUILDER.build({}, { buffer_factory = buf.new_fallback })

        expect(native):toBe("\\e")
        expect(fallback):toBe("\\e")
        -- 両経路でバイト一致
        expect(fallback):toBe(native)
    end)
end)

describe("SAKURA_BUILDER - string-buffer: フォールバック実走バイト一致 (Task 3.1)", function()
    -- 要件 2.4 / 3.5 / 4.2:
    -- (i) 既定(native buf)と (ii) buffer_factory=buf.new_fallback の出力がバイト一致すること。
    -- spot 状態を使うため、両呼び出しで独立した actor_spots テーブルを渡し副作用を分離する。

    -- 代表的な grouped_tokens を生成するファクトリ。
    -- 呼び出しごとに新しいテーブルを返し、2回の build 間で入力の共有・汚染を避ける。
    local function make_complex_tokens(actors)
        return {
            { type = "spot", actor = actors.sakura, spot = 0 },
            { type = "spot", actor = actors.kero,   spot = 1 },
            {
                type = "actor",
                actor = actors.sakura,
                tokens = {
                    { type = "talk",    actor = actors.sakura, text = "こんにちは。今日は、いい天気。" },
                    { type = "surface", id = 5 },
                    { type = "wait",    ms = 100 },
                    { type = "newline", n = 2 },
                }
            },
            {
                type = "actor",
                actor = actors.kero,
                tokens = {
                    { type = "talk",          actor = actors.kero, text = "Hi there" },
                    { type = "sakura_script", actor = actors.kero, text = "\\w9" },
                    { type = "clear" },
                    { type = "choice_timeout", seconds = 5 },
                    { type = "choice",        display = "はい]", target = "yes,route" },
                    { type = "raw_script",    text = "\\![open,calendar]" },
                }
            },
        }
    end

    test("複合入力(spot/talk/surface/wait/newline/choice 等)でバイト一致する", function()
        local BUILDER, actors = setup()
        local buf = require("pasta.buf")

        local config = { spot_newlines = 1.5 }

        -- (i) 既定(native buf)。独立した actor_spots を渡す
        local native_spots = {}
        local native = BUILDER.build(make_complex_tokens(actors), config, native_spots)

        -- (ii) フォールバック注入。同じ config・独立した actor_spots
        local fb_spots = {}
        local fb_config = { spot_newlines = 1.5, buffer_factory = buf.new_fallback }
        local fallback = BUILDER.build(make_complex_tokens(actors), fb_config, fb_spots)

        -- 完全一致(バイト一致)
        expect(fallback):toBe(native)
        -- vacuous でないこと(実際に内容を持つ出力である)の確認
        expect(#native > 2):toBeTruthy()
        expect(native:sub(-2)):toBe("\\e")
        -- 副作用(actor_spots 更新)も両経路で一致
        expect(fb_spots["さくら"]):toBe(native_spots["さくら"])
        expect(fb_spots["うにゅう"]):toBe(native_spots["うにゅう"])
    end)

    test("spot 変更・改行を含む入力でバイト一致する", function()
        local BUILDER, actors = setup()
        local buf = require("pasta.buf")

        local function make_tokens()
            return {
                { type = "spot", actor = actors.sakura, spot = 0 },
                { type = "spot", actor = actors.kero,   spot = 2 },
                {
                    type = "actor",
                    actor = actors.sakura,
                    tokens = {
                        { type = "talk", actor = actors.sakura, text = "おはよう。" },
                    }
                },
                {
                    type = "actor",
                    actor = actors.kero,
                    tokens = {
                        { type = "talk", actor = actors.kero, text = "おはよ、ございます。" },
                        { type = "newline", n = 3 },
                    }
                },
                { type = "clear_spot" },
                {
                    type = "actor",
                    actor = actors.sakura,
                    tokens = {
                        { type = "talk", actor = actors.sakura, text = "またね。" },
                    }
                },
            }
        end

        local config = { spot_newlines = 2.0 }

        local native = BUILDER.build(make_tokens(), config, {})
        local fallback = BUILDER.build(make_tokens(), { spot_newlines = 2.0, buffer_factory = buf.new_fallback }, {})

        expect(fallback):toBe(native)
        -- spot 変更時の段落改行 \n[200] が実在し(出力が空でない=vacuousでない)
        expect(native:find("\\n%[200%]")):toBeTruthy()
        expect(native:sub(-2)):toBe("\\e")
    end)

    test("choice/raw_script/sakura_script を含む単一actor入力でバイト一致する", function()
        local BUILDER, actors = setup()
        local buf = require("pasta.buf")

        local function make_tokens()
            return {
                {
                    type = "actor",
                    actor = actors.sakura,
                    tokens = {
                        { type = "talk",          actor = actors.sakura, text = "選んでね" },
                        { type = "choice_timeout", seconds = 10 },
                        { type = "choice",        display = "A,B]C\\D", target = "route]1" },
                        { type = "sakura_script", actor = actors.sakura, text = "\\_w[200]" },
                        { type = "raw_script",    text = "\\![open,notepad]" },
                    }
                },
            }
        end

        local native = BUILDER.build(make_tokens(), {}, {})
        local fallback = BUILDER.build(make_tokens(), { buffer_factory = buf.new_fallback }, {})

        expect(fallback):toBe(native)
        -- 出力が実際にエスケープ済みchoiceを含む(vacuousでない)
        expect(native:find("\\!%[%*%]\\q%[A\\,B\\]C\\\\D,route\\]1%]")):toBeTruthy()
        expect(native:sub(-2)):toBe("\\e")
    end)
end)