@quenty/saveslot 2.14.3 → 2.15.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.
@@ -7,8 +7,8 @@
7
7
  ]]
8
8
  local require = require(script.Parent.loader).load(script)
9
9
 
10
+ local BindToCloseService = require("BindToCloseService")
10
11
  local DataStoreMock = require("DataStoreMock")
11
- local DataStoreTestUtils = require("DataStoreTestUtils")
12
12
  local Jest = require("Jest")
13
13
  local JestUtils = require("JestUtils")
14
14
  local Maid = require("Maid")
@@ -36,6 +36,7 @@ local function setup()
36
36
 
37
37
  local serviceBag = ServiceBag.new()
38
38
  serviceBag:GetService(require("TeleportDataService"))
39
+ local bindToCloseService: BindToCloseService.BindToCloseService = serviceBag:GetService(BindToCloseService) :: any
39
40
  local playerDataStoreService: PlayerDataStoreService.PlayerDataStoreService =
40
41
  serviceBag:GetService(PlayerDataStoreService) :: any
41
42
  local sharedService: SaveSlotSharedDataStoreService.SaveSlotSharedDataStoreService =
@@ -52,10 +53,8 @@ local function setup()
52
53
  local hasSaveSlots = assert(binder:Bind(fakePlayer), "Failed to bind HasSaveSlots")
53
54
  hasSaveSlots.MaxSlotCount.Value = 5
54
55
 
55
- -- A PlayerMock never fires the real Players.PlayerRemoving, and the store the spec loaded is only
56
- -- destroyed by a removal, so shut it down the way Roblox does or its auto-save loop outlives this spec.
57
56
  maid:GiveTask(function()
58
- DataStoreTestUtils.awaitServiceShutdown(playerDataStoreService)
57
+ PromiseTestUtils.awaitFulfilled(bindToCloseService:PromiseClose())
59
58
  fakePlayer:Destroy()
60
59
  serviceBag:Destroy()
61
60
  end)
@@ -77,37 +76,10 @@ local function setup()
77
76
  return controller
78
77
  end
79
78
 
80
- local function runWithContext(body)
81
- local context = setup()
82
- local ok, err = pcall(body, context)
83
- context:Destroy()
84
- if not ok then
85
- error(err, 0)
86
- end
87
- end
88
-
89
- local function awaitValueOf(promise)
90
- if not PromiseTestUtils.awaitSettled(promise, 10) then
91
- error("promise hung", 0)
92
- end
93
- local ok, value = promise:Yield()
94
- if not ok then
95
- error(`promise rejected: {tostring(value)}`, 0)
96
- end
97
- return value
98
- end
99
-
100
- local function awaitResolved(promise): boolean
101
- if not PromiseTestUtils.awaitSettled(promise, 10) then
102
- error("promise hung", 0)
103
- end
104
- return (promise:Yield())
105
- end
106
-
107
79
  local function createSelectAndWrite(hasSaveSlots, slotIndex: number): string
108
- local slotId = awaitValueOf(hasSaveSlots:PromiseCreateSlot(slotIndex))
109
- awaitValueOf(hasSaveSlots:PromiseSelectSlot(slotId))
110
- local store = awaitValueOf(hasSaveSlots:PromiseActiveSlotStore())
80
+ local slotId = PromiseTestUtils.awaitFulfilled(hasSaveSlots:PromiseCreateSlot(slotIndex))
81
+ PromiseTestUtils.awaitFulfilled(hasSaveSlots:PromiseSelectSlot(slotId))
82
+ local store = PromiseTestUtils.awaitFulfilled(hasSaveSlots:PromiseActiveSlotStore())
111
83
  store:Store("Coins", 7)
112
84
  store:GetSubStore("World_2"):Store("Eggs", 3)
113
85
  return slotId
@@ -115,92 +87,98 @@ end
115
87
 
116
88
  describe("HasSaveSlots shared-store save/import", function()
117
89
  it("saves a non-main slot to the shared store and imports it into a fresh non-main slot", function()
118
- runWithContext(function(context)
119
- local hasSaveSlots = context.hasSaveSlots
90
+ local context = setup()
91
+ local hasSaveSlots = context.hasSaveSlots
92
+
93
+ local sourceSlotId = createSelectAndWrite(hasSaveSlots, 2)
94
+ PromiseTestUtils.awaitFulfilled(hasSaveSlots:PromiseSaveSlotToSharedDataStore(sourceSlotId, "code-abc"))
120
95
 
121
- local sourceSlotId = createSelectAndWrite(hasSaveSlots, 2)
122
- awaitValueOf(hasSaveSlots:PromiseSaveSlotToSharedDataStore(sourceSlotId, "code-abc"))
96
+ local newSlotId = PromiseTestUtils.awaitFulfilled(hasSaveSlots:PromiseImportSlotFromSharedDataStore("code-abc"))
97
+ expect(newSlotId).never.toEqual(sourceSlotId)
123
98
 
124
- local newSlotId = awaitValueOf(hasSaveSlots:PromiseImportSlotFromSharedDataStore("code-abc"))
125
- expect(newSlotId).never.toEqual(sourceSlotId)
99
+ local reexport = PromiseTestUtils.awaitFulfilled(hasSaveSlots:PromiseExportSlot(newSlotId))
100
+ expect(reexport.data.Coins).toEqual(7)
101
+ expect(reexport.data.World_2.Eggs).toEqual(3)
126
102
 
127
- local reexport = awaitValueOf(hasSaveSlots:PromiseExportSlot(newSlotId))
128
- expect(reexport.data.Coins).toEqual(7)
129
- expect(reexport.data.World_2.Eggs).toEqual(3)
130
- end)
103
+ context:Destroy()
131
104
  end)
132
105
 
133
106
  it("rejects import from a missing key", function()
134
- runWithContext(function(context)
135
- expect(awaitResolved(context.hasSaveSlots:PromiseImportSlotFromSharedDataStore("nope"))).toEqual(false)
136
- end)
107
+ local context = setup()
108
+ PromiseTestUtils.awaitRejected(context.hasSaveSlots:PromiseImportSlotFromSharedDataStore("nope"))
109
+
110
+ context:Destroy()
137
111
  end)
138
112
 
139
113
  it("tags what it writes as a share code, keeping it out of the teleport arrival path", function()
140
- runWithContext(function(context)
141
- local hasSaveSlots = context.hasSaveSlots
142
- local sourceSlotId = createSelectAndWrite(hasSaveSlots, 2)
143
- awaitValueOf(hasSaveSlots:PromiseSaveSlotToSharedDataStore(sourceSlotId, "code-kind"))
144
-
145
- local stored = awaitValueOf(context.sharedService:PromiseRead("code-kind"))
146
- expect(stored.kind).toEqual(SaveSlotExportUtils.Kind.CODE)
147
- end)
114
+ local context = setup()
115
+ local hasSaveSlots = context.hasSaveSlots
116
+ local sourceSlotId = createSelectAndWrite(hasSaveSlots, 2)
117
+ PromiseTestUtils.awaitFulfilled(hasSaveSlots:PromiseSaveSlotToSharedDataStore(sourceSlotId, "code-kind"))
118
+
119
+ local stored = PromiseTestUtils.awaitFulfilled(context.sharedService:PromiseRead("code-kind"))
120
+ expect(stored.kind).toEqual(SaveSlotExportUtils.Kind.CODE)
121
+
122
+ context:Destroy()
148
123
  end)
149
124
 
150
125
  it("refuses to import a transfer snapshot, whose key every client can read", function()
151
- runWithContext(function(context)
152
- awaitValueOf(
153
- context.sharedService:PromiseWrite(
154
- "transfer-key",
155
- SaveSlotExportUtils.withKind({ data = { Coins = 5 } }, SaveSlotExportUtils.Kind.TRANSFER)
156
- )
126
+ local context = setup()
127
+ PromiseTestUtils.awaitFulfilled(
128
+ context.sharedService:PromiseWrite(
129
+ "transfer-key",
130
+ SaveSlotExportUtils.withKind({ data = { Coins = 5 } }, SaveSlotExportUtils.Kind.TRANSFER)
157
131
  )
132
+ )
158
133
 
159
- expect(awaitResolved(context.hasSaveSlots:PromiseImportSlotFromSharedDataStore("transfer-key"))).toEqual(
160
- false
161
- )
162
- end)
134
+ PromiseTestUtils.awaitRejected(context.hasSaveSlots:PromiseImportSlotFromSharedDataStore("transfer-key"))
135
+
136
+ context:Destroy()
163
137
  end)
164
138
 
165
139
  it("still imports an entry written before kinds existed", function()
166
- runWithContext(function(context)
167
- awaitValueOf(context.sharedService:PromiseWrite("legacy-code", { data = { Coins = 5 } }))
140
+ local context = setup()
141
+ PromiseTestUtils.awaitFulfilled(context.sharedService:PromiseWrite("legacy-code", { data = { Coins = 5 } }))
142
+
143
+ local newSlotId =
144
+ PromiseTestUtils.awaitFulfilled(context.hasSaveSlots:PromiseImportSlotFromSharedDataStore("legacy-code"))
145
+ local reexport = PromiseTestUtils.awaitFulfilled(context.hasSaveSlots:PromiseExportSlot(newSlotId))
146
+ expect(reexport.data.Coins).toEqual(5)
168
147
 
169
- local newSlotId = awaitValueOf(context.hasSaveSlots:PromiseImportSlotFromSharedDataStore("legacy-code"))
170
- local reexport = awaitValueOf(context.hasSaveSlots:PromiseExportSlot(newSlotId))
171
- expect(reexport.data.Coins).toEqual(5)
172
- end)
148
+ context:Destroy()
173
149
  end)
174
150
 
175
151
  it("propagates the main-slot guard through a shared-store save", function()
176
- runWithContext(function(context)
177
- local hasSaveSlots = context.hasSaveSlots
152
+ local context = setup()
153
+ local hasSaveSlots = context.hasSaveSlots
178
154
 
179
- local mainSlotId = awaitValueOf(hasSaveSlots:PromiseCreateSlot(SaveSlotConstants.DEFAULT_SLOT_INDEX))
180
- expect(awaitResolved(hasSaveSlots:PromiseSaveSlotToSharedDataStore(mainSlotId, "k"))).toEqual(false)
181
- end)
155
+ local mainSlotId =
156
+ PromiseTestUtils.awaitFulfilled(hasSaveSlots:PromiseCreateSlot(SaveSlotConstants.DEFAULT_SLOT_INDEX))
157
+ PromiseTestUtils.awaitRejected(hasSaveSlots:PromiseSaveSlotToSharedDataStore(mainSlotId, "k"))
158
+
159
+ context:Destroy()
182
160
  end)
183
161
 
184
162
  it("rejects a save when the shared store write fails", function()
185
- runWithContext(function(context)
186
- local hasSaveSlots = context.hasSaveSlots
187
- local sourceSlotId = createSelectAndWrite(hasSaveSlots, 2)
163
+ local context = setup()
164
+ local hasSaveSlots = context.hasSaveSlots
165
+ local sourceSlotId = createSelectAndWrite(hasSaveSlots, 2)
188
166
 
189
- context.sharedMock:FailNextRequests(1)
190
- expect(awaitResolved(hasSaveSlots:PromiseSaveSlotToSharedDataStore(sourceSlotId, "code-fail"))).toEqual(
191
- false
192
- )
193
- end)
167
+ context.sharedMock:FailNextRequests(1)
168
+ PromiseTestUtils.awaitRejected(hasSaveSlots:PromiseSaveSlotToSharedDataStore(sourceSlotId, "code-fail"))
169
+
170
+ context:Destroy()
194
171
  end)
195
172
 
196
173
  it("rejects an import when the shared store read fails", function()
197
- runWithContext(function(context)
198
- local hasSaveSlots = context.hasSaveSlots
199
- local sourceSlotId = createSelectAndWrite(hasSaveSlots, 2)
200
- awaitValueOf(hasSaveSlots:PromiseSaveSlotToSharedDataStore(sourceSlotId, "code-read"))
201
-
202
- context.sharedMock:FailNextRequests(1)
203
- expect(awaitResolved(hasSaveSlots:PromiseImportSlotFromSharedDataStore("code-read"))).toEqual(false)
204
- end)
174
+ local context = setup()
175
+ local hasSaveSlots = context.hasSaveSlots
176
+ local sourceSlotId = createSelectAndWrite(hasSaveSlots, 2)
177
+ PromiseTestUtils.awaitFulfilled(hasSaveSlots:PromiseSaveSlotToSharedDataStore(sourceSlotId, "code-read"))
178
+
179
+ context.sharedMock:FailNextRequests(1)
180
+ PromiseTestUtils.awaitRejected(hasSaveSlots:PromiseImportSlotFromSharedDataStore("code-read"))
181
+
182
+ context:Destroy()
205
183
  end)
206
184
  end)