pasta_lua 0.2.1

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
-- virtual_dispatcher_spec.lua
-- Lua-side BDD tests for pasta.shiori.event.virtual_dispatcher module
local describe = require("lua_test.test").describe
local test = require("lua_test.test").test
local expect = require("lua_test.test").expect

-- Helper: create mock act object with req field
local function create_mock_act(req)
    return { req = req }
end

describe("pasta.shiori.event.virtual_dispatcher", function()
    local dispatcher

    -- before each: reset module state
    local function setup()
        dispatcher = require("pasta.shiori.event.virtual_dispatcher")
        dispatcher._reset()
        -- Set up mock scene executor for testing
        dispatcher._set_scene_executor(function(event_name)
            if event_name == "OnHour" then
                return "hour_result"
            elseif event_name == "OnTalk" then
                return "talk_result"
            end
            return nil
        end)
    end

    test("module is loadable", function()
        setup()
        expect(type(dispatcher)):toBe("table")
    end)

    test("exports required functions", function()
        setup()
        expect(type(dispatcher.dispatch)):toBe("function")
        expect(type(dispatcher.check_hour)):toBe("function")
        expect(type(dispatcher.check_talk)):toBe("function")
        expect(type(dispatcher._reset)):toBe("function")
        expect(type(dispatcher._get_internal_state)):toBe("function")
        expect(type(dispatcher._set_scene_executor)):toBe("function")
    end)

    test("initial state is zeroed", function()
        setup()
        local state = dispatcher._get_internal_state()
        expect(state.next_hour_unix):toBe(0)
        expect(state.next_talk_time):toBe(0)
        expect(state.cached_config):toBe(nil)
    end)
end)

describe("dispatch function", function()
    local dispatcher

    local function setup()
        dispatcher = require("pasta.shiori.event.virtual_dispatcher")
        dispatcher._reset()
        dispatcher._set_scene_executor(function(event_name)
            return event_name .. "_result"
        end)
    end

    test("returns nil when req.date is missing", function()
        setup()
        local act = create_mock_act({ id = "OnSecondChange", status = "idle" })
        local result = dispatcher.dispatch(act)
        expect(result):toBe(nil)
    end)

    test("returns nil when req.date is nil", function()
        setup()
        local act = create_mock_act({ id = "OnSecondChange", status = "idle", date = nil })
        local result = dispatcher.dispatch(act)
        expect(result):toBe(nil)
    end)
end)

describe("check_hour function", function()
    local dispatcher

    local function setup()
        dispatcher = require("pasta.shiori.event.virtual_dispatcher")
        dispatcher._reset()
        dispatcher._set_scene_executor(function(event_name)
            if event_name == "OnHourOther" then
                return coroutine.create(function() return "hour_result" end)
            end
            return nil
        end)
    end

    test("first call initializes next_hour_unix and returns nil", function()
        setup()
        local act = create_mock_act({
            id = "OnSecondChange",
            status = "idle",
            date = { unix = 1702648800 } -- 14:00:00
        })
        local result = dispatcher.check_hour(act)
        local state = dispatcher._get_internal_state()

        expect(result):toBe(nil)
        expect(state.next_hour_unix > 0):toBe(true)
    end)

    test("fires at hour boundary", function()
        setup()
        -- Initialize
        local act1 = create_mock_act({ id = "OnSecondChange", status = "idle", date = { unix = 1702648800 } })
        dispatcher.check_hour(act1)

        -- At next hour
        local act2 = create_mock_act({ id = "OnSecondChange", status = "idle", date = { unix = 1702652400, hour = 15 } })
        local result = dispatcher.check_hour(act2)

        expect(type(result)):toBe("thread")
    end)

    test("returns nil before hour boundary", function()
        setup()
        -- Initialize
        local act1 = create_mock_act({ id = "OnSecondChange", status = "idle", date = { unix = 1702648800 } })
        dispatcher.check_hour(act1)

        -- Not yet at next hour
        local act2 = create_mock_act({ id = "OnSecondChange", status = "idle", date = { unix = 1702649000 } })
        local result = dispatcher.check_hour(act2)

        expect(result):toBe(nil)
    end)
end)

describe("check_talk function", function()
    local dispatcher

    local function setup()
        dispatcher = require("pasta.shiori.event.virtual_dispatcher")
        dispatcher._reset()
        dispatcher._set_scene_executor(function(event_name)
            if event_name == "OnTalk" then
                return coroutine.create(function() return "talk_result" end)
            end
            return nil
        end)
    end

    test("first call initializes next_talk_time and returns nil", function()
        setup()
        local act = create_mock_act({ id = "OnSecondChange", status = "idle", date = { unix = 1702648800 } })
        local result = dispatcher.check_talk(act)
        local state = dispatcher._get_internal_state()

        expect(result):toBe(nil)
        expect(state.next_talk_time > 0):toBe(true)
    end)

    test("fires after interval", function()
        setup()
        -- Initialize
        local act1 = create_mock_act({ id = "OnSecondChange", status = "idle", date = { unix = 1702648800 } })
        dispatcher.dispatch(act1) -- Initialize both timers

        local state = dispatcher._get_internal_state()

        -- After interval
        local act2 = create_mock_act({ id = "OnSecondChange", status = "idle", date = { unix = state.next_talk_time + 1 } })
        local result = dispatcher.check_talk(act2)

        expect(type(result)):toBe("thread")
    end)

    test("skips before interval", function()
        setup()
        -- Initialize
        local act1 = create_mock_act({ id = "OnSecondChange", status = "idle", date = { unix = 1702648800 } })
        dispatcher.dispatch(act1)

        -- Before interval
        local act2 = create_mock_act({ id = "OnSecondChange", status = "idle", date = { unix = 1702648810 } })
        local result = dispatcher.check_talk(act2)

        expect(result):toBe(nil)
    end)
end)

describe("priority and integration", function()
    local dispatcher

    local function setup()
        dispatcher = require("pasta.shiori.event.virtual_dispatcher")
        dispatcher._reset()
        dispatcher._set_scene_executor(function(event_name)
            return coroutine.create(function() return event_name .. "_result" end)
        end)
    end

    test("OnHour has priority over OnTalk", function()
        setup()
        -- Initialize both
        local act1 = create_mock_act({ id = "OnSecondChange", status = "idle", date = { unix = 1702648800 } })
        dispatcher.dispatch(act1)

        -- At next hour (OnHour should fire, OnTalk should not)
        local act2 = create_mock_act({ id = "OnSecondChange", status = "idle", date = { unix = 1702652400, hour = 15 } })
        local result = dispatcher.dispatch(act2)

        -- Should return thread from check_hour
        expect(type(result)):toBe("thread")
    end)

    test("_reset clears all state", function()
        setup()
        -- Set some state
        local act = create_mock_act({ id = "OnSecondChange", status = "idle", date = { unix = 1702648800 } })
        dispatcher.dispatch(act)

        local state_before = dispatcher._get_internal_state()
        expect(state_before.next_hour_unix > 0):toBe(true)

        -- Reset
        dispatcher._reset()

        local state_after = dispatcher._get_internal_state()
        expect(state_after.next_hour_unix):toBe(0)
        expect(state_after.next_talk_time):toBe(0)
        expect(state_after.cached_config):toBe(nil)
    end)
end)

-- ============================================================================
-- Task 4.2: check_hour() 統合テスト - transfer_date_to_var() 呼び出し確認
-- Requirements: 2.1
-- ============================================================================

describe("check_hour - transfer_date_to_var integration", function()
    local dispatcher
    local SHIORI_ACT

    local function setup()
        dispatcher = require("pasta.shiori.event.virtual_dispatcher")
        SHIORI_ACT = require("pasta.shiori.act")
        dispatcher._reset()
        -- 常にシーン実行成功を返すモック(thread返却)
        dispatcher._set_scene_executor(function(event_name, act)
            return coroutine.create(function() return "scene_result" end)
        end)
    end

    test("calls transfer_date_to_var when OnHour fires", function()
        setup()
        local actors = { sakura = { name = "さくら", spot = "sakura" } }
        local act = SHIORI_ACT.new(actors, {
            id = "OnSecondChange",
            status = "idle",
            date = { unix = 1702648800, year = 2026, month = 2, day = 1, hour = 14, min = 0, sec = 0, wday = 0 },
        })

        -- First call: initialize
        dispatcher.check_hour(act)

        -- Advance to next hour
        act.req.date.unix = 1702652400

        -- Second call: should fire and call transfer_date_to_var
        local result = dispatcher.check_hour(act)

        expect(type(result)):toBe("thread")
        -- 日時変数が設定されていること
        expect(act.var.year):toBe(2026)
        expect(act.var[""]):toBe("2026年")
        expect(act.var["時12"]):toBe("午後2時") -- hour 14
    end)

    test("sets Japanese date variables on OnHour fire", function()
        setup()
        local actors = { sakura = { name = "さくら", spot = "sakura" } }
        local act = SHIORI_ACT.new(actors, {
            id = "OnSecondChange",
            status = "idle",
            date = { unix = 0, year = 2026, month = 2, day = 1, hour = 12, min = 0, sec = 0, wday = 0 },
        })

        -- Initialize
        dispatcher.check_hour(act)

        -- Fire (unix > next_hour_unix)
        act.req.date.unix = 3600

        local result = dispatcher.check_hour(act)

        expect(type(result)):toBe("thread")
        expect(act.var["曜日"]):toBe("日曜日")
        expect(act.var.week):toBe("Sunday")
        expect(act.var["時12"]):toBe("正午") -- hour 12
    end)
end)

-- ============================================================================
-- Task 4.3: execute_scene() 統合テスト - act がシーン関数に渡される確認
-- Requirements: 3.1, 3.2
-- ============================================================================

describe("execute_scene - act parameter passing", function()
    local dispatcher

    local function setup()
        dispatcher = require("pasta.shiori.event.virtual_dispatcher")
        dispatcher._reset()
    end

    test("scene_executor receives act parameter", function()
        setup()
        local received_act = nil
        local received_event_name = nil

        dispatcher._set_scene_executor(function(event_name, act)
            received_event_name = event_name
            received_act = act
            return coroutine.create(function() return "scene_result" end)
        end)

        local SHIORI_ACT = require("pasta.shiori.act")
        local actors = { sakura = { name = "さくら", spot = "sakura" } }
        local act = SHIORI_ACT.new(actors, {
            id = "OnSecondChange",
            status = "idle",
            date = { unix = 0, year = 2026, month = 2, day = 1, hour = 14, min = 0, sec = 0, wday = 0 },
        })

        -- Initialize
        dispatcher.check_hour(act)

        -- Fire
        act.req.date.unix = 3600
        dispatcher.check_hour(act)

        expect(received_event_name):toBe("時報14")
        expect(received_act):toBe(act)
    end)
end)

-- ============================================================================
-- Task 4.4: check_talk() 統合テスト - transfer_date_to_var() が呼び出されないこと確認
-- Requirements: 4.1, 3.3
-- ============================================================================

describe("check_talk - no transfer_date_to_var", function()
    local dispatcher
    local SHIORI_ACT

    local function setup()
        dispatcher = require("pasta.shiori.event.virtual_dispatcher")
        SHIORI_ACT = require("pasta.shiori.act")
        dispatcher._reset()
        dispatcher._set_scene_executor(function(event_name, act)
            return coroutine.create(function() return "scene_result" end)
        end)
    end

    test("does not call transfer_date_to_var on OnTalk", function()
        setup()
        local actors = { sakura = { name = "さくら", spot = "sakura" } }
        local act = SHIORI_ACT.new(actors, {
            id = "OnSecondChange",
            status = "idle",
            date = { unix = 0, year = 2026, month = 2, day = 1, hour = 14, min = 0, sec = 0, wday = 0 },
        })

        -- Initialize check_talk
        dispatcher.check_talk(act)

        -- Advance time past talk interval (300+ seconds)
        act.req.date.unix = 500

        -- Also need to initialize check_hour to set next_hour_unix
        dispatcher.check_hour(act)
        act.req.date.unix = 501

        local result = dispatcher.check_talk(act)

        -- OnTalk may or may not fire depending on random interval
        -- but act.var should NOT have date variables set
        expect(act.var.year):toBe(nil)
        expect(act.var[""]):toBe(nil)
    end)

    test("passes act to scene_executor for OnTalk", function()
        setup()
        local received_act = nil

        dispatcher._set_scene_executor(function(event_name, act)
            received_act = act
            return coroutine.create(function() return "scene_result" end)
        end)

        local actors = { sakura = { name = "さくら", spot = "sakura" } }
        local act = SHIORI_ACT.new(actors, {
            id = "OnSecondChange",
            status = "idle",
            date = { unix = 0, year = 2026, month = 2, day = 1, hour = 14, min = 0, sec = 0, wday = 0 },
        })

        -- Initialize both to set up state
        dispatcher.check_hour(act)
        dispatcher.check_talk(act)

        -- Advance time significantly (past talk interval, but not near hour)
        -- next_hour_unix is about 3600, hour_margin is 30, so safe at unix=500
        act.req.date.unix = 500

        -- Force fire by advancing past next_talk_time
        local result = dispatcher.check_talk(act)

        -- Even if it doesn't fire (random), the act should be passed when it does
        -- We can't guarantee a fire, but we can test the executor signature
        if type(result) == "thread" then
            expect(received_act):toBe(act)
        end
    end)
end)

-- ============================================================================
-- Task 3.1: フォールバック順序テスト
-- Requirements: 1.1
-- ============================================================================

describe("check_hour - fallback chain order", function()
    local dispatcher

    local function setup()
        dispatcher = require("pasta.shiori.event.virtual_dispatcher")
        dispatcher._reset()
    end

    test("searches candidates in order: 時報{HH} → OnHour{HH} → 時報その他 → OnHourOther", function()
        setup()
        local called_names = {}

        -- すべての候補で nil を返し、呼び出し順序を記録
        dispatcher._set_scene_executor(function(event_name, act)
            table.insert(called_names, event_name)
            return nil
        end)

        local act = { req = { id = "OnSecondChange", status = "idle", date = { unix = 0, hour = 12 } } }
        -- Initialize
        dispatcher.check_hour(act)
        -- Fire
        act.req.date.unix = 3600
        dispatcher.check_hour(act)

        expect(#called_names):toBe(4)
        expect(called_names[1]):toBe("時報12")
        expect(called_names[2]):toBe("OnHour12")
        expect(called_names[3]):toBe("時報その他")
        expect(called_names[4]):toBe("OnHourOther")
    end)
end)

-- ============================================================================
-- Task 3.2: 早期打ち切りテスト
-- Requirements: 1.3
-- ============================================================================

describe("check_hour - early termination", function()
    local dispatcher

    local function setup()
        dispatcher = require("pasta.shiori.event.virtual_dispatcher")
        dispatcher._reset()
    end

    test("stops at first matching candidate (candidate 1)", function()
        setup()
        local call_count = 0

        dispatcher._set_scene_executor(function(event_name, act)
            call_count = call_count + 1
            -- 候補1(時報12)でヒット
            return coroutine.create(function() return "scene_result" end)
        end)

        local act = { req = { id = "OnSecondChange", status = "idle", date = { unix = 0, hour = 12 } } }
        dispatcher.check_hour(act)
        act.req.date.unix = 3600

        local result = dispatcher.check_hour(act)

        expect(type(result)):toBe("thread")
        expect(call_count):toBe(1)
    end)

    test("stops at candidate 3 when candidates 1-2 miss", function()
        setup()
        local called_names = {}

        dispatcher._set_scene_executor(function(event_name, act)
            table.insert(called_names, event_name)
            if event_name == "時報その他" then
                return coroutine.create(function() return "scene_result" end)
            end
            return nil
        end)

        local act = { req = { id = "OnSecondChange", status = "idle", date = { unix = 0, hour = 9 } } }
        dispatcher.check_hour(act)
        act.req.date.unix = 3600

        local result = dispatcher.check_hour(act)

        expect(type(result)):toBe("thread")
        expect(#called_names):toBe(3)
        expect(called_names[3]):toBe("時報その他")
    end)
end)

-- ============================================================================
-- Task 3.3: 全候補未発見テスト
-- Requirements: 1.2
-- ============================================================================

describe("check_hour - all candidates miss", function()
    local dispatcher

    local function setup()
        dispatcher = require("pasta.shiori.event.virtual_dispatcher")
        dispatcher._reset()
    end

    test("returns nil when no candidate matches", function()
        setup()

        dispatcher._set_scene_executor(function(event_name, act)
            return nil
        end)

        local act = { req = { id = "OnSecondChange", status = "idle", date = { unix = 0, hour = 12 } } }
        dispatcher.check_hour(act)
        act.req.date.unix = 3600

        local result = dispatcher.check_hour(act)

        expect(result):toBe(nil)
    end)
end)

-- ============================================================================
-- Task 3.4: HH フォーマットテスト
-- Requirements: 2.1, 2.2
-- ============================================================================

describe("check_hour - HH format", function()
    local dispatcher

    local function setup()
        dispatcher = require("pasta.shiori.event.virtual_dispatcher")
        dispatcher._reset()
    end

    test("hour=0 generates candidate 時報00", function()
        setup()
        local first_name = nil

        dispatcher._set_scene_executor(function(event_name, act)
            if not first_name then first_name = event_name end
            return coroutine.create(function() return "result" end)
        end)

        local act = { req = { id = "OnSecondChange", status = "idle", date = { unix = 0, hour = 0 } } }
        dispatcher.check_hour(act)
        act.req.date.unix = 3600
        dispatcher.check_hour(act)

        expect(first_name):toBe("時報00")
    end)

    test("hour=9 generates candidate 時報09", function()
        setup()
        local first_name = nil

        dispatcher._set_scene_executor(function(event_name, act)
            if not first_name then first_name = event_name end
            return coroutine.create(function() return "result" end)
        end)

        local act = { req = { id = "OnSecondChange", status = "idle", date = { unix = 0, hour = 9 } } }
        dispatcher.check_hour(act)
        act.req.date.unix = 3600
        dispatcher.check_hour(act)

        expect(first_name):toBe("時報09")
    end)

    test("hour=12 generates candidate 時報12", function()
        setup()
        local first_name = nil

        dispatcher._set_scene_executor(function(event_name, act)
            if not first_name then first_name = event_name end
            return coroutine.create(function() return "result" end)
        end)

        local act = { req = { id = "OnSecondChange", status = "idle", date = { unix = 0, hour = 12 } } }
        dispatcher.check_hour(act)
        act.req.date.unix = 3600
        dispatcher.check_hour(act)

        expect(first_name):toBe("時報12")
    end)

    test("hour=23 generates candidate 時報23", function()
        setup()
        local first_name = nil

        dispatcher._set_scene_executor(function(event_name, act)
            if not first_name then first_name = event_name end
            return coroutine.create(function() return "result" end)
        end)

        local act = { req = { id = "OnSecondChange", status = "idle", date = { unix = 0, hour = 23 } } }
        dispatcher.check_hour(act)
        act.req.date.unix = 3600
        dispatcher.check_hour(act)

        expect(first_name):toBe("時報23")
    end)
end)

-- ============================================================================
-- ontalk-block-condition: dispatch() 経由のブロック条件テスト
-- Requirements: 3.1, 3.2, 3.3, 3.4
-- ============================================================================

describe("dispatch - status block conditions", function()
    local dispatcher

    local function setup()
        dispatcher = require("pasta.shiori.event.virtual_dispatcher")
        dispatcher._reset()
        dispatcher._set_scene_executor(function(event_name)
            return coroutine.create(function() return event_name .. "_result" end)
        end)
    end

    -- ブロック対象 Status 9件(各キーワード単体): dispatch() が nil を返す
    -- Requirements: 3.1

    test("blocks when status is 'talking'", function()
        setup()
        local act1 = create_mock_act({ id = "OnSecondChange", status = "idle", date = { unix = 1702648800 } })
        dispatcher.dispatch(act1)
        local act2 = create_mock_act({ id = "OnSecondChange", status = "talking", date = { unix = 1702652400, hour = 15 } })
        local result = dispatcher.dispatch(act2)
        expect(result):toBe(nil)
    end)

    test("blocks when status is 'choosing'", function()
        setup()
        local act1 = create_mock_act({ id = "OnSecondChange", status = "idle", date = { unix = 1702648800 } })
        dispatcher.dispatch(act1)
        local act2 = create_mock_act({ id = "OnSecondChange", status = "choosing", date = { unix = 1702652400, hour = 15 } })
        local result = dispatcher.dispatch(act2)
        expect(result):toBe(nil)
    end)

    test("blocks when status is 'online'", function()
        setup()
        local act1 = create_mock_act({ id = "OnSecondChange", status = "idle", date = { unix = 1702648800 } })
        dispatcher.dispatch(act1)
        local act2 = create_mock_act({ id = "OnSecondChange", status = "online", date = { unix = 1702652400, hour = 15 } })
        local result = dispatcher.dispatch(act2)
        expect(result):toBe(nil)
    end)

    test("blocks when status is 'opening(communicate)'", function()
        setup()
        local act1 = create_mock_act({ id = "OnSecondChange", status = "idle", date = { unix = 1702648800 } })
        dispatcher.dispatch(act1)
        local act2 = create_mock_act({ id = "OnSecondChange", status = "opening(communicate)", date = { unix = 1702652400, hour = 15 } })
        local result = dispatcher.dispatch(act2)
        expect(result):toBe(nil)
    end)

    test("blocks when status is 'passive'", function()
        setup()
        local act1 = create_mock_act({ id = "OnSecondChange", status = "idle", date = { unix = 1702648800 } })
        dispatcher.dispatch(act1)
        local act2 = create_mock_act({ id = "OnSecondChange", status = "passive", date = { unix = 1702652400, hour = 15 } })
        local result = dispatcher.dispatch(act2)
        expect(result):toBe(nil)
    end)

    test("blocks when status is 'induction'", function()
        setup()
        local act1 = create_mock_act({ id = "OnSecondChange", status = "idle", date = { unix = 1702648800 } })
        dispatcher.dispatch(act1)
        local act2 = create_mock_act({ id = "OnSecondChange", status = "induction", date = { unix = 1702652400, hour = 15 } })
        local result = dispatcher.dispatch(act2)
        expect(result):toBe(nil)
    end)

    test("blocks when status is 'timecritical'", function()
        setup()
        local act1 = create_mock_act({ id = "OnSecondChange", status = "idle", date = { unix = 1702648800 } })
        dispatcher.dispatch(act1)
        local act2 = create_mock_act({ id = "OnSecondChange", status = "timecritical", date = { unix = 1702652400, hour = 15 } })
        local result = dispatcher.dispatch(act2)
        expect(result):toBe(nil)
    end)

    test("blocks when status is 'nouserbreak'", function()
        setup()
        local act1 = create_mock_act({ id = "OnSecondChange", status = "idle", date = { unix = 1702648800 } })
        dispatcher.dispatch(act1)
        local act2 = create_mock_act({ id = "OnSecondChange", status = "nouserbreak", date = { unix = 1702652400, hour = 15 } })
        local result = dispatcher.dispatch(act2)
        expect(result):toBe(nil)
    end)

    test("blocks when status is 'minimizing'", function()
        setup()
        local act1 = create_mock_act({ id = "OnSecondChange", status = "idle", date = { unix = 1702648800 } })
        dispatcher.dispatch(act1)
        local act2 = create_mock_act({ id = "OnSecondChange", status = "minimizing", date = { unix = 1702652400, hour = 15 } })
        local result = dispatcher.dispatch(act2)
        expect(result):toBe(nil)
    end)

    -- 複合 Status 2件: dispatch() が nil を返す
    -- Requirements: 3.2

    test("blocks when compound status contains 'choosing'", function()
        setup()
        local act1 = create_mock_act({ id = "OnSecondChange", status = "idle", date = { unix = 1702648800 } })
        dispatcher.dispatch(act1)
        local act2 = create_mock_act({ id = "OnSecondChange", status = "choosing,balloon(0=0)", date = { unix = 1702652400, hour = 15 } })
        local result = dispatcher.dispatch(act2)
        expect(result):toBe(nil)
    end)

    test("blocks when compound status contains 'online'", function()
        setup()
        local act1 = create_mock_act({ id = "OnSecondChange", status = "idle", date = { unix = 1702648800 } })
        dispatcher.dispatch(act1)
        local act2 = create_mock_act({ id = "OnSecondChange", status = "online,balloon(1=2)", date = { unix = 1702652400, hour = 15 } })
        local result = dispatcher.dispatch(act2)
        expect(result):toBe(nil)
    end)

    -- 非ブロック Status 4件: dispatch() が nil 以外を返す
    -- Requirements: 3.3

    test("does not block when status is nil", function()
        setup()
        local act1 = create_mock_act({ id = "OnSecondChange", status = nil, date = { unix = 1702648800 } })
        dispatcher.dispatch(act1)
        local act2 = create_mock_act({ id = "OnSecondChange", status = nil, date = { unix = 1702652400, hour = 15 } })
        local result = dispatcher.dispatch(act2)
        expect(result ~= nil):toBe(true)
    end)

    test("does not block when status is empty string", function()
        setup()
        local act1 = create_mock_act({ id = "OnSecondChange", status = "", date = { unix = 1702648800 } })
        dispatcher.dispatch(act1)
        local act2 = create_mock_act({ id = "OnSecondChange", status = "", date = { unix = 1702652400, hour = 15 } })
        local result = dispatcher.dispatch(act2)
        expect(result ~= nil):toBe(true)
    end)

    test("does not block when status is 'idle'", function()
        setup()
        local act1 = create_mock_act({ id = "OnSecondChange", status = "idle", date = { unix = 1702648800 } })
        dispatcher.dispatch(act1)
        local act2 = create_mock_act({ id = "OnSecondChange", status = "idle", date = { unix = 1702652400, hour = 15 } })
        local result = dispatcher.dispatch(act2)
        expect(result ~= nil):toBe(true)
    end)

    test("does not block when status is 'balloon(0=0)'", function()
        setup()
        local act1 = create_mock_act({ id = "OnSecondChange", status = "balloon(0=0)", date = { unix = 1702648800 } })
        dispatcher.dispatch(act1)
        local act2 = create_mock_act({ id = "OnSecondChange", status = "balloon(0=0)", date = { unix = 1702652400, hour = 15 } })
        local result = dispatcher.dispatch(act2)
        expect(result ~= nil):toBe(true)
    end)

    -- M.is_blocked() 直接呼び出し 3件
    -- Requirements: 3.4

    test("is_blocked returns true for 'talking'", function()
        setup()
        expect(dispatcher.is_blocked("talking")):toBe(true)
    end)

    test("is_blocked returns false for 'idle'", function()
        setup()
        expect(dispatcher.is_blocked("idle")):toBe(false)
    end)

    test("is_blocked returns false for nil", function()
        setup()
        expect(dispatcher.is_blocked(nil)):toBe(false)
    end)
end)