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
-- event_coroutine_test.lua
-- Lua-side BDD tests for EVENT.fire coroutine management
local describe = require("lua_test.test").describe
local test = require("lua_test.test").test
local expect = require("lua_test.test").expect

-- 共通セットアップ: 関連モジュールを package.loaded から一括リロードする(スイート分離規約)。
-- 戻り値は EVENT, REG, STORE。reset_res=false の場合のみ pasta.shiori.res をリロードしない
-- (「既存コルーチン置換」describe の従来挙動を保存)。register/res は依存なしの純モジュールで、
-- pasta.shiori.event のリロード時に内部 require で追従するため、明示 require の省略は等価。
local function reload_event_modules(reset_res)
    package.loaded["pasta.shiori.event"] = nil
    package.loaded["pasta.shiori.event.register"] = nil
    package.loaded["pasta.store"] = nil
    if reset_res ~= false then
        package.loaded["pasta.shiori.res"] = nil
    end
    package.loaded["pasta.shiori.event.boot"] = nil
    package.loaded["pasta.shiori.event.second_change"] = nil

    local REG = require("pasta.shiori.event.register")
    local STORE = require("pasta.store")
    STORE.reset()

    -- 登録をクリア
    for k in pairs(REG) do
        REG[k] = nil
    end

    local EVENT = require("pasta.shiori.event")
    return EVENT, REG, STORE
end

-- ============================================================================
-- Task 2.1: set_co_scene() ローカル関数のテスト
-- Task 2.2: EVENT.fire thread判定とresume処理
-- Task 2.3: EVENT.fire コルーチン状態管理
-- Requirements: 1.1-1.6, 2.1-2.8
-- ============================================================================

describe("EVENT.fire - コルーチン対応", function()
    local EVENT, REG, STORE

    local function setup()
        EVENT, REG, STORE = reload_event_modules()
    end

    test("handler が thread を返した場合 resume が実行される", function()
        setup()
        local resumed = false
        REG.TestEvent = function(act)
            return coroutine.create(function()
                resumed = true
                return "test_result"
            end)
        end

        local req = { id = "TestEvent" }
        EVENT.fire(req)

        expect(resumed):toBe(true)
    end)

    test("threadがyieldした場合 STORE.co_scene が設定される", function()
        setup()
        REG.TestEvent = function(act)
            return coroutine.create(function()
                coroutine.yield("first_yield")
                return "final"
            end)
        end

        local req = { id = "TestEvent" }
        EVENT.fire(req)

        expect(type(STORE.co_scene)):toBe("thread")
        expect(coroutine.status(STORE.co_scene)):toBe("suspended")
    end)

    test("threadが正常終了した場合 STORE.co_scene が nil にクリアされる", function()
        setup()
        REG.TestEvent = function(act)
            return coroutine.create(function()
                return "completed"
            end)
        end

        local req = { id = "TestEvent" }
        EVENT.fire(req)

        expect(STORE.co_scene):toBe(nil)
    end)

    test("handler が string を返した場合 そのまま RES.ok() で返す", function()
        setup()
        REG.TestEvent = function(act)
            return "string_result"
        end

        local req = { id = "TestEvent" }
        local result = EVENT.fire(req)

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

    test("handler が nil を返した場合 RES.no_content() が返る", function()
        setup()
        REG.TestEvent = function(act)
            return nil
        end

        local req = { id = "TestEvent" }
        local result = EVENT.fire(req)

        expect(result:find("204 No Content")):toBeTruthy()
    end)

    test("yield値が RES.ok() で返される", function()
        setup()
        REG.TestEvent = function(act)
            return coroutine.create(function()
                coroutine.yield("yielded_value")
            end)
        end

        local req = { id = "TestEvent" }
        local result = EVENT.fire(req)

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

    test("coroutine.resume() エラー時に STORE.co_scene がクリアされる", function()
        setup()
        REG.TestEvent = function(act)
            return coroutine.create(function()
                error("intentional_error")
            end)
        end

        local req = { id = "TestEvent" }
        -- EVENT.fire はエラーを伝搬する
        local ok, err = pcall(function()
            EVENT.fire(req)
        end)

        expect(ok):toBe(false)
        expect(STORE.co_scene):toBe(nil)
    end)
end)

-- ============================================================================
-- Task 3.1: resume_until_valid() 単体テスト
-- Requirements: 1.1, 1.2, 1.3
-- ============================================================================

describe("resume_until_valid - nil yieldスキップループ", function()
    local EVENT

    local function setup()
        EVENT = reload_event_modules()
    end

    test("nil yieldしてsuspendedのコルーチンが再resumeされる (1.1)", function()
        setup()
        local resume_count = 0
        local co = coroutine.create(function()
            resume_count = resume_count + 1
            coroutine.yield(nil) -- nil yield (should continue)
            resume_count = resume_count + 1
            coroutine.yield(nil) -- nil yield again
            resume_count = resume_count + 1
            return "valid_result"
        end)

        local ok, value = EVENT._resume_until_valid(co)

        expect(ok):toBe(true)
        expect(value):toBe("valid_result")
        expect(resume_count):toBe(3)
    end)

    test("有効値(nil以外)を返したらループ終了 (1.2)", function()
        setup()
        local resume_count = 0
        local co = coroutine.create(function()
            resume_count = resume_count + 1
            return "immediate_result"
        end)

        local ok, value = EVENT._resume_until_valid(co)

        expect(ok):toBe(true)
        expect(value):toBe("immediate_result")
        expect(resume_count):toBe(1)
    end)

    test("dead状態でnilを返したらループ終了(空シーン) (1.2)", function()
        setup()
        local resume_count = 0
        local co = coroutine.create(function()
            resume_count = resume_count + 1
            -- 終了時にnilを返す(空シーン)
            return nil
        end)

        local ok, value = EVENT._resume_until_valid(co)

        expect(ok):toBe(true)
        expect(value):toBe(nil) -- dead状態のnilは有効値
        expect(resume_count):toBe(1)
        expect(coroutine.status(co)):toBe("dead")
    end)

    test("エラー発生時にok=false、エラーメッセージを返す (1.3)", function()
        setup()
        local co = coroutine.create(function()
            error("intentional_error")
        end)

        local ok, value = EVENT._resume_until_valid(co)

        expect(ok):toBe(false)
        expect(type(value)):toBe("string")
        expect(value:find("intentional_error")):toBeTruthy()
    end)

    test("初回resume引数がコルーチンに渡される", function()
        setup()
        local received_arg = nil
        local co = coroutine.create(function(arg)
            received_arg = arg
            return "done"
        end)

        local ok, value = EVENT._resume_until_valid(co, "test_arg")

        expect(ok):toBe(true)
        expect(received_arg):toBe("test_arg")
    end)

    test("2回目以降のresumeは引数なしで呼ばれる", function()
        setup()
        local args_received = {}
        local co = coroutine.create(function(first_arg)
            table.insert(args_received, first_arg or "nil")
            local second_arg = coroutine.yield(nil) -- nil yield
            table.insert(args_received, second_arg or "nil")
            return "done"
        end)

        local ok, value = EVENT._resume_until_valid(co, "first")

        expect(ok):toBe(true)
        expect(args_received[1]):toBe("first")
        expect(args_received[2]):toBe("nil") -- 2回目は引数なし
    end)
end)

describe("EVENT.fire - 既存コルーチン置換", function()
    local EVENT, REG, STORE

    local function setup()
        -- pasta.shiori.res は従来からリロード対象外(reset_res=false)
        EVENT, REG, STORE = reload_event_modules(false)
    end

    test("新しいコルーチンが設定されると既存のコルーチンがcloseされる", function()
        setup()

        -- 既存のsuspendedコルーチンを設定
        local old_co = coroutine.create(function()
            coroutine.yield("old")
        end)
        coroutine.resume(old_co) -- suspended状態に
        STORE.co_scene = old_co

        -- 新しいthreadを返すハンドラ
        REG.TestEvent = function(act)
            return coroutine.create(function()
                coroutine.yield("new")
            end)
        end

        local req = { id = "TestEvent" }
        EVENT.fire(req)

        if coroutine.close then
            -- 旧コルーチンはcloseされてdead状態
            expect(coroutine.status(old_co)):toBe("dead")
        else
            -- LuaJIT/Lua 5.1 does not provide coroutine.close().
            expect(coroutine.status(old_co)):toBe("suspended")
        end
        -- 新しいコルーチンがSTORE.co_sceneに設定されている
        expect(type(STORE.co_scene)):toBe("thread")
        expect(STORE.co_scene ~= old_co):toBe(true)
    end)

    test("同じコルーチンを再設定しようとしてもcloseしない", function()
        setup()

        local the_co

        REG.TestEvent = function(act)
            the_co = coroutine.create(function()
                coroutine.yield("first")
                coroutine.yield("second")
            end)
            return the_co
        end

        -- 最初の呼び出し
        local req = { id = "TestEvent" }
        EVENT.fire(req)

        -- the_coがSTORE.co_sceneに設定されている
        expect(STORE.co_scene):toBe(the_co)
        expect(coroutine.status(the_co)):toBe("suspended")
    end)
end)

-- ============================================================================
-- Task 3.2: EVENT.fire統合テスト(nil yieldスキップ対応)
-- Requirements: 2.1, 2.4, 3.1, 3.2
-- ============================================================================

describe("EVENT.fire - nil yieldスキップ統合", function()
    local EVENT, REG, STORE

    local function setup()
        EVENT, REG, STORE = reload_event_modules()
    end

    test("nil yieldするthreadがresume_until_validで処理される (2.1)", function()
        setup()
        local resume_count = 0
        REG.TestEvent = function(act)
            return coroutine.create(function()
                resume_count = resume_count + 1
                coroutine.yield(nil)            -- スキップされる
                resume_count = resume_count + 1
                coroutine.yield("valid_result") -- これが返る
            end)
        end

        local req = { id = "TestEvent" }
        local result = EVENT.fire(req)

        expect(resume_count):toBe(2)
        expect(result:find("valid_result")):toBeTruthy()
    end)

    test("有効値を返すthreadハンドラが正常動作 (2.1)", function()
        setup()
        REG.TestEvent = function(act)
            return coroutine.create(function()
                return "immediate_result"
            end)
        end

        local req = { id = "TestEvent" }
        local result = EVENT.fire(req)

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

    test("エラー発生時にset_co_sceneでcloseされる (3.1, 3.2)", function()
        setup()
        REG.TestEvent = function(act)
            return coroutine.create(function()
                error("test_error")
            end)
        end

        local req = { id = "TestEvent" }
        local ok, err = pcall(function()
            EVENT.fire(req)
        end)

        expect(ok):toBe(false)
        expect(err:find("test_error")):toBeTruthy()
        expect(STORE.co_scene):toBe(nil)
    end)

    test("ループ終了後にsuspendedならset_co_sceneで保存 (2.4)", function()
        setup()
        REG.TestEvent = function(act)
            return coroutine.create(function()
                coroutine.yield(nil)     -- スキップ
                coroutine.yield("valid") -- 有効値
                -- まだ終了していない
                coroutine.yield("more")
            end)
        end

        local req = { id = "TestEvent" }
        EVENT.fire(req)

        expect(type(STORE.co_scene)):toBe("thread")
        expect(coroutine.status(STORE.co_scene)):toBe("suspended")
    end)

    test("空シーン(dead + nil)が204 No Contentに変換 (2.3)", function()
        setup()
        REG.TestEvent = function(act)
            return coroutine.create(function()
                -- 何もyieldせず終了(空シーン)
                return nil
            end)
        end

        local req = { id = "TestEvent" }
        local result = EVENT.fire(req)

        expect(result:find("204 No Content")):toBeTruthy()
    end)
end)

-- ============================================================================
-- Task 3.3: 後方互換性テスト
-- Requirements: 2.2, 2.3
-- ============================================================================

describe("EVENT.fire - 後方互換性", function()
    local EVENT, REG

    local function setup()
        EVENT, REG = reload_event_modules()
    end

    test("既存の文字列ハンドラが正常動作 (2.2)", function()
        setup()
        REG.TestEvent = function(act)
            return "string_response"
        end

        local req = { id = "TestEvent" }
        local result = EVENT.fire(req)

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

    test("既存のnil戻り値が204 No Contentに変換 (2.3)", function()
        setup()
        REG.TestEvent = function(act)
            return nil
        end

        local req = { id = "TestEvent" }
        local result = EVENT.fire(req)

        expect(result:find("204 No Content")):toBeTruthy()
    end)
end)

return true