@quenty/saveslot 2.10.0 → 2.12.0

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.
@@ -16,6 +16,7 @@ local CmdrReplyUtils = require("CmdrReplyUtils")
16
16
  local DataStoreMock = require("DataStoreMock")
17
17
  local DataStoreTestUtils = require("DataStoreTestUtils")
18
18
  local Jest = require("Jest")
19
+ local JestUtils = require("JestUtils")
19
20
  local Maid = require("Maid")
20
21
  local PlayerDataStoreService = require("PlayerDataStoreService")
21
22
  local Promise = require("Promise")
@@ -103,7 +104,7 @@ local function setup()
103
104
  end,
104
105
  }
105
106
 
106
- return {
107
+ local controller = {
107
108
  mock = mock,
108
109
  replies = replies,
109
110
  run = function(commandName: string, ...)
@@ -115,11 +116,15 @@ local function setup()
115
116
  readRaw = function(userId: number?)
116
117
  return mock:GetRaw(tostring(userId or ABSENT_USER_ID))
117
118
  end,
118
- destroy = function()
119
+ Destroy = function(_self)
119
120
  DataStoreTestUtils.awaitServiceShutdown(playerDataStoreService)
120
121
  maid:DoCleaning()
121
122
  end,
122
123
  }
124
+
125
+ maid:GiveTask(JestUtils.afterThis(controller))
126
+
127
+ return controller
123
128
  end
124
129
 
125
130
  describe("SaveSlotCmdrService against an absent player", function()
@@ -138,7 +143,7 @@ describe("SaveSlotCmdrService against an absent player", function()
138
143
  expect(string.find(output, "Alpha", 1, true) ~= nil).toEqual(true)
139
144
  expect(string.find(output, tostring(ABSENT_USER_ID), 1, true) ~= nil).toEqual(true)
140
145
 
141
- controller.destroy()
146
+ controller:Destroy()
142
147
  end)
143
148
 
144
149
  it("lists a slot's summary and playtime, which is what the listing is read for", function()
@@ -164,7 +169,7 @@ describe("SaveSlotCmdrService against an absent player", function()
164
169
  -- The bug this replaced: a structured summary printed as "table: 0x...".
165
170
  expect(string.find(output, "table: 0x", 1, true)).toBeNil()
166
171
 
167
- controller.destroy()
172
+ controller:Destroy()
168
173
  end)
169
174
 
170
175
  it("creates a slot that lands in their datastore", function()
@@ -182,7 +187,7 @@ describe("SaveSlotCmdrService against an absent player", function()
182
187
  local listed = controller.run("saveslot-list", { ABSENT_USER_ID })
183
188
  expect(string.find(listed, "(2)", 1, true) ~= nil).toEqual(true)
184
189
 
185
- controller.destroy()
190
+ controller:Destroy()
186
191
  end)
187
192
 
188
193
  it("deletes a stored slot", function()
@@ -197,7 +202,7 @@ describe("SaveSlotCmdrService against an absent player", function()
197
202
  expect(string.find(listed, "(1)", 1, true)).toBeNil()
198
203
  expect(string.find(listed, "(2)", 1, true) ~= nil).toEqual(true)
199
204
 
200
- controller.destroy()
205
+ controller:Destroy()
201
206
  end)
202
207
 
203
208
  it("copies a slot into the lowest free index when no destination is given", function()
@@ -218,7 +223,7 @@ describe("SaveSlotCmdrService against an absent player", function()
218
223
  local listed = controller.run("saveslot-list", { ABSENT_USER_ID })
219
224
  expect(string.find(listed, "Alpha (Copy)", 1, true) ~= nil).toEqual(true)
220
225
 
221
- controller.destroy()
226
+ controller:Destroy()
222
227
  end)
223
228
 
224
229
  it("copies one player's slot onto another player's, in their own datastore", function()
@@ -246,7 +251,7 @@ describe("SaveSlotCmdrService against an absent player", function()
246
251
  local listed = controller.run("saveslot-list", { OTHER_USER_ID })
247
252
  expect(string.find(listed, '"Alpha" (2)', 1, true) ~= nil).toEqual(true)
248
253
 
249
- controller.destroy()
254
+ controller:Destroy()
250
255
  end)
251
256
 
252
257
  it("copies onto the named destination, overwriting the slot already there", function()
@@ -270,7 +275,7 @@ describe("SaveSlotCmdrService against an absent player", function()
270
275
  expect(string.find(listed, "(4)", 1, true)).toBeNil()
271
276
  expect(string.find(listed, 'Alpha (Copy)" (3)', 1, true) ~= nil).toEqual(true)
272
277
 
273
- controller.destroy()
278
+ controller:Destroy()
274
279
  end)
275
280
 
276
281
  it("refuses a copy whose source and destination are the same slot", function()
@@ -281,7 +286,7 @@ describe("SaveSlotCmdrService against an absent player", function()
281
286
  local output = controller.run("saveslot-copy", ABSENT_USER_ID, 2, { ABSENT_USER_ID }, 2)
282
287
  expect(string.find(output, "both the source and the destination", 1, true) ~= nil).toEqual(true)
283
288
 
284
- controller.destroy()
289
+ controller:Destroy()
285
290
  end)
286
291
 
287
292
  it("refuses the main slot as a destination, whose store is the player's root data", function()
@@ -292,7 +297,7 @@ describe("SaveSlotCmdrService against an absent player", function()
292
297
  local output = controller.run("saveslot-copy", ABSENT_USER_ID, 2, { ABSENT_USER_ID }, 1)
293
298
  expect(string.find(output, "main slot (1) cannot be copied onto", 1, true) ~= nil).toEqual(true)
294
299
 
295
- controller.destroy()
300
+ controller:Destroy()
296
301
  end)
297
302
 
298
303
  it("round-trips a slot through export and import, the console's own share-code loop", function()
@@ -320,7 +325,7 @@ describe("SaveSlotCmdrService against an absent player", function()
320
325
  local listed = controller.run("saveslot-list", { OTHER_USER_ID })
321
326
  expect(string.find(listed, '"Alpha"', 1, true) ~= nil).toEqual(true)
322
327
 
323
- controller.destroy()
328
+ controller:Destroy()
324
329
  end)
325
330
 
326
331
  it("round-trips a slot through read-json and write-json", function()
@@ -348,7 +353,7 @@ describe("SaveSlotCmdrService against an absent player", function()
348
353
  local listed = controller.run("saveslot-list", { ABSENT_USER_ID })
349
354
  expect(string.find(listed, "(3)", 1, true) ~= nil).toEqual(true)
350
355
 
351
- controller.destroy()
356
+ controller:Destroy()
352
357
  end)
353
358
 
354
359
  it("reports malformed write-json input without touching anyone's data", function()
@@ -358,7 +363,7 @@ describe("SaveSlotCmdrService against an absent player", function()
358
363
  expect(string.find(output, "could not decode JSON", 1, true) ~= nil).toEqual(true)
359
364
  expect(controller.readRaw()).toBeNil()
360
365
 
361
- controller.destroy()
366
+ controller:Destroy()
362
367
  end)
363
368
 
364
369
  it("reports the slot they would resume on, since nothing is selected offline", function()
@@ -376,7 +381,7 @@ describe("SaveSlotCmdrService against an absent player", function()
376
381
  local output = controller.run("saveslot-get-active", { ABSENT_USER_ID })
377
382
  expect(string.find(output, "would resume on slot 3", 1, true) ~= nil).toEqual(true)
378
383
 
379
- controller.destroy()
384
+ controller:Destroy()
380
385
  end)
381
386
 
382
387
  it("releases the session, so back-to-back commands both work", function()
@@ -394,7 +399,7 @@ describe("SaveSlotCmdrService against an absent player", function()
394
399
  expect(string.find(listed, "(1)", 1, true) ~= nil).toEqual(true)
395
400
  expect(string.find(listed, "(2)", 1, true) ~= nil).toEqual(true)
396
401
 
397
- controller.destroy()
402
+ controller:Destroy()
398
403
  end)
399
404
 
400
405
  it("reports an empty target list rather than doing nothing quietly", function()
@@ -402,7 +407,7 @@ describe("SaveSlotCmdrService against an absent player", function()
402
407
 
403
408
  expect(controller.run("saveslot-list", {})).toEqual("No players to act on.")
404
409
 
405
- controller.destroy()
410
+ controller:Destroy()
406
411
  end)
407
412
 
408
413
  it("reports a target that is taking a while", function()
@@ -415,6 +420,6 @@ describe("SaveSlotCmdrService against an absent player", function()
415
420
  expect(#controller.replies).toEqual(1)
416
421
  expect(string.find(controller.replies[1], tostring(ABSENT_USER_ID), 1, true) ~= nil).toEqual(true)
417
422
 
418
- controller.destroy()
423
+ controller:Destroy()
419
424
  end)
420
425
  end)
@@ -1376,7 +1376,7 @@ end
1376
1376
  -- (metatable-stripped) object -- or into its destroyed stores -- throws, and because a datastore load
1377
1377
  -- resolves inside its UpdateAsync transform, that throw surfaces as a "Transform function error" and takes
1378
1378
  -- the transform's write down with it. The liveness checks cover a settle that lands mid-teardown, before
1379
- -- this promise's own maid task was reached. See SaveSlotLateSettle.spec.
1379
+ -- this promise's own maid task was reached. See SaveSlotService.LateSettle.spec.
1380
1380
  function HasSaveSlotsDataStore._promiseLoadSlots(
1381
1381
  self: HasSaveSlotsDataStore,
1382
1382
  promiseDataStore: Promise.Promise<any>
@@ -11,6 +11,7 @@ local require = require(script.Parent.loader).load(script)
11
11
  local DataStoreMock = require("DataStoreMock")
12
12
  local DataStoreTestUtils = require("DataStoreTestUtils")
13
13
  local Jest = require("Jest")
14
+ local JestUtils = require("JestUtils")
14
15
  local Maid = require("Maid")
15
16
  local PlayerDataStoreService = require("PlayerDataStoreService")
16
17
  local PromiseTestUtils = require("PromiseTestUtils")
@@ -49,7 +50,7 @@ local function setup()
49
50
  return value
50
51
  end
51
52
 
52
- return {
53
+ local controller = {
53
54
  serviceBag = serviceBag,
54
55
  saveSlotService = saveSlotService,
55
56
  mock = mock,
@@ -63,13 +64,17 @@ local function setup()
63
64
  end
64
65
  return offline
65
66
  end,
66
- destroy = function()
67
+ Destroy = function(_self)
67
68
  -- A PlayerMock never fires the real Players.PlayerRemoving, and an offline store is only
68
69
  -- released by its handle, so shut down the way Roblox does before tearing the bag down.
69
70
  DataStoreTestUtils.awaitServiceShutdown(playerDataStoreService)
70
71
  maid:DoCleaning()
71
72
  end,
72
73
  }
74
+
75
+ maid:GiveTask(JestUtils.afterThis(controller))
76
+
77
+ return controller
73
78
  end
74
79
 
75
80
  describe("SaveSlotService.PromiseOfflineSaveSlots", function()
@@ -101,7 +106,7 @@ describe("SaveSlotService.PromiseOfflineSaveSlots", function()
101
106
  expect(slots:GetLastActiveSlotId()).toEqual("slot-a")
102
107
 
103
108
  offline:Destroy()
104
- controller.destroy()
109
+ controller:Destroy()
105
110
  end)
106
111
 
107
112
  it("writes a slot created offline into the player's datastore", function()
@@ -120,7 +125,7 @@ describe("SaveSlotService.PromiseOfflineSaveSlots", function()
120
125
  expect(slotList[1].SlotIndex).toEqual(1)
121
126
 
122
127
  reopened:Destroy()
123
- controller.destroy()
128
+ controller:Destroy()
124
129
  end)
125
130
 
126
131
  it("releases the borrowed session, so the next open builds a fresh one", function()
@@ -135,7 +140,7 @@ describe("SaveSlotService.PromiseOfflineSaveSlots", function()
135
140
  expect(second:GetSlotsDataStore() == firstStore).toEqual(false)
136
141
 
137
142
  second:Destroy()
138
- controller.destroy()
143
+ controller:Destroy()
139
144
  end)
140
145
 
141
146
  -- The refusal for a player who *is* in this server is not covered: the guard reads
@@ -156,7 +161,7 @@ describe("SaveSlotService.PromiseOfflineSaveSlots", function()
156
161
  expect(metadata.TimePlayed).toBeNil()
157
162
 
158
163
  offline:Destroy()
159
- controller.destroy()
164
+ controller:Destroy()
160
165
  end)
161
166
 
162
167
  it("survives being destroyed twice", function()
@@ -169,6 +174,6 @@ describe("SaveSlotService.PromiseOfflineSaveSlots", function()
169
174
  offline:Destroy()
170
175
  end).never.toThrow()
171
176
 
172
- controller.destroy()
177
+ controller:Destroy()
173
178
  end)
174
179
  end)
@@ -7,7 +7,7 @@
7
7
  stray error that fails the whole run. The chain must consume a late settle silently: every
8
8
  hop is maid-owned (cancelled with the binder's brio) and re-checks the binder is alive.
9
9
 
10
- @class SaveSlotLateSettle.spec.lua
10
+ @class SaveSlotService.LateSettle.spec.lua
11
11
  ]]
12
12
  local require = require(script.Parent.loader).load(script)
13
13
 
@@ -16,13 +16,14 @@ local Workspace = game:GetService("Workspace")
16
16
  local DataStoreMock = require("DataStoreMock")
17
17
  local DataStoreTestUtils = require("DataStoreTestUtils")
18
18
  local Jest = require("Jest")
19
+ local JestUtils = require("JestUtils")
19
20
  local Maid = require("Maid")
21
+ local PlayerDataStoreService = require("PlayerDataStoreService")
20
22
  local PlayerMock = require("PlayerMock")
21
23
  local PromiseTestUtils = require("PromiseTestUtils")
22
24
  local SaveSlotConstants = require("SaveSlotConstants")
23
25
  local ServiceBag = require("ServiceBag")
24
26
 
25
- local afterEach = Jest.Globals.afterEach
26
27
  local describe = Jest.Globals.describe
27
28
  local expect = Jest.Globals.expect
28
29
  local it = Jest.Globals.it
@@ -31,20 +32,11 @@ local USER_ID = 636363
31
32
  local EXISTING_SLOT_ID = "e6f0c1a2-late-settle"
32
33
 
33
34
  -- Every datastore read yields this long, so the spec can land the unbind inside a chosen
34
- -- read's in-flight window.
35
- local READ_YIELD_SECONDS = 0.5
36
-
37
- local activeController: any = nil
38
-
39
- afterEach(function()
40
- -- Safety net for a failing test: destroy is idempotent, so this is a no-op after the test's
41
- -- own controller:destroy() and a full teardown when an assertion threw before reaching it.
42
- if activeController then
43
- local controller = activeController
44
- activeController = nil
45
- controller:destroy()
46
- end
47
- end)
35
+ -- read's in-flight window. Kept as short as the window can be while still spanning several frames:
36
+ -- these four tests otherwise spend their whole runtime waiting out real datastore latency, and the
37
+ -- window is guarded -- a read that has already settled fails the awaitSettled assertion before the
38
+ -- unbind, rather than quietly reducing the test to nothing.
39
+ local READ_YIELD_SECONDS = 0.15
48
40
 
49
41
  local function setup()
50
42
  local maid = Maid.new()
@@ -53,8 +45,9 @@ local function setup()
53
45
  mock:SetYieldTime(READ_YIELD_SECONDS)
54
46
 
55
47
  local serviceBag = maid:Add(ServiceBag.new())
56
- local playerDataStoreService: any = serviceBag:GetService(require("PlayerDataStoreService"))
57
- local hasSaveSlotsBinder: any = serviceBag:GetService(require("HasSaveSlots"))
48
+ local playerDataStoreService: PlayerDataStoreService.PlayerDataStoreService =
49
+ serviceBag:GetService(PlayerDataStoreService) :: any
50
+ local hasSaveSlotsBinder = serviceBag:GetService(require("HasSaveSlots"))
58
51
  serviceBag:GetService(require("SaveSlotService"))
59
52
  serviceBag:Init()
60
53
  playerDataStoreService:SetRobloxDataStore(mock)
@@ -63,66 +56,62 @@ local function setup()
63
56
  local fakePlayer = maid:Add(PlayerMock.new({ UserId = USER_ID }))
64
57
  fakePlayer.Parent = Workspace
65
58
 
66
- local destroyed = false
67
- local controller: any
68
- controller = {
69
- hasSaveSlotsBinder = hasSaveSlotsBinder,
70
- fakePlayer = fakePlayer,
59
+ local function Destroy(_self)
60
+ -- The store the spec loaded is only destroyed by a removal, and a PlayerMock never fires the
61
+ -- real Players.PlayerRemoving, so shut down the way Roblox does or its auto-save loop outlives
62
+ -- this spec and fires inside a later package's window.
63
+ DataStoreTestUtils.awaitServiceShutdown(playerDataStoreService)
64
+ serviceBag:Destroy()
65
+ maid:DoCleaning()
66
+ -- Any straggler continuation blows up here, inside the test that owns it, rather
67
+ -- than as flake in a later suite.
68
+ task.wait(READ_YIELD_SECONDS * 3)
69
+ end
70
+
71
+ local controller = {
71
72
  mock = mock,
72
- destroy = function(self: any)
73
- if destroyed then
74
- return
75
- end
76
- destroyed = true
77
- if activeController == self then
78
- activeController = nil
79
- end
80
- -- The store the spec loaded is only destroyed by a removal, and a PlayerMock never fires the
81
- -- real Players.PlayerRemoving, so shut down the way Roblox does or its auto-save loop outlives
82
- -- this spec and fires inside a later package's window.
83
- DataStoreTestUtils.awaitServiceShutdown(playerDataStoreService)
84
- serviceBag:Destroy()
85
- maid:DoCleaning()
86
- -- Any straggler continuation blows up here, inside the test that owns it, rather
87
- -- than as flake in a later suite.
88
- task.wait(READ_YIELD_SECONDS * 3)
73
+ -- A returning player's persisted slot, written straight into the mock the way a previous
74
+ -- session left it.
75
+ seedExistingSlot = function()
76
+ mock:SetRaw(tostring(USER_ID), {
77
+ [SaveSlotConstants.SYSTEM_STORE_KEY] = {
78
+ [SaveSlotConstants.METADATA_STORE_KEY] = {
79
+ [EXISTING_SLOT_ID] = {
80
+ SlotIndex = SaveSlotConstants.DEFAULT_SLOT_INDEX,
81
+ SlotName = "Slot 1",
82
+ CreatedTime = 1,
83
+ },
84
+ },
85
+ activeSlotId = EXISTING_SLOT_ID,
86
+ },
87
+ })
88
+ end,
89
+ join = function()
90
+ return assert(hasSaveSlotsBinder:Bind(fakePlayer), "Failed to bind HasSaveSlots")
89
91
  end,
92
+ leave = function()
93
+ hasSaveSlotsBinder:Unbind(fakePlayer)
94
+ end,
95
+ Destroy = Destroy,
90
96
  }
91
- activeController = controller
92
97
 
93
- return controller
94
- end
98
+ maid:GiveTask(JestUtils.afterThis(controller))
95
99
 
96
- -- A returning player's persisted slot, written straight into the mock the way a previous session left it.
97
- local function seedExistingSlot(controller: any)
98
- controller.mock:SetRaw(tostring(USER_ID), {
99
- [SaveSlotConstants.SYSTEM_STORE_KEY] = {
100
- [SaveSlotConstants.METADATA_STORE_KEY] = {
101
- [EXISTING_SLOT_ID] = {
102
- SlotIndex = SaveSlotConstants.DEFAULT_SLOT_INDEX,
103
- SlotName = "Slot 1",
104
- CreatedTime = 1,
105
- },
106
- },
107
- activeSlotId = EXISTING_SLOT_ID,
108
- },
109
- })
100
+ return controller
110
101
  end
111
102
 
112
103
  describe("SaveSlotService selection chain vs a player who leaves mid-load", function()
113
104
  it("consumes a slots-load still pending when the binder dies", function()
114
105
  local controller = setup()
115
106
 
116
- local hasSaveSlots =
117
- assert(controller.hasSaveSlotsBinder:Bind(controller.fakePlayer), "Failed to bind HasSaveSlots")
118
- local slotsLoaded = hasSaveSlots:PromiseSlotsLoaded()
107
+ local slotsLoaded = controller.join():PromiseSlotsLoaded()
119
108
 
120
109
  -- Let the service's chain attach to the (still pending) load, then "leave".
121
110
  task.wait()
122
111
  expect(PromiseTestUtils.awaitSettled(slotsLoaded, 0)).toEqual(false)
123
- controller.hasSaveSlotsBinder:Unbind(controller.fakePlayer)
112
+ controller.leave()
124
113
 
125
- controller:destroy()
114
+ controller:Destroy()
126
115
  end)
127
116
 
128
117
  -- Pins that seedExistingSlot really lands where the load reads it. Without this, drift in the raw
@@ -130,10 +119,9 @@ describe("SaveSlotService selection chain vs a player who leaves mid-load", func
130
119
  -- replace -- green, and no longer reaching _buildSlot at all.
131
120
  it("loads the seeded slot when the player stays", function()
132
121
  local controller = setup()
133
- seedExistingSlot(controller)
122
+ controller.seedExistingSlot()
134
123
 
135
- local hasSaveSlots =
136
- assert(controller.hasSaveSlotsBinder:Bind(controller.fakePlayer), "Failed to bind HasSaveSlots")
124
+ local hasSaveSlots = controller.join()
137
125
  expect(PromiseTestUtils.awaitSettled(hasSaveSlots:PromiseSlotsLoaded(), 10)).toEqual(true)
138
126
 
139
127
  local hasSlotStatus, hasSlot = PromiseTestUtils.awaitOutcome(hasSaveSlots:PromiseHasSlot(EXISTING_SLOT_ID), 10)
@@ -146,7 +134,7 @@ describe("SaveSlotService selection chain vs a player who leaves mid-load", func
146
134
  return hasSaveSlots.ActiveSlotId.Value == EXISTING_SLOT_ID
147
135
  end, 10)).toEqual(true)
148
136
 
149
- controller:destroy()
137
+ controller:Destroy()
150
138
  end)
151
139
 
152
140
  it("consumes a returning player's slots-load that settles after the binder died", function()
@@ -155,24 +143,21 @@ describe("SaveSlotService selection chain vs a player who leaves mid-load", func
155
143
  -- A returning player, so the load settles with metadata to build slots from. The empty-metadata
156
144
  -- case above never reaches _buildSlot, which is how a live server kept throwing there
157
145
  -- ("attempt to call missing method '_buildSlot'") while that test stayed green.
158
- seedExistingSlot(controller)
146
+ controller.seedExistingSlot()
159
147
 
160
- local hasSaveSlots =
161
- assert(controller.hasSaveSlotsBinder:Bind(controller.fakePlayer), "Failed to bind HasSaveSlots")
162
- local slotsLoaded = hasSaveSlots:PromiseSlotsLoaded()
148
+ local slotsLoaded = controller.join():PromiseSlotsLoaded()
163
149
 
164
150
  task.wait()
165
151
  expect(PromiseTestUtils.awaitSettled(slotsLoaded, 0)).toEqual(false)
166
- controller.hasSaveSlotsBinder:Unbind(controller.fakePlayer)
152
+ controller.leave()
167
153
 
168
- controller:destroy()
154
+ controller:Destroy()
169
155
  end)
170
156
 
171
157
  it("consumes a default-slot read that settles after the binder died", function()
172
158
  local controller = setup()
173
159
 
174
- local hasSaveSlots =
175
- assert(controller.hasSaveSlotsBinder:Bind(controller.fakePlayer), "Failed to bind HasSaveSlots")
160
+ local hasSaveSlots = controller.join()
176
161
 
177
162
  -- Play the chain forward to the default-slot resolution: slots loaded, teleport read
178
163
  -- consumed, and the last-active-slot datastore read now in its in-flight window.
@@ -182,8 +167,8 @@ describe("SaveSlotService selection chain vs a player who leaves mid-load", func
182
167
  -- The player leaves inside that window; the read settles after the binder is gone.
183
168
  -- With the regression, the settle's continuation calls the destroyed binder and a
184
169
  -- stray "attempt to call missing method" error fails the run.
185
- controller.hasSaveSlotsBinder:Unbind(controller.fakePlayer)
170
+ controller.leave()
186
171
 
187
- controller:destroy()
172
+ controller:Destroy()
188
173
  end)
189
174
  end)
@@ -0,0 +1,117 @@
1
+ --!strict
2
+ --[[
3
+ Characterizes the save-slot load flow -- the substore reads HasSaveSlots performs -- driven
4
+ through a mock-injected PlayerDataStoreService. A headless cloud test server has no real Players
5
+ to bind the PlayerBinder to, so these reproduce the load against the same datastore substore
6
+ layout (SaveSlotConstants) with a numeric userId.
7
+
8
+ @class SaveSlotService.LoadFlow.spec.lua
9
+ ]]
10
+ local require = require(script.Parent.loader).load(script)
11
+
12
+ local DataStore = require("DataStore")
13
+ local DataStoreMock = require("DataStoreMock")
14
+ local DataStoreTestUtils = require("DataStoreTestUtils")
15
+ local Jest = require("Jest")
16
+ local JestUtils = require("JestUtils")
17
+ local Maid = require("Maid")
18
+ local PlayerDataStoreService = require("PlayerDataStoreService")
19
+ local PromiseTestUtils = require("PromiseTestUtils")
20
+ local SaveSlotConstants = require("SaveSlotConstants")
21
+ local ServiceBag = require("ServiceBag")
22
+
23
+ local describe = Jest.Globals.describe
24
+ local expect = Jest.Globals.expect
25
+ local it = Jest.Globals.it
26
+
27
+ local USER_ID = 1
28
+
29
+ local function setup(mock: DataStoreMock.DataStoreMock?)
30
+ local maid = Maid.new()
31
+ local dataStoreMock = mock or DataStoreMock.new()
32
+
33
+ local serviceBag = maid:Add(ServiceBag.new())
34
+ local playerDataStoreService: PlayerDataStoreService.PlayerDataStoreService =
35
+ serviceBag:GetService(PlayerDataStoreService) :: any
36
+ serviceBag:Init()
37
+ playerDataStoreService:SetRobloxDataStore(dataStoreMock)
38
+ serviceBag:Start()
39
+
40
+ -- The manager hands back a datastore without loading it, so this resolves even when every
41
+ -- request is set to fail -- the failure surfaces on the first read instead.
42
+ local status, resolved = PromiseTestUtils.awaitOutcome(playerDataStoreService:PromiseDataStore(USER_ID), 10)
43
+ assert(status == "resolved", `Failed to resolve the player data store ({status})`)
44
+
45
+ local dataStore: DataStore.DataStore = resolved
46
+ local systemStore = dataStore:GetSubStore(SaveSlotConstants.SYSTEM_STORE_KEY)
47
+
48
+ local function Destroy(_self)
49
+ -- The store the spec loaded is only destroyed by a removal, and a PlayerMock never fires the
50
+ -- real Players.PlayerRemoving, so shut down the way Roblox does or its auto-save loop outlives
51
+ -- this spec and fires inside a later package's window.
52
+ DataStoreTestUtils.awaitServiceShutdown(playerDataStoreService)
53
+ maid:DoCleaning()
54
+ end
55
+
56
+ local controller = {
57
+ mock = dataStoreMock,
58
+ dataStore = dataStore,
59
+ systemStore = systemStore,
60
+ metadataStore = systemStore:GetSubStore(SaveSlotConstants.METADATA_STORE_KEY),
61
+ getSlotStore = function(slotId: string)
62
+ return systemStore:GetSubStore(SaveSlotConstants.SLOT_STORE_KEY):GetSubStore(slotId)
63
+ end,
64
+ Destroy = Destroy,
65
+ }
66
+
67
+ maid:GiveTask(JestUtils.afterThis(controller))
68
+
69
+ return controller
70
+ end
71
+
72
+ describe("save slot load flow (healthy datastore)", function()
73
+ it("loads empty slot metadata and no active slot for a fresh user", function()
74
+ local controller = setup()
75
+
76
+ local metadataStatus, metadata = PromiseTestUtils.awaitOutcome(controller.metadataStore:LoadAll({}), 10)
77
+ expect(metadataStatus).toEqual("resolved")
78
+ expect(metadata).toEqual({})
79
+
80
+ local activeStatus, activeSlotId =
81
+ PromiseTestUtils.awaitOutcome(controller.systemStore:Load("activeSlotId"), 10)
82
+ expect(activeStatus).toEqual("resolved")
83
+ expect(activeSlotId).toEqual(nil)
84
+
85
+ controller:Destroy()
86
+ end)
87
+
88
+ it("round-trips a slot's data through the slot substore", function()
89
+ local controller = setup()
90
+
91
+ local slotStore = controller.getSlotStore("slot-abc")
92
+ slotStore:Store("coins", 25)
93
+
94
+ local saveStatus = PromiseTestUtils.awaitOutcome(controller.dataStore:Save(), 10)
95
+ expect(saveStatus).toEqual("resolved")
96
+
97
+ local loadStatus, coins = PromiseTestUtils.awaitOutcome(slotStore:Load("coins"), 10)
98
+ expect(loadStatus).toEqual("resolved")
99
+ expect(coins).toEqual(25)
100
+
101
+ controller:Destroy()
102
+ end)
103
+ end)
104
+
105
+ describe("save slot load flow (datastore down)", function()
106
+ it("surfaces an error fast instead of hanging when datastores are down", function()
107
+ local mock = DataStoreMock.new()
108
+ mock:FailAllRequests()
109
+
110
+ local controller = setup(mock)
111
+
112
+ local status = PromiseTestUtils.awaitOutcome(controller.metadataStore:LoadAll({}), 5)
113
+ expect(status).toEqual("rejected")
114
+
115
+ controller:Destroy()
116
+ end)
117
+ end)