Skip to content

Commit 4e80c3e

Browse files
committed
feat: add msgEqual and msgClose
1 parent 4ddc43a commit 4e80c3e

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

src/gameClient.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,29 @@ export namespace MenuArgs {
4343
}
4444
}
4545

46+
export type MsgArgs = {
47+
text: string
48+
close: () => void
49+
}
50+
51+
export namespace MsgArgs {
52+
export function create(
53+
text?: string,
54+
close?: () => void
55+
): MsgArgs {
56+
return {
57+
text: text || "",
58+
close: close || (() => {}),
59+
}
60+
}
61+
}
62+
4663
export type GameClientState = {
4764
main: TimeUpdatedValue<string>
4865
actions: TimeUpdatedValue<QspListItem[]>
4966
objects: TimeUpdatedValue<QspListItem[]>
5067
menus: TimeUpdatedValue<MenuArgs>
68+
msg: TimeUpdatedValue<MsgArgs>
5169
error?: TimeUpdatedValue<QspErrorData>
5270
}
5371

@@ -58,6 +76,7 @@ export namespace GameClientState {
5876
actions: TimeUpdatedValue.create(dateTime, []),
5977
objects: TimeUpdatedValue.create(dateTime, []),
6078
menus: TimeUpdatedValue.create(dateTime, MenuArgs.create()),
79+
msg: TimeUpdatedValue.create(dateTime, MsgArgs.create()),
6180
error: undefined
6281
}
6382
}
@@ -72,6 +91,8 @@ export type GameClient = Atom<GameClientState> & Actions<{
7291
getObjects: () => TimeUpdatedValue<QspListItem[]>,
7392
setMenu: (args: MenuArgs) => void,
7493
getMenu: () => TimeUpdatedValue<MenuArgs>,
94+
setMsg: (args: MsgArgs) => void,
95+
getMsg: () => TimeUpdatedValue<MsgArgs>,
7596
}>
7697

7798
export namespace GameClient {
@@ -107,6 +128,13 @@ export namespace GameClient {
107128
},
108129
getMenu: () : TimeUpdatedValue<MenuArgs> =>
109130
atom.value.menus,
131+
setMsg: (msg: MsgArgs) => {
132+
atom.focus("msg").set(
133+
TimeUpdatedValue.create(new Date, msg)
134+
)
135+
},
136+
getMsg: () : TimeUpdatedValue<MsgArgs> =>
137+
atom.value.msg,
110138
})
111139
)
112140
}
@@ -137,6 +165,12 @@ export namespace GameClient {
137165
)
138166
})
139167

168+
api.on("msg", (text, close) => {
169+
$gameClient.actions.setMsg(
170+
MsgArgs.create(text, close)
171+
)
172+
})
173+
140174
api.on("version", (type, onVersion) => {
141175
onVersion(api.version())
142176
})

src/testClient.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,23 @@ export namespace TestClient {
218218
assert.fail(`Found "${menu}" in:\n${actionNames}`)
219219
}
220220
}
221+
222+
export async function msgEqual(
223+
testClient: TestClient,
224+
expectedString: string
225+
) {
226+
const $msgState = await getNewValue(
227+
testClient.lastSelectedTime,
228+
testClient.client.actions.getMsg,
229+
)
230+
expect($msgState.value.text).toBe(expectedString)
231+
}
232+
233+
export async function msgClose(testClient: TestClient) {
234+
const $msgState = await getNewValue(
235+
testClient.lastSelectedTime,
236+
testClient.client.actions.getMsg,
237+
)
238+
$msgState.value.close()
239+
}
221240
}

tests/mocks/msg.qsps

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# begin
2+
msg 'Hello world'
3+
'Text after message'
4+
-
5+
6+
# without msg
7+
'without msg'
8+
-

tests/testClient.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,29 @@ describe("Menu not has", () => {
241241
await TestClient.notHasMenu(testClient, "Non-existent action")
242242
})
243243
})
244+
245+
describe("msg equal", () => {
246+
it("equal", async () => {
247+
const testClient = await TestClient.start("tests/mocks", "msg.qsps")
248+
await TestClient.msgEqual(testClient, "Hello world")
249+
})
250+
it("not equal", async () => {
251+
const testClient = await TestClient.start("tests/mocks", "msg.qsps")
252+
await expect(() => TestClient.msgEqual(testClient, "anything else"))
253+
.rejects
254+
.toThrowError("expected 'Hello world' to be 'anything else'")
255+
})
256+
})
257+
258+
describe("msg close", () => {
259+
it("success", async () => {
260+
const testClient = await TestClient.start("tests/mocks", "msg.qsps")
261+
await TestClient.msgClose(testClient)
262+
await TestClient.mainEqual(testClient, "Text after message\r\n")
263+
})
264+
// it("msg not opened", async () => {
265+
// const testClient = await TestClient.start(
266+
// "tests/mocks", "msg.qsps", StartingLocation.mkCustom("without msg"))
267+
// await TestClient.msgClose(testClient) // Error: Test timed out in 5000ms.
268+
// })
269+
})

0 commit comments

Comments
 (0)