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
-- ============================================================================
-- Task 7: 統合テスト - コルーチン実行とチェイントーク
-- Requirements: 6.1, 6.2, 6.3, 6.4, 8.1, 8.2, 8.3, 8.4, 8.5
-- ============================================================================
local describe = require("lua_test.test").describe
local test = require("lua_test.test").test
local expect = require("lua_test.test").expect
-- 共通セットアップ: 関連モジュールを package.loaded から一括リロードする(スイート分離規約)。
-- pasta.shiori.res は pasta.shiori.event のリロード時に内部 require で追従する(純モジュール)。
local function reload_modules()
package.loaded["pasta.store"] = nil
package.loaded["pasta.shiori.event"] = nil
package.loaded["pasta.shiori.event.register"] = nil
package.loaded["pasta.shiori.res"] = nil
package.loaded["pasta.shiori.act"] = nil
local STORE = require("pasta.store")
local EVENT = require("pasta.shiori.event")
local REG = require("pasta.shiori.event.register")
-- Setup actors for SHIORI_ACT
STORE.actors = { sakura = { name = "さくら", spot = "sakura" } }
-- Ensure clean state
STORE.co_scene = nil
return EVENT, REG, STORE
end
-- ============================================================================
-- Task 7.1: E2Eテストでact:yield()とチェイントークを検証
-- ============================================================================
describe("Integration - act:yield() and chain talk", function()
local EVENT
local STORE
local REG
local function setup()
EVENT, REG, STORE = reload_modules()
end
test("yield() returns value and sets STORE.co_scene", function()
setup()
-- Register a handler that yields using coroutine.yield directly
REG.OnYieldTest = function(handler_act)
return coroutine.create(function(act)
coroutine.yield("First message\\e")
return "Second message\\e"
end)
end
-- Fire the event
local response = EVENT.fire({ id = "OnYieldTest" })
-- First response should contain "First message"
expect(response:find("First message")).not_:toBe(nil)
expect(response:find("200 OK")).not_:toBe(nil)
-- STORE.co_scene should be set (suspended)
expect(STORE.co_scene).not_:toBe(nil)
expect(coroutine.status(STORE.co_scene)):toBe("suspended")
end)
test("chain talk continues suspended coroutine", function()
setup()
local call_count = 0
-- Register a handler that yields
REG.OnChainTest = function(handler_act)
return coroutine.create(function(act)
call_count = call_count + 1
coroutine.yield("Message " .. call_count .. "\\e")
call_count = call_count + 1
return "Message " .. call_count .. "\\e"
end)
end
-- First fire
local response1 = EVENT.fire({ id = "OnChainTest" })
expect(response1:find("Message 1")).not_:toBe(nil)
expect(call_count):toBe(1)
-- STORE.co_scene should be set
local co = STORE.co_scene
expect(co).not_:toBe(nil)
expect(coroutine.status(co)):toBe("suspended")
-- Create a new handler that returns the suspended coroutine
-- (simulating what check_talk() does for chain talk)
REG.OnChainContinue = function(handler_act)
-- Return existing suspended coroutine for continuation
return STORE.co_scene
end
-- Second fire should resume the coroutine
local response2 = EVENT.fire({ id = "OnChainContinue" })
expect(response2:find("Message 2")).not_:toBe(nil)
expect(call_count):toBe(2)
-- STORE.co_scene should be cleared after completion
expect(STORE.co_scene):toBe(nil)
end)
test("completed coroutine clears STORE.co_scene", function()
setup()
-- Register a handler that completes without yielding
REG.OnCompleteTest = function(handler_act)
return coroutine.create(function(act)
return "Only message\\e"
end)
end
-- Fire the event
local response = EVENT.fire({ id = "OnCompleteTest" })
-- Response should contain message
expect(response:find("Only message")).not_:toBe(nil)
-- STORE.co_scene should be nil (coroutine completed)
expect(STORE.co_scene):toBe(nil)
end)
test("multiple yields work correctly", function()
setup()
local step = 0
REG.OnMultiYield = function(handler_act)
return coroutine.create(function(act)
step = 1
coroutine.yield("Step 1\\e")
step = 2
coroutine.yield("Step 2\\e")
step = 3
return "Step 3\\e"
end)
end
-- Simulate continuation by returning suspended coroutine
REG.OnMultiContinue = function(handler_act)
return STORE.co_scene
end
-- First fire
EVENT.fire({ id = "OnMultiYield" })
expect(step):toBe(1)
expect(STORE.co_scene).not_:toBe(nil)
-- Second fire (continue)
EVENT.fire({ id = "OnMultiContinue" })
expect(step):toBe(2)
expect(STORE.co_scene).not_:toBe(nil)
-- Third fire (complete)
EVENT.fire({ id = "OnMultiContinue" })
expect(step):toBe(3)
expect(STORE.co_scene):toBe(nil)
end)
end)
-- ============================================================================
-- Task 7.2: エラー処理の統合テスト
-- ============================================================================
describe("Integration - error handling", function()
local EVENT
local STORE
local REG
local function setup()
EVENT, REG, STORE = reload_modules()
end
test("coroutine error clears STORE.co_scene", function()
setup()
REG.OnErrorTest = function(act)
return coroutine.create(function()
error("Test error")
end)
end
-- Fire should propagate error
local ok, err = pcall(function()
EVENT.fire({ id = "OnErrorTest" })
end)
expect(ok):toBe(false)
expect(tostring(err):find("Test error")).not_:toBe(nil)
-- STORE.co_scene should be cleared
expect(STORE.co_scene):toBe(nil)
end)
test("new coroutine closes existing suspended coroutine", function()
setup()
local old_co
-- First handler that yields
REG.OnFirst = function(act)
old_co = coroutine.create(function()
coroutine.yield("first")
end)
return old_co
end
-- Fire first to set up suspended coroutine
EVENT.fire({ id = "OnFirst" })
expect(STORE.co_scene):toBe(old_co)
expect(coroutine.status(old_co)):toBe("suspended")
-- Second handler that returns a NEW coroutine
REG.OnSecond = function(act)
return coroutine.create(function()
return "second result"
end)
end
-- Fire second - should close the old coroutine
EVENT.fire({ id = "OnSecond" })
if coroutine.close then
-- Old coroutine should be dead (closed)
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 should be nil (new coroutine completed)
expect(STORE.co_scene):toBe(nil)
end)
test("STORE.reset() closes suspended coroutine", function()
setup()
local co
REG.OnResetTest = function(act)
co = coroutine.create(function()
coroutine.yield("message")
end)
return co
end
-- Fire to create suspended coroutine
EVENT.fire({ id = "OnResetTest" })
expect(STORE.co_scene):toBe(co)
expect(coroutine.status(co)):toBe("suspended")
-- Reset should close and clear
STORE.reset()
expect(STORE.co_scene):toBe(nil)
if coroutine.close then
expect(coroutine.status(co)):toBe("dead")
else
-- LuaJIT/Lua 5.1 does not provide coroutine.close().
expect(coroutine.status(co)):toBe("suspended")
end
end)
end)