pasta_lua 0.2.4

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
-- SHIORI Act module tests
-- Tests for pasta.shiori.act module - sakura script builder
local describe = require("lua_test.test").describe
local test = require("lua_test.test").test
local expect = require("lua_test.test").expect

-- Mock actors for testing
local function create_mock_actors()
    return {
        sakura = { name = "さくら", spot = "sakura" },
        kero = { name = "うにゅう", spot = "kero" },
        char2 = { name = "キャラ2", spot = "char2" },
    }
end

-- Create mock context (full CTX-like structure)
local function create_mock_ctx()
    local ACTOR = require("pasta.actor")
    local sakura = ACTOR.get_or_create("さくら")
    sakura.spot = "sakura"
    local kero = ACTOR.get_or_create("うにゅう")
    kero.spot = "kero"

    return {
        actors = {
            sakura = sakura,
            kero = kero,
        }
    }
end

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

-- Test inheritance from pasta.act
describe("SHIORI_ACT - inheritance", function()
    test("inherits ACT.IMPL methods", function()
        local act = new_act()

        -- raw_script() is inherited from ACT_IMPL
        act:raw_script("\\e")
        expect(#act.token):toBe(1)
        expect(act.token[1].type):toBe("raw_script")
    end)

    test("has IMPL field for further inheritance", function()
        local SHIORI_ACT = require("pasta.shiori.act")
        -- Check IMPL exists and is a table (avoid deep inspection)
        expect(type(SHIORI_ACT.IMPL)):toBe("table")
        -- Check __index is set (use rawget to avoid metatable traversal)
        expect(rawget(SHIORI_ACT.IMPL, "__index") ~= nil):toBe(true)
    end)

    test("inherits word() method", function()
        local act = new_act()

        -- word() method should be accessible (returns nil for unknown word)
        local result = act:word("unknown_word")
        expect(result):toBe(nil)
    end)

    test("supports actor proxy (act.sakura:talk)", function()
        local SHIORI_ACT = require("pasta.shiori.act")
        local ctx = create_mock_ctx()
        local act = SHIORI_ACT.new(ctx.actors)

        -- act.sakura should create a proxy that redirects to act:talk(sakura, text)
        act.sakura:talk("Hello via proxy")
        local result = act:build()

        -- Should contain scope tag and text (same as direct call)
        expect(result:find("\\p%[0%]")):toBeTruthy()
        expect(result:find("Hello via proxy")):toBeTruthy()
        expect(result:sub(-2)):toBe("\\e")
    end)

    test("actor proxy supports method chaining", function()
        local SHIORI_ACT = require("pasta.shiori.act")
        local ctx = create_mock_ctx()
        local act = SHIORI_ACT.new(ctx.actors)

        -- 新アーキテクチャ: set_spot()でスポット位置を明示的に設定
        act:set_spot("sakura", 0)
        act:set_spot("kero", 1)

        -- Proxy talk returns nil, but act methods can chain
        act.sakura:talk("First")
        act:surface(5)
        act.kero:talk("Second")
        local result = act:build()

        expect(result:find("\\p%[0%]")):toBeTruthy()
        expect(result:find("First")):toBeTruthy()
        expect(result:find("\\s%[5%]")):toBeTruthy()
        expect(result:find("\\p%[1%]")):toBeTruthy()
        expect(result:find("Second")):toBeTruthy()
    end)
end)

-- Test talk() method override
describe("SHIORI_ACT - talk()", function()
    test("appends scope tag on first actor", function()
        local act, actors = new_act()

        act:talk(actors.sakura, "Hello")
        local result = act:build()

        -- Should contain: \p[0] (default spot) + Hello + \e
        expect(result:find("\\p%[0%]")):toBeTruthy()
        expect(result:find("Hello")):toBeTruthy()
        expect(result:sub(-2)):toBe("\\e")
    end)

    test("appends scope tag on actor switch", function()
        local act, actors = new_act()

        -- 新アーキテクチャ: set_spot()でスポット位置を明示的に設定
        act:set_spot("sakura", 0)
        act:set_spot("kero", 1)

        act:talk(actors.sakura, "Hello")
        act:talk(actors.kero, "Hi")
        local result = act:build()

        -- Should contain both spot tags (SSP compliant)
        expect(result:find("\\p%[0%]")):toBeTruthy()
        expect(result:find("\\p%[1%]")):toBeTruthy()
    end)

    test("does not append scope tag on same actor", function()
        local act, actors = new_act()

        act:talk(actors.sakura, "Hello")
        act:talk(actors.sakura, "World")
        local result = act:build()

        -- \p[0] should appear only once
        local _, count = result:gsub("\\p%[0%]", "")
        expect(count):toBe(1)
    end)

    test("uses \\p[N] for char2+ actors", function()
        local act, actors = new_act()

        -- 新アーキテクチャ: set_spot()でスポット位置を明示的に設定
        act:set_spot("char2", 2)

        act:talk(actors.char2, "Third character")
        local result = act:build()

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

    test("adds newline after spot switch", function()
        local act, actors = new_act()

        -- 新アーキテクチャ: set_spot()でスポット位置を明示的に設定
        act:set_spot("sakura", 0)
        act:set_spot("kero", 1)

        act:talk(actors.sakura, "Hello")
        act:talk(actors.kero, "Hi")
        local result = act:build()

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

    test("supports method chaining", function()
        local act, actors = new_act()

        local returned = act:talk(actors.sakura, "Hello")
        expect(returned):toBe(act)
    end)

    test("also updates token buffer (parent behavior)", function()
        local act, actors = new_act()

        act:talk(actors.sakura, "Hello")
        expect(#act.token):toBeGraterThan(0)
    end)
end)

-- Test surface() method (グループ化対応版: surfaceはtalkのactorグループ内で処理される)
describe("SHIORI_ACT - surface()", function()
    test("appends surface tag with number", function()
        local act, actors = new_act()

        -- グループ化後: surfaceはtalkのactorグループ内で処理される
        act:talk(actors.sakura, "")
        act:surface(5)
        local result = act:build()

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

    test("appends surface tag with alias string", function()
        local act, actors = new_act()

        -- グループ化後: surfaceはtalkのactorグループ内で処理される
        act:talk(actors.sakura, "")
        act:surface("smile")
        local result = act:build()

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

    test("supports method chaining", function()
        local act = new_act()

        local returned = act:surface(5)
        expect(returned):toBe(act)
    end)
end)

-- Test wait() method (グループ化対応版: waitはtalkのactorグループ内で処理される)
describe("SHIORI_ACT - wait()", function()
    test("appends wait tag", function()
        local act, actors = new_act()

        act:talk(actors.sakura, "")
        act:wait(500)
        local result = act:build()

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

    test("handles negative values as 0", function()
        local act, actors = new_act()

        act:talk(actors.sakura, "")
        act:wait(-100)
        local result = act:build()

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

    test("truncates float to integer", function()
        local act, actors = new_act()

        act:talk(actors.sakura, "")
        act:wait(500.7)
        local result = act:build()

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

    test("supports method chaining", function()
        local act = new_act()

        local returned = act:wait(500)
        expect(returned):toBe(act)
    end)
end)

-- Test newline() method (グループ化対応版: newlineはtalkのactorグループ内で処理される)
describe("SHIORI_ACT - newline()", function()
    test("appends single newline by default", function()
        local act, actors = new_act()

        act:talk(actors.sakura, "")
        act:newline()
        local result = act:build()

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

    test("appends multiple newlines", function()
        local act, actors = new_act()

        act:talk(actors.sakura, "")
        act:newline(3)
        local result = act:build()

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

    test("does nothing for n < 1", function()
        local act, actors = new_act()

        act:talk(actors.sakura, "")
        act:newline(0)
        act:newline(-1)
        local result = act:build()

        -- n=0やn=-1のnewlineは出力されない
        -- talkが空文字列なので、スポットタグ + \e のみ
        expect(result:find("\\p%[0%]")):toBeTruthy()
    end)

    test("supports method chaining", function()
        local act = new_act()

        local returned = act:newline()
        expect(returned):toBe(act)
    end)
end)

-- Test clear() method (グループ化対応版: clearはtalkのactorグループ内で処理される)
describe("SHIORI_ACT - clear()", function()
    test("appends clear tag", function()
        local act, actors = new_act()

        act:talk(actors.sakura, "")
        act:clear()
        local result = act:build()

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

    test("supports method chaining", function()
        local act = new_act()

        local returned = act:clear()
        expect(returned):toBe(act)
    end)
end)

-- Test build() method
describe("SHIORI_ACT - build()", function()
    test("returns nil for empty buffer (act-build-early-return)", function()
        local act = new_act()

        local result = act:build()

        expect(result):toBe(nil)
    end)

    test("appends \\e to end", function()
        local act, actors = new_act()

        act:talk(actors.sakura, "")
        act:surface(5):wait(100)
        local result = act:build()

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

    test("auto-resets after build", function()
        local act, actors = new_act()

        act:talk(actors.sakura, "test")
        act:surface(5)
        local result1 = act:build()
        -- After build(), buffer is auto-reset
        local result2 = act:build()

        expect(result1:find("\\s%[5%]")):toBeTruthy()
        expect(result2):toBe(nil) -- nil after auto-reset (act-build-early-return)
    end)
end)

-- Test reset() method - REMOVED (reset is no longer a public API)
-- The reset functionality is now integrated into build() method.
-- build() automatically resets the token buffer and spot state.

-- Test talk_to_script conversion (wait insertion)
describe("SHIORI_ACT - talk_to_script変換", function()
    test("通常テキストがそのまま出力される(デフォルト設定)", function()
        local act, actors = new_act()

        act:talk(actors.sakura, "Hello")
        local result = act:build()

        -- デフォルト設定ではウェイトタグなし(effective_wait = 0)
        expect(result:find("Hello")):toBeTruthy()
    end)

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

        act:talk(actors.sakura, "あ。")
        local result = act:build()

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

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

        act:talk(actors.sakura, "あ、")
        local result = act:build()

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

-- E2E scenario test
describe("SHIORI_ACT - E2E scenario", function()
    test("complex script generation", function()
        local act, actors = new_act()

        -- 新アーキテクチャ: set_spot()でスポット位置を明示的に設定
        act:set_spot("sakura", 0)
        act:set_spot("kero", 1)

        act:talk(actors.sakura, "こんにちは")
            :surface(5)
            :wait(500)
            :talk(actors.kero, "やあ")
            :clear()

        local result = act:build()

        -- Verify structure (SSP compliant: \p[ID] format)
        expect(result:find("\\p%[0%]")):toBeTruthy()   -- sakura spot
        expect(result:find("こんにちは")):toBeTruthy()
        expect(result:find("\\s%[5%]")):toBeTruthy()   -- surface
        expect(result:find("\\w%[500%]")):toBeTruthy() -- wait
        expect(result:find("\\p%[1%]")):toBeTruthy()   -- kero spot
        expect(result:find("やあ")):toBeTruthy()
        expect(result:find("\\c")):toBeTruthy()        -- clear
        expect(result:sub(-2)):toBe("\\e")             -- end
    end)

    test("multiple rounds (build auto-resets)", function()
        local act, actors = new_act()

        -- First round
        act:talk(actors.sakura, "First")
        local result1 = act:build()
        expect(result1:find("First")):toBeTruthy()

        -- Second round (build auto-resets, so no manual reset needed)
        -- 新アーキテクチャ: actor_spotsもbuild()ごとにリセットされる
        act:set_spot("kero", 1)
        act:talk(actors.kero, "Second")
        local result2 = act:build()

        expect(result2:find("First")):toBeFalsy()     -- First should be cleared by auto-reset
        expect(result2:find("Second")):toBeTruthy()
        expect(result2:find("\\p%[1%]")):toBeTruthy() -- kero spot
    end)
end)

-- Test act.req field
describe("SHIORI_ACT - req field", function()
    test("stores req parameter in act.req", function()
        local req = {
            id = "OnTest",
            method = "get",
            version = 30,
            reference = { "ref0", "ref1" },
        }

        local act = new_act(req)

        expect(act.req):toBe(req)
        expect(act.req.id):toBe("OnTest")
        expect(act.req.method):toBe("get")
        expect(act.req.version):toBe(30)
    end)

    test("req reference is same object (not a copy)", function()
        local req = {
            id = "OnBoot",
            reference = { "value0" },
        }

        local act = new_act(req)

        -- Same reference check
        expect(act.req == req):toBe(true)
        expect(act.req.reference[1]):toBe("value0")
    end)

    test("act.req.date contains date info when provided", function()
        local req = {
            id = "OnSecondChange",
            date = {
                unix = 1704067200,
                hour = 12,
                minute = 0,
            },
        }

        local act = new_act(req)

        expect(act.req.date.unix):toBe(1704067200)
        expect(act.req.date.hour):toBe(12)
    end)
end)

-- ============================================================================
-- Task 4.1: transfer_date_to_var() テスト
-- Requirements: 1.1, 1.2, 1.3, 1.4
-- ============================================================================

describe("SHIORI_ACT - transfer_date_to_var()", function()
    -- 正常系: 全フィールド転記確認(英語・数値型)
    test("transfers all date fields from req.date to var", function()
        local req = {
            id = "OnSecondChange",
            date = {
                year = 2026,
                month = 2,
                day = 1,
                hour = 14,
                min = 37,
                sec = 45,
                wday = 0,
                unix = 1769932665,
                ns = 123456789,
                yday = 32,
            },
        }

        local act = new_act(req)
        act:transfer_date_to_var()

        -- 英語フィールド(数値型)確認
        expect(act.var.year):toBe(2026)
        expect(act.var.month):toBe(2)
        expect(act.var.day):toBe(1)
        expect(act.var.hour):toBe(14)
        expect(act.var.min):toBe(37)
        expect(act.var.sec):toBe(45)
        expect(act.var.wday):toBe(0)

        -- 転記対象外の確認(unix, ns, yday は転記されない)
        expect(act.var.unix):toBe(nil)
        expect(act.var.ns):toBe(nil)
        expect(act.var.yday):toBe(nil)
    end)

    -- req 不在時の安全終了
    test("returns self safely when req is nil", function()
        local act = new_act(nil)
        local result = act:transfer_date_to_var()

        -- 何もせず正常終了、メソッドチェーン用に self を返す
        expect(result):toBe(act)
    end)

    -- req.date 不在時の安全終了
    test("returns self safely when req.date is nil", function()
        local req = { id = "OnSecondChange" }

        local act = new_act(req)
        local result = act:transfer_date_to_var()

        -- 何もせず正常終了、メソッドチェーン用に self を返す
        expect(result):toBe(act)
    end)

    -- 日本語変数マッピング確認(年月日時分秒)
    test("maps Japanese variable names with formatted strings", function()
        local req = {
            id = "OnSecondChange",
            date = {
                year = 2026,
                month = 2,
                day = 1,
                hour = 14,
                min = 37,
                sec = 45,
                wday = 0,
            },
        }

        local act = new_act(req)
        act:transfer_date_to_var()

        -- 日本語変数(文字列型)確認
        expect(act.var[""]):toBe("2026年")
        expect(act.var[""]):toBe("2月")
        expect(act.var[""]):toBe("1日")
        expect(act.var[""]):toBe("14時")
        expect(act.var[""]):toBe("37分")
        expect(act.var[""]):toBe("45秒")
    end)

    -- 曜日変換確認(wday 0-6 全パターン)
    test("converts wday to Japanese and English weekday names", function()
        local weekdays_ja = { "日曜日", "月曜日", "火曜日", "水曜日", "木曜日", "金曜日", "土曜日" }
        local weekdays_en = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }

        for wday = 0, 6 do
            local req = {
                id = "OnSecondChange",
                date = { year = 2026, month = 2, day = 1, hour = 0, min = 0, sec = 0, wday = wday },
            }
            local act = new_act(req)
            act:transfer_date_to_var()

            expect(act.var["曜日"]):toBe(weekdays_ja[wday + 1])
            expect(act.var.week):toBe(weekdays_en[wday + 1])
        end
    end)

    -- 12時間制変換確認(hour 0, 1, 11, 12, 13, 23 のケース)
    test("converts hour to 12-hour format with 深夜0時/正午 special cases", function()
        local test_cases = {
            { hour = 0, expected = "深夜0時" },
            { hour = 1, expected = "午前1時" },
            { hour = 11, expected = "午前11時" },
            { hour = 12, expected = "正午" },
            { hour = 13, expected = "午後1時" },
            { hour = 23, expected = "午後11時" },
        }

        for _, tc in ipairs(test_cases) do
            local req = {
                id = "OnSecondChange",
                date = { year = 2026, month = 2, day = 1, hour = tc.hour, min = 0, sec = 0, wday = 0 },
            }
            local act = new_act(req)
            act:transfer_date_to_var()

            expect(act.var["時12"]):toBe(tc.expected)
        end
    end)

    -- メソッドチェーン用に self を返す
    test("returns self for method chaining", function()
        local req = {
            id = "OnSecondChange",
            date = { year = 2026, month = 2, day = 1, hour = 14, min = 37, sec = 45, wday = 0 },
        }

        local act = new_act(req)
        local result = act:transfer_date_to_var()

        expect(result):toBe(act)
    end)
end)