@quenty/saveslot 2.7.1 → 2.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,420 @@
1
+ --!strict
2
+ --[[
3
+ Drives the command bodies directly against a player who is not in this server. The service is built
4
+ by hand rather than through a ServiceBag so the real CmdrService (and the Cmdr instance tree behind
5
+ it) stays out of the test place -- what is worth checking here is that a command reaches an absent
6
+ player's stored data at all, and hands their session back afterwards.
7
+
8
+ Command bodies take the userId list Cmdr's `playerIds` type parses, so these pass one directly.
9
+ Slot arguments are passed as resolved indices, which is what the `slotIndices` type produces.
10
+
11
+ @class SaveSlotCmdrService.spec.lua
12
+ ]]
13
+ local require = require(script.Parent.loader).load(script)
14
+
15
+ local CmdrReplyUtils = require("CmdrReplyUtils")
16
+ local DataStoreMock = require("DataStoreMock")
17
+ local DataStoreTestUtils = require("DataStoreTestUtils")
18
+ local Jest = require("Jest")
19
+ local Maid = require("Maid")
20
+ local PlayerDataStoreService = require("PlayerDataStoreService")
21
+ local Promise = require("Promise")
22
+ local PromiseTestUtils = require("PromiseTestUtils")
23
+ local SaveSlotCmdrService = require("SaveSlotCmdrService")
24
+ local SaveSlotService = require("SaveSlotService")
25
+ local ServiceBag = require("ServiceBag")
26
+
27
+ local describe = Jest.Globals.describe
28
+ local expect = Jest.Globals.expect
29
+ local it = Jest.Globals.it
30
+
31
+ local ABSENT_USER_ID = 5150
32
+ local OTHER_USER_ID = 5151
33
+
34
+ -- Short enough that the progress test does not have to sit through the real threshold.
35
+ local SLOW_REPLY_SECONDS = 0.05
36
+
37
+ local function setup()
38
+ local maid = Maid.new()
39
+ local serviceBag = maid:Add(ServiceBag.new())
40
+ local playerDataStoreService: PlayerDataStoreService.PlayerDataStoreService =
41
+ serviceBag:GetService(PlayerDataStoreService) :: any
42
+ local saveSlotService: SaveSlotService.SaveSlotService = serviceBag:GetService(SaveSlotService) :: any
43
+ -- The store behind share codes, mocked separately from the player store the slots themselves live in.
44
+ local sharedService: any = serviceBag:GetService(require("SaveSlotSharedDataStoreService"))
45
+ serviceBag:Init()
46
+
47
+ local mock = DataStoreMock.new()
48
+ playerDataStoreService:SetRobloxDataStore(mock)
49
+ sharedService:SetRobloxDataStore(DataStoreMock.new())
50
+ saveSlotService:SetMaxSlotCount(5)
51
+ serviceBag:Start()
52
+
53
+ local registered = {}
54
+ local cmdr = {
55
+ Registry = {
56
+ RegisterType = function() end,
57
+ },
58
+ Util = {
59
+ MakeFuzzyFinder = function()
60
+ return function()
61
+ return {}
62
+ end
63
+ end,
64
+ MakeListableType = function(definition)
65
+ return definition
66
+ end,
67
+ },
68
+ }
69
+ local cmdrService = {
70
+ RegisterCommand = function(_self, definition, execute)
71
+ registered[definition.Name] = execute
72
+ end,
73
+ PromiseCmdr = function()
74
+ return Promise.resolved(cmdr)
75
+ end,
76
+ }
77
+
78
+ -- Typed loosely: the fields are filled in by hand below rather than by Init, so the table is not a
79
+ -- SaveSlotCmdrService until the last of them lands.
80
+ local service: any = setmetatable({}, { __index = SaveSlotCmdrService })
81
+ service._maid = Maid.new()
82
+ service._serviceBag = serviceBag
83
+ service._cmdrService = cmdrService
84
+ service._playerDataStoreService = playerDataStoreService
85
+ service._saveSlotDataService = serviceBag:GetService(require("SaveSlotDataService"))
86
+ service._hasSaveSlotsBinder = serviceBag:GetService(require("HasSaveSlots"))
87
+ -- Shadows the module-instance lookup, which resolves SaveSlotService through the script tree to
88
+ -- dodge a require cycle. Handing it over directly keeps that indirection out of the test.
89
+ service._getSaveSlotService = function()
90
+ return saveSlotService
91
+ end
92
+ service:SetReplyConfig(CmdrReplyUtils.createConfig({ slowReplySeconds = SLOW_REPLY_SECONDS }))
93
+ service:Start()
94
+
95
+ maid:GiveTask(function()
96
+ service._maid:Destroy()
97
+ end)
98
+
99
+ local replies: { string } = {}
100
+ local context = {
101
+ Reply = function(_self, text: string)
102
+ table.insert(replies, text)
103
+ end,
104
+ }
105
+
106
+ return {
107
+ mock = mock,
108
+ replies = replies,
109
+ run = function(commandName: string, ...)
110
+ return registered[commandName](context, ...)
111
+ end,
112
+ seed = function(raw, userId: number?)
113
+ mock:SetRaw(tostring(userId or ABSENT_USER_ID), raw)
114
+ end,
115
+ readRaw = function(userId: number?)
116
+ return mock:GetRaw(tostring(userId or ABSENT_USER_ID))
117
+ end,
118
+ destroy = function()
119
+ DataStoreTestUtils.awaitServiceShutdown(playerDataStoreService)
120
+ maid:DoCleaning()
121
+ end,
122
+ }
123
+ end
124
+
125
+ describe("SaveSlotCmdrService against an absent player", function()
126
+ it("lists the stored slots", function()
127
+ local controller = setup()
128
+
129
+ controller.seed({
130
+ SaveSlots = {
131
+ slotMetadata = {
132
+ ["slot-a"] = { SlotIndex = 1, SlotName = "Alpha" },
133
+ },
134
+ },
135
+ })
136
+
137
+ local output = controller.run("saveslot-list", { ABSENT_USER_ID })
138
+ expect(string.find(output, "Alpha", 1, true) ~= nil).toEqual(true)
139
+ expect(string.find(output, tostring(ABSENT_USER_ID), 1, true) ~= nil).toEqual(true)
140
+
141
+ controller.destroy()
142
+ end)
143
+
144
+ it("lists a slot's summary and playtime, which is what the listing is read for", function()
145
+ local controller = setup()
146
+
147
+ controller.seed({
148
+ SaveSlots = {
149
+ slotMetadata = {
150
+ ["slot-a"] = {
151
+ SlotIndex = 1,
152
+ SlotName = "Alpha",
153
+ TimePlayed = 5000,
154
+ PlayCount = 4,
155
+ Summary = { progress = { chapter = 3 } },
156
+ },
157
+ },
158
+ },
159
+ })
160
+
161
+ local output = controller.run("saveslot-list", { ABSENT_USER_ID })
162
+ expect(string.find(output, "progress: chapter = 3", 1, true) ~= nil).toEqual(true)
163
+ expect(string.find(output, "played 1h 23m, 4 session(s)", 1, true) ~= nil).toEqual(true)
164
+ -- The bug this replaced: a structured summary printed as "table: 0x...".
165
+ expect(string.find(output, "table: 0x", 1, true)).toBeNil()
166
+
167
+ controller.destroy()
168
+ end)
169
+
170
+ it("creates a slot that lands in their datastore", function()
171
+ local controller = setup()
172
+
173
+ local output = controller.run("saveslot-create", { ABSENT_USER_ID }, { 2 })
174
+ expect(string.find(output, "created slot(s) 2", 1, true) ~= nil).toEqual(true)
175
+
176
+ -- The session is released when the command finishes, which is what flushes the write.
177
+ local raw = controller.readRaw()
178
+ expect(raw).never.toBeNil()
179
+ expect(raw.SaveSlots).never.toBeNil()
180
+
181
+ -- And a second command sees it, which it only can if the first one really persisted.
182
+ local listed = controller.run("saveslot-list", { ABSENT_USER_ID })
183
+ expect(string.find(listed, "(2)", 1, true) ~= nil).toEqual(true)
184
+
185
+ controller.destroy()
186
+ end)
187
+
188
+ it("deletes a stored slot", function()
189
+ local controller = setup()
190
+
191
+ controller.run("saveslot-create", { ABSENT_USER_ID }, { 1, 2 })
192
+
193
+ local output = controller.run("saveslot-delete", { ABSENT_USER_ID }, { 1 })
194
+ expect(string.find(output, "deleted slot 1", 1, true) ~= nil).toEqual(true)
195
+
196
+ local listed = controller.run("saveslot-list", { ABSENT_USER_ID })
197
+ expect(string.find(listed, "(1)", 1, true)).toBeNil()
198
+ expect(string.find(listed, "(2)", 1, true) ~= nil).toEqual(true)
199
+
200
+ controller.destroy()
201
+ end)
202
+
203
+ it("copies a slot into the lowest free index when no destination is given", function()
204
+ local controller = setup()
205
+
206
+ controller.seed({
207
+ SaveSlots = {
208
+ slotMetadata = {
209
+ ["slot-a"] = { SlotIndex = 1, SlotName = "Alpha" },
210
+ },
211
+ },
212
+ })
213
+
214
+ local output = controller.run("saveslot-copy", ABSENT_USER_ID, 1, { ABSENT_USER_ID })
215
+ expect(string.find(output, "slot 1 →", 1, true) ~= nil).toEqual(true)
216
+
217
+ -- Inside one roster the copy is suffixed, so it does not sit beside the original under its name.
218
+ local listed = controller.run("saveslot-list", { ABSENT_USER_ID })
219
+ expect(string.find(listed, "Alpha (Copy)", 1, true) ~= nil).toEqual(true)
220
+
221
+ controller.destroy()
222
+ end)
223
+
224
+ it("copies one player's slot onto another player's, in their own datastore", function()
225
+ local controller = setup()
226
+
227
+ controller.seed({
228
+ SaveSlots = {
229
+ slotMetadata = {
230
+ ["slot-a"] = { SlotIndex = 2, SlotName = "Alpha" },
231
+ },
232
+ slots = {
233
+ ["slot-a"] = { coins = 100 },
234
+ },
235
+ },
236
+ })
237
+
238
+ local output = controller.run("saveslot-copy", ABSENT_USER_ID, 2, { OTHER_USER_ID })
239
+ expect(string.find(output, `Copied {ABSENT_USER_ID} slot 2 → {OTHER_USER_ID} slot 2`, 1, true) ~= nil).toEqual(
240
+ true
241
+ )
242
+
243
+ -- Landed in the other player's own key, keeping the source's name -- only a copy inside one
244
+ -- roster is suffixed.
245
+ expect(controller.readRaw(OTHER_USER_ID)).never.toBeNil()
246
+ local listed = controller.run("saveslot-list", { OTHER_USER_ID })
247
+ expect(string.find(listed, '"Alpha" (2)', 1, true) ~= nil).toEqual(true)
248
+
249
+ controller.destroy()
250
+ end)
251
+
252
+ it("copies onto the named destination, overwriting the slot already there", function()
253
+ local controller = setup()
254
+
255
+ controller.seed({
256
+ SaveSlots = {
257
+ slotMetadata = {
258
+ ["slot-a"] = { SlotIndex = 2, SlotName = "Alpha" },
259
+ ["slot-b"] = { SlotIndex = 3, SlotName = "Beta" },
260
+ },
261
+ },
262
+ })
263
+
264
+ local output = controller.run("saveslot-copy", ABSENT_USER_ID, 2, { ABSENT_USER_ID }, 3)
265
+ expect(string.find(output, "overwriting what was there", 1, true) ~= nil).toEqual(true)
266
+
267
+ -- Beta is gone, and slot 3 is now the copy of Alpha rather than a fourth slot.
268
+ local listed = controller.run("saveslot-list", { ABSENT_USER_ID })
269
+ expect(string.find(listed, "Beta", 1, true)).toBeNil()
270
+ expect(string.find(listed, "(4)", 1, true)).toBeNil()
271
+ expect(string.find(listed, 'Alpha (Copy)" (3)', 1, true) ~= nil).toEqual(true)
272
+
273
+ controller.destroy()
274
+ end)
275
+
276
+ it("refuses a copy whose source and destination are the same slot", function()
277
+ local controller = setup()
278
+
279
+ controller.run("saveslot-create", { ABSENT_USER_ID }, { 2 })
280
+
281
+ local output = controller.run("saveslot-copy", ABSENT_USER_ID, 2, { ABSENT_USER_ID }, 2)
282
+ expect(string.find(output, "both the source and the destination", 1, true) ~= nil).toEqual(true)
283
+
284
+ controller.destroy()
285
+ end)
286
+
287
+ it("refuses the main slot as a destination, whose store is the player's root data", function()
288
+ local controller = setup()
289
+
290
+ controller.run("saveslot-create", { ABSENT_USER_ID }, { 1, 2 })
291
+
292
+ local output = controller.run("saveslot-copy", ABSENT_USER_ID, 2, { ABSENT_USER_ID }, 1)
293
+ expect(string.find(output, "main slot (1) cannot be copied onto", 1, true) ~= nil).toEqual(true)
294
+
295
+ controller.destroy()
296
+ end)
297
+
298
+ it("round-trips a slot through export and import, the console's own share-code loop", function()
299
+ local controller = setup()
300
+
301
+ controller.seed({
302
+ SaveSlots = {
303
+ slotMetadata = {
304
+ ["slot-a"] = { SlotIndex = 2, SlotName = "Alpha" },
305
+ },
306
+ slots = {
307
+ ["slot-a"] = { coins = 100 },
308
+ },
309
+ },
310
+ })
311
+
312
+ local exported = controller.run("saveslot-export", { ABSENT_USER_ID }, { 2 })
313
+ local code = string.match(exported, "→ (%S+)")
314
+ expect(code).never.toBeNil()
315
+
316
+ -- Imported onto a second player, which is what a hand-off across the console looks like.
317
+ local output = controller.run("saveslot-import", OTHER_USER_ID, code)
318
+ expect(string.find(output, "Imported save slot from code", 1, true) ~= nil).toEqual(true)
319
+
320
+ local listed = controller.run("saveslot-list", { OTHER_USER_ID })
321
+ expect(string.find(listed, '"Alpha"', 1, true) ~= nil).toEqual(true)
322
+
323
+ controller.destroy()
324
+ end)
325
+
326
+ it("round-trips a slot through read-json and write-json", function()
327
+ local controller = setup()
328
+
329
+ controller.seed({
330
+ SaveSlots = {
331
+ slotMetadata = {
332
+ ["slot-a"] = { SlotIndex = 2, SlotName = "Alpha" },
333
+ },
334
+ slots = {
335
+ ["slot-a"] = { coins = 100 },
336
+ },
337
+ },
338
+ })
339
+
340
+ local read = controller.run("saveslot-read-json", { ABSENT_USER_ID }, { 2 })
341
+ -- The command prefixes a "-- player slot N" comment line; the JSON is the rest.
342
+ local json = string.match(read, "\n(.+)$")
343
+ expect(json).never.toBeNil()
344
+
345
+ local output = controller.run("saveslot-write-json", { ABSENT_USER_ID }, json)
346
+ expect(string.find(output, "wrote JSON → slot 3", 1, true) ~= nil).toEqual(true)
347
+
348
+ local listed = controller.run("saveslot-list", { ABSENT_USER_ID })
349
+ expect(string.find(listed, "(3)", 1, true) ~= nil).toEqual(true)
350
+
351
+ controller.destroy()
352
+ end)
353
+
354
+ it("reports malformed write-json input without touching anyone's data", function()
355
+ local controller = setup()
356
+
357
+ local output = controller.run("saveslot-write-json", { ABSENT_USER_ID }, "not json")
358
+ expect(string.find(output, "could not decode JSON", 1, true) ~= nil).toEqual(true)
359
+ expect(controller.readRaw()).toBeNil()
360
+
361
+ controller.destroy()
362
+ end)
363
+
364
+ it("reports the slot they would resume on, since nothing is selected offline", function()
365
+ local controller = setup()
366
+
367
+ controller.seed({
368
+ SaveSlots = {
369
+ slotMetadata = {
370
+ ["slot-a"] = { SlotIndex = 3, SlotName = "Gamma" },
371
+ },
372
+ activeSlotId = "slot-a",
373
+ },
374
+ })
375
+
376
+ local output = controller.run("saveslot-get-active", { ABSENT_USER_ID })
377
+ expect(string.find(output, "would resume on slot 3", 1, true) ~= nil).toEqual(true)
378
+
379
+ controller.destroy()
380
+ end)
381
+
382
+ it("releases the session, so back-to-back commands both work", function()
383
+ local controller = setup()
384
+
385
+ controller.run("saveslot-create", { ABSENT_USER_ID }, { 1 })
386
+
387
+ -- A leaked session would leave the second command waiting on a lock this server still holds.
388
+ local promise = Promise.new(function(resolve)
389
+ resolve(controller.run("saveslot-create", { ABSENT_USER_ID }, { 2 }))
390
+ end)
391
+ expect(PromiseTestUtils.awaitSettled(promise, 10)).toEqual(true)
392
+
393
+ local listed = controller.run("saveslot-list", { ABSENT_USER_ID })
394
+ expect(string.find(listed, "(1)", 1, true) ~= nil).toEqual(true)
395
+ expect(string.find(listed, "(2)", 1, true) ~= nil).toEqual(true)
396
+
397
+ controller.destroy()
398
+ end)
399
+
400
+ it("reports an empty target list rather than doing nothing quietly", function()
401
+ local controller = setup()
402
+
403
+ expect(controller.run("saveslot-list", {})).toEqual("No players to act on.")
404
+
405
+ controller.destroy()
406
+ end)
407
+
408
+ it("reports a target that is taking a while", function()
409
+ local controller = setup()
410
+
411
+ controller.mock:SetYieldTime(SLOW_REPLY_SECONDS * 2)
412
+
413
+ controller.run("saveslot-list", { ABSENT_USER_ID })
414
+
415
+ expect(#controller.replies).toEqual(1)
416
+ expect(string.find(controller.replies[1], tostring(ABSENT_USER_ID), 1, true) ~= nil).toEqual(true)
417
+
418
+ controller.destroy()
419
+ end)
420
+ end)