@quenty/saveslot 1.7.0 → 1.9.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.
@@ -11,9 +11,11 @@ local require = require(script.Parent.loader).load(script)
11
11
 
12
12
  local DataStoreMock = require("DataStoreMock")
13
13
  local Jest = require("Jest")
14
+ local Observable = require("Observable")
14
15
  local PlayerDataStoreManager = require("PlayerDataStoreManager")
15
16
  local PlayerDataStoreService = require("PlayerDataStoreService")
16
17
  local PromiseTestUtils = require("PromiseTestUtils")
18
+ local Rx = require("Rx")
17
19
  local ServiceBag = require("ServiceBag")
18
20
 
19
21
  local Workspace = game:GetService("Workspace")
@@ -198,6 +200,127 @@ describe("HasSaveSlots against a fake player (healthy datastore)", function()
198
200
  context.destroy()
199
201
  end)
200
202
 
203
+ it("ObserveActiveSlotStoreBrio tears its brio down once the active slot is deselected", function()
204
+ local context = setup()
205
+
206
+ local createPromise = context.hasSaveSlots:PromiseCreateSlot(1)
207
+ if not PromiseTestUtils.awaitSettled(createPromise, 10) then
208
+ expect("create hung").toEqual("create settled")
209
+ context.destroy()
210
+ return
211
+ end
212
+ local _, slotId = createPromise:Yield()
213
+
214
+ local selectPromise = context.hasSaveSlots:PromiseSelectSlot(slotId)
215
+ if not PromiseTestUtils.awaitSettled(selectPromise, 10) then
216
+ expect("select hung").toEqual("select settled")
217
+ context.destroy()
218
+ return
219
+ end
220
+ selectPromise:Yield()
221
+
222
+ local activeBrio: any = nil
223
+ local subscription = context.hasSaveSlots:ObserveActiveSlotStoreBrio():Subscribe(function(brio)
224
+ if not brio:IsDead() then
225
+ activeBrio = brio
226
+ end
227
+ end)
228
+
229
+ local emitted = PromiseTestUtils.awaitValue(function()
230
+ return activeBrio ~= nil
231
+ end, 10)
232
+ expect(emitted).toEqual(true)
233
+ expect(activeBrio:IsDead()).toEqual(false)
234
+
235
+ local deselectPromise = context.hasSaveSlots:PromiseDeselectSlot()
236
+ if not PromiseTestUtils.awaitSettled(deselectPromise, 10) then
237
+ expect("deselect hung").toEqual("deselect settled")
238
+ subscription:Destroy()
239
+ context.destroy()
240
+ return
241
+ end
242
+ deselectPromise:Yield()
243
+
244
+ -- The store observable kills its brio when the slot is cleared. This is the reactive teardown the
245
+ -- game relies on: on deselect the server unbinds per-slot data and removes the character purely
246
+ -- because this brio dies -- no bespoke deselect handling.
247
+ expect(activeBrio:IsDead()).toEqual(true)
248
+
249
+ subscription:Destroy()
250
+ context.destroy()
251
+ end)
252
+
253
+ it("ObserveActiveSlotStoreBrio swaps its brio when switching to another slot", function()
254
+ local context = setup()
255
+
256
+ local firstCreate = context.hasSaveSlots:PromiseCreateSlot(1)
257
+ if not PromiseTestUtils.awaitSettled(firstCreate, 10) then
258
+ expect("first create hung").toEqual("first create settled")
259
+ context.destroy()
260
+ return
261
+ end
262
+ local _, firstSlotId = firstCreate:Yield()
263
+
264
+ local secondCreate = context.hasSaveSlots:PromiseCreateSlot(2)
265
+ if not PromiseTestUtils.awaitSettled(secondCreate, 10) then
266
+ expect("second create hung").toEqual("second create settled")
267
+ context.destroy()
268
+ return
269
+ end
270
+ local _, secondSlotId = secondCreate:Yield()
271
+
272
+ local selectFirst = context.hasSaveSlots:PromiseSelectSlot(firstSlotId)
273
+ if not PromiseTestUtils.awaitSettled(selectFirst, 10) then
274
+ expect("select hung").toEqual("select settled")
275
+ context.destroy()
276
+ return
277
+ end
278
+ selectFirst:Yield()
279
+
280
+ local currentBrio: any = nil
281
+ local subscription = context.hasSaveSlots:ObserveActiveSlotStoreBrio():Subscribe(function(brio)
282
+ if not brio:IsDead() then
283
+ currentBrio = brio
284
+ end
285
+ end)
286
+
287
+ if not PromiseTestUtils.awaitValue(function()
288
+ return currentBrio ~= nil
289
+ end, 10) then
290
+ expect("first brio hung").toEqual("first brio emitted")
291
+ subscription:Destroy()
292
+ context.destroy()
293
+ return
294
+ end
295
+ local firstBrio = currentBrio
296
+
297
+ local selectSecond = context.hasSaveSlots:PromiseSelectSlot(secondSlotId)
298
+ if not PromiseTestUtils.awaitSettled(selectSecond, 10) then
299
+ expect("switch hung").toEqual("switch settled")
300
+ subscription:Destroy()
301
+ context.destroy()
302
+ return
303
+ end
304
+ selectSecond:Yield()
305
+
306
+ if not PromiseTestUtils.awaitValue(function()
307
+ return currentBrio ~= firstBrio
308
+ end, 10) then
309
+ expect("second brio hung").toEqual("second brio emitted")
310
+ subscription:Destroy()
311
+ context.destroy()
312
+ return
313
+ end
314
+
315
+ -- Switching slots kills the old slot's store brio and emits a fresh one, so per-slot server state
316
+ -- (data bindings, checkpoints, the character) rebuilds against the newly-selected slot.
317
+ expect(firstBrio:IsDead()).toEqual(true)
318
+ expect(currentBrio:IsDead()).toEqual(false)
319
+
320
+ subscription:Destroy()
321
+ context.destroy()
322
+ end)
323
+
201
324
  it("should reject creating a slot beyond the max slot count", function()
202
325
  local context = setup()
203
326
  context.hasSaveSlots.MaxSlotCount.Value = 1
@@ -662,6 +785,387 @@ describe("HasSaveSlots against a fake player (healthy datastore)", function()
662
785
  context.destroy()
663
786
  end)
664
787
 
788
+ it("PromiseDuplicateSlot copies saved data into a fresh slot and marks the name", function()
789
+ local context = setup()
790
+
791
+ local createPromise = context.hasSaveSlots:PromiseCreateSlot(1, { SlotName = "Adventure" })
792
+ if not PromiseTestUtils.awaitSettled(createPromise, 10) then
793
+ expect("create hung").toEqual("create settled")
794
+ context.destroy()
795
+ return
796
+ end
797
+ local _, slotId = createPromise:Yield()
798
+
799
+ -- Seed the source slot with some saved data to copy across
800
+ local tracker: any = context.hasSaveSlots
801
+ tracker:_getSlotStore(slotId):Store("Coins", 500)
802
+
803
+ local duplicatePromise = context.hasSaveSlots:PromiseDuplicateSlot(slotId)
804
+ if not PromiseTestUtils.awaitSettled(duplicatePromise, 10) then
805
+ expect("duplicate hung").toEqual("duplicate settled")
806
+ context.destroy()
807
+ return
808
+ end
809
+ local newSlotId = duplicatePromise:Wait()
810
+ expect(type(newSlotId)).toEqual("string")
811
+ expect(newSlotId ~= slotId).toEqual(true)
812
+
813
+ -- The copy lands at the next free index with a marked name
814
+ local metadataPromise = context.hasSaveSlots:PromiseGetSlotMetadata(newSlotId)
815
+ if not PromiseTestUtils.awaitSettled(metadataPromise, 10) then
816
+ expect("metadata hung").toEqual("metadata settled")
817
+ context.destroy()
818
+ return
819
+ end
820
+ local metadata = metadataPromise:Wait()
821
+ expect(metadata.SlotIndex).toEqual(2)
822
+ expect(metadata.SlotName).toEqual("Adventure (Copy)")
823
+
824
+ -- The saved data carried across into the new slot's own store
825
+ local dataPromise = tracker:_getSlotStore(newSlotId):Load("Coins")
826
+ if not PromiseTestUtils.awaitSettled(dataPromise, 10) then
827
+ expect("data hung").toEqual("data settled")
828
+ context.destroy()
829
+ return
830
+ end
831
+ expect((dataPromise:Wait())).toEqual(500)
832
+
833
+ context.destroy()
834
+ end)
835
+
836
+ it("PromiseDuplicateSlot into the default slot preserves the system store", function()
837
+ local context = setup()
838
+
839
+ -- Only a non-default slot exists, so the duplicate lands at the free default index (1), whose
840
+ -- store is the shared root alongside the SaveSlots system data.
841
+ local createPromise = context.hasSaveSlots:PromiseCreateSlot(2, { SlotName = "Save" })
842
+ if not PromiseTestUtils.awaitSettled(createPromise, 10) then
843
+ expect("create hung").toEqual("create settled")
844
+ context.destroy()
845
+ return
846
+ end
847
+ local _, slotId = createPromise:Yield()
848
+
849
+ local tracker: any = context.hasSaveSlots
850
+ tracker:_getSlotStore(slotId):Store("Coins", 750)
851
+
852
+ local duplicatePromise = context.hasSaveSlots:PromiseDuplicateSlot(slotId)
853
+ if not PromiseTestUtils.awaitSettled(duplicatePromise, 10) then
854
+ expect("duplicate hung").toEqual("duplicate settled")
855
+ context.destroy()
856
+ return
857
+ end
858
+ local newSlotId = duplicatePromise:Wait()
859
+
860
+ local metadataPromise = context.hasSaveSlots:PromiseGetSlotMetadata(newSlotId)
861
+ if not PromiseTestUtils.awaitSettled(metadataPromise, 10) then
862
+ expect("metadata hung").toEqual("metadata settled")
863
+ context.destroy()
864
+ return
865
+ end
866
+ expect((metadataPromise:Wait()).SlotIndex).toEqual(1)
867
+
868
+ -- The original slot still resolves -- merging into root did not clobber the system store
869
+ local sourceStillThere = context.hasSaveSlots:PromiseSlotIdFromIndex(2)
870
+ if not PromiseTestUtils.awaitSettled(sourceStillThere, 10) then
871
+ expect("lookup hung").toEqual("lookup settled")
872
+ context.destroy()
873
+ return
874
+ end
875
+ expect((sourceStillThere:Wait())).toEqual(slotId)
876
+
877
+ -- The copied game data landed in the default (root) store
878
+ local dataPromise = tracker:_getSlotStore(newSlotId):Load("Coins")
879
+ if not PromiseTestUtils.awaitSettled(dataPromise, 10) then
880
+ expect("data hung").toEqual("data settled")
881
+ context.destroy()
882
+ return
883
+ end
884
+ expect((dataPromise:Wait())).toEqual(750)
885
+
886
+ context.destroy()
887
+ end)
888
+
889
+ it("PromiseDuplicateSlot rejects when the source slot is missing", function()
890
+ local context = setup()
891
+
892
+ local duplicatePromise = context.hasSaveSlots:PromiseDuplicateSlot("does-not-exist")
893
+ if not PromiseTestUtils.awaitSettled(duplicatePromise, 10) then
894
+ expect("duplicate hung").toEqual("duplicate settled")
895
+ context.destroy()
896
+ return
897
+ end
898
+ expect((duplicatePromise:Yield())).toEqual(false)
899
+
900
+ context.destroy()
901
+ end)
902
+
903
+ it("PromiseResetActiveSlot wipes saved data but keeps the slot's index and name", function()
904
+ local context = setup()
905
+
906
+ local createPromise = context.hasSaveSlots:PromiseCreateSlot(2, { SlotName = "Adventure" })
907
+ if not PromiseTestUtils.awaitSettled(createPromise, 10) then
908
+ expect("create hung").toEqual("create settled")
909
+ context.destroy()
910
+ return
911
+ end
912
+ local _, slotId = createPromise:Yield()
913
+
914
+ local selectPromise = context.hasSaveSlots:PromiseSelectSlot(slotId)
915
+ if not PromiseTestUtils.awaitSettled(selectPromise, 10) then
916
+ expect("select hung").toEqual("select settled")
917
+ context.destroy()
918
+ return
919
+ end
920
+ selectPromise:Yield()
921
+
922
+ -- Seed the active slot with saved progress the reset must clear
923
+ local tracker: any = context.hasSaveSlots
924
+ tracker:_getSlotStore(slotId):Store("Coins", 500)
925
+
926
+ local resetPromise = context.hasSaveSlots:PromiseResetActiveSlot()
927
+ if not PromiseTestUtils.awaitSettled(resetPromise, 10) then
928
+ expect("reset hung").toEqual("reset settled")
929
+ context.destroy()
930
+ return
931
+ end
932
+ local newSlotId = resetPromise:Wait()
933
+ expect(type(newSlotId)).toEqual("string")
934
+ expect(newSlotId ~= slotId).toEqual(true)
935
+
936
+ -- The fresh slot is selected and the old one is gone
937
+ expect(context.hasSaveSlots.ActiveSlotId.Value).toEqual(newSlotId)
938
+ local hasOldPromise = context.hasSaveSlots:PromiseHasSlot(slotId)
939
+ if not PromiseTestUtils.awaitSettled(hasOldPromise, 10) then
940
+ expect("hasSlot hung").toEqual("hasSlot settled")
941
+ context.destroy()
942
+ return
943
+ end
944
+ expect((hasOldPromise:Wait())).toEqual(false)
945
+
946
+ -- Same index and name carried across into the fresh slot
947
+ local metadataPromise = context.hasSaveSlots:PromiseGetSlotMetadata(newSlotId)
948
+ if not PromiseTestUtils.awaitSettled(metadataPromise, 10) then
949
+ expect("metadata hung").toEqual("metadata settled")
950
+ context.destroy()
951
+ return
952
+ end
953
+ local metadata = metadataPromise:Wait()
954
+ expect(metadata.SlotIndex).toEqual(2)
955
+ expect(metadata.SlotName).toEqual("Adventure")
956
+
957
+ -- The saved progress did not survive into the fresh slot's store
958
+ local dataPromise = tracker:_getSlotStore(newSlotId):Load("Coins")
959
+ if not PromiseTestUtils.awaitSettled(dataPromise, 10) then
960
+ expect("data hung").toEqual("data settled")
961
+ context.destroy()
962
+ return
963
+ end
964
+ expect((dataPromise:Wait())).toBeNil()
965
+
966
+ context.destroy()
967
+ end)
968
+
969
+ it("PromiseResetActiveSlot resets the default slot in place, preserving the system store", function()
970
+ local context = setup()
971
+
972
+ -- Index 1 is the default slot, whose store is the shared root alongside the SaveSlots
973
+ -- system data; the reset must wipe the game data without clobbering that system store.
974
+ local createPromise = context.hasSaveSlots:PromiseCreateSlot(1, { SlotName = "Save" })
975
+ if not PromiseTestUtils.awaitSettled(createPromise, 10) then
976
+ expect("create hung").toEqual("create settled")
977
+ context.destroy()
978
+ return
979
+ end
980
+ local _, slotId = createPromise:Yield()
981
+
982
+ local selectPromise = context.hasSaveSlots:PromiseSelectSlot(slotId)
983
+ if not PromiseTestUtils.awaitSettled(selectPromise, 10) then
984
+ expect("select hung").toEqual("select settled")
985
+ context.destroy()
986
+ return
987
+ end
988
+ selectPromise:Yield()
989
+
990
+ local tracker: any = context.hasSaveSlots
991
+ tracker:_getSlotStore(slotId):Store("Coins", 750)
992
+
993
+ local resetPromise = context.hasSaveSlots:PromiseResetActiveSlot()
994
+ if not PromiseTestUtils.awaitSettled(resetPromise, 10) then
995
+ expect("reset hung").toEqual("reset settled")
996
+ context.destroy()
997
+ return
998
+ end
999
+ local newSlotId = resetPromise:Wait()
1000
+
1001
+ -- The system store survived: the fresh default slot still resolves at index 1
1002
+ local lookupPromise = context.hasSaveSlots:PromiseSlotIdFromIndex(1)
1003
+ if not PromiseTestUtils.awaitSettled(lookupPromise, 10) then
1004
+ expect("lookup hung").toEqual("lookup settled")
1005
+ context.destroy()
1006
+ return
1007
+ end
1008
+ expect((lookupPromise:Wait())).toEqual(newSlotId)
1009
+
1010
+ -- The game data in the shared root store was wiped
1011
+ local dataPromise = tracker:_getSlotStore(newSlotId):Load("Coins")
1012
+ if not PromiseTestUtils.awaitSettled(dataPromise, 10) then
1013
+ expect("data hung").toEqual("data settled")
1014
+ context.destroy()
1015
+ return
1016
+ end
1017
+ expect((dataPromise:Wait())).toBeNil()
1018
+
1019
+ context.destroy()
1020
+ end)
1021
+
1022
+ it("PromiseResetActiveSlot is a no-op resolving nil when no slot is active", function()
1023
+ local context = setup()
1024
+
1025
+ local resetPromise = context.hasSaveSlots:PromiseResetActiveSlot()
1026
+ if not PromiseTestUtils.awaitSettled(resetPromise, 10) then
1027
+ expect("reset hung").toEqual("reset settled")
1028
+ context.destroy()
1029
+ return
1030
+ end
1031
+ expect((resetPromise:Wait())).toBeNil()
1032
+ expect(context.hasSaveSlots.ActiveSlotId.Value).toBeNil()
1033
+
1034
+ context.destroy()
1035
+ end)
1036
+
1037
+ it("PromiseResetSlot wipes a non-active slot without touching the active selection", function()
1038
+ local context = setup()
1039
+
1040
+ -- Slot 1 is active; slot 2 is the one we reset and must stay unselected throughout.
1041
+ local activePromise = context.hasSaveSlots:PromiseCreateSlot(1)
1042
+ if not PromiseTestUtils.awaitSettled(activePromise, 10) then
1043
+ expect("active create hung").toEqual("active create settled")
1044
+ context.destroy()
1045
+ return
1046
+ end
1047
+ local _, activeSlotId = activePromise:Yield()
1048
+
1049
+ local selectPromise = context.hasSaveSlots:PromiseSelectSlot(activeSlotId)
1050
+ if not PromiseTestUtils.awaitSettled(selectPromise, 10) then
1051
+ expect("select hung").toEqual("select settled")
1052
+ context.destroy()
1053
+ return
1054
+ end
1055
+ selectPromise:Yield()
1056
+
1057
+ local targetPromise = context.hasSaveSlots:PromiseCreateSlot(2, { SlotName = "Adventure" })
1058
+ if not PromiseTestUtils.awaitSettled(targetPromise, 10) then
1059
+ expect("target create hung").toEqual("target create settled")
1060
+ context.destroy()
1061
+ return
1062
+ end
1063
+ local _, targetSlotId = targetPromise:Yield()
1064
+
1065
+ local tracker: any = context.hasSaveSlots
1066
+ tracker:_getSlotStore(targetSlotId):Store("Coins", 500)
1067
+
1068
+ local resetPromise = context.hasSaveSlots:PromiseResetSlot(targetSlotId)
1069
+ if not PromiseTestUtils.awaitSettled(resetPromise, 10) then
1070
+ expect("reset hung").toEqual("reset settled")
1071
+ context.destroy()
1072
+ return
1073
+ end
1074
+ local newSlotId = resetPromise:Wait()
1075
+ expect(type(newSlotId)).toEqual("string")
1076
+ expect(newSlotId ~= targetSlotId).toEqual(true)
1077
+
1078
+ -- The active slot is untouched and the fresh slot is not selected
1079
+ expect(context.hasSaveSlots.ActiveSlotId.Value).toEqual(activeSlotId)
1080
+
1081
+ -- Same index and name carried across; saved progress was wiped
1082
+ local metadataPromise = context.hasSaveSlots:PromiseGetSlotMetadata(newSlotId)
1083
+ if not PromiseTestUtils.awaitSettled(metadataPromise, 10) then
1084
+ expect("metadata hung").toEqual("metadata settled")
1085
+ context.destroy()
1086
+ return
1087
+ end
1088
+ local metadata = metadataPromise:Wait()
1089
+ expect(metadata.SlotIndex).toEqual(2)
1090
+ expect(metadata.SlotName).toEqual("Adventure")
1091
+
1092
+ local dataPromise = tracker:_getSlotStore(newSlotId):Load("Coins")
1093
+ if not PromiseTestUtils.awaitSettled(dataPromise, 10) then
1094
+ expect("data hung").toEqual("data settled")
1095
+ context.destroy()
1096
+ return
1097
+ end
1098
+ expect((dataPromise:Wait())).toBeNil()
1099
+
1100
+ context.destroy()
1101
+ end)
1102
+
1103
+ it("PromiseResetSlot keeps the continue pointer on the reset slot when it was last-active", function()
1104
+ local context = setup()
1105
+
1106
+ -- Select then deselect so the slot is the resumable last-active but no longer selected.
1107
+ local createPromise = context.hasSaveSlots:PromiseCreateSlot(1)
1108
+ if not PromiseTestUtils.awaitSettled(createPromise, 10) then
1109
+ expect("create hung").toEqual("create settled")
1110
+ context.destroy()
1111
+ return
1112
+ end
1113
+ local _, slotId = createPromise:Yield()
1114
+
1115
+ local selectPromise = context.hasSaveSlots:PromiseSelectSlot(slotId)
1116
+ if not PromiseTestUtils.awaitSettled(selectPromise, 10) then
1117
+ expect("select hung").toEqual("select settled")
1118
+ context.destroy()
1119
+ return
1120
+ end
1121
+ selectPromise:Yield()
1122
+
1123
+ local deselectPromise = context.hasSaveSlots:PromiseDeselectSlot()
1124
+ if not PromiseTestUtils.awaitSettled(deselectPromise, 10) then
1125
+ expect("deselect hung").toEqual("deselect settled")
1126
+ context.destroy()
1127
+ return
1128
+ end
1129
+ deselectPromise:Yield()
1130
+ expect(context.hasSaveSlots.LastActiveSlotId.Value).toEqual(slotId)
1131
+
1132
+ local resetPromise = context.hasSaveSlots:PromiseResetSlot(slotId)
1133
+ if not PromiseTestUtils.awaitSettled(resetPromise, 10) then
1134
+ expect("reset hung").toEqual("reset settled")
1135
+ context.destroy()
1136
+ return
1137
+ end
1138
+ local newSlotId = resetPromise:Wait()
1139
+
1140
+ -- The resume pointer follows the reset slot to its fresh id, and "Continue" still resolves it
1141
+ expect(context.hasSaveSlots.ActiveSlotId.Value).toBeNil()
1142
+ expect(context.hasSaveSlots.LastActiveSlotId.Value).toEqual(newSlotId)
1143
+
1144
+ local lastPromise = context.hasSaveSlots:PromiseLastActiveSlotId()
1145
+ if not PromiseTestUtils.awaitSettled(lastPromise, 10) then
1146
+ expect("lastActive hung").toEqual("lastActive settled")
1147
+ context.destroy()
1148
+ return
1149
+ end
1150
+ expect((lastPromise:Wait())).toEqual(newSlotId)
1151
+
1152
+ context.destroy()
1153
+ end)
1154
+
1155
+ it("PromiseResetSlot rejects when the slot is missing", function()
1156
+ local context = setup()
1157
+
1158
+ local resetPromise = context.hasSaveSlots:PromiseResetSlot("does-not-exist")
1159
+ if not PromiseTestUtils.awaitSettled(resetPromise, 10) then
1160
+ expect("reset hung").toEqual("reset settled")
1161
+ context.destroy()
1162
+ return
1163
+ end
1164
+ expect((resetPromise:Yield())).toEqual(false)
1165
+
1166
+ context.destroy()
1167
+ end)
1168
+
665
1169
  it("allows creating a slot past a finite cap when the count is unbounded", function()
666
1170
  local context = setup()
667
1171
  context.hasSaveSlots.MaxSlotCount.Value = math.huge
@@ -699,6 +1203,286 @@ describe("HasSaveSlots against a fake player (healthy datastore)", function()
699
1203
  end)
700
1204
  end)
701
1205
 
1206
+ describe("HasSaveSlots playtime tracking", function()
1207
+ local function createAndSelect(context: any, slotIndex: number): any
1208
+ local createPromise = context.hasSaveSlots:PromiseCreateSlot(slotIndex)
1209
+ if not PromiseTestUtils.awaitSettled(createPromise, 10) then
1210
+ expect("create hung").toEqual("create settled")
1211
+ return nil
1212
+ end
1213
+ local _, slotId = createPromise:Yield()
1214
+
1215
+ local selectPromise = context.hasSaveSlots:PromiseSelectSlot(slotId)
1216
+ if not PromiseTestUtils.awaitSettled(selectPromise, 10) then
1217
+ expect("select hung").toEqual("select settled")
1218
+ return nil
1219
+ end
1220
+ selectPromise:Yield()
1221
+
1222
+ return slotId
1223
+ end
1224
+
1225
+ local function getMetadata(context: any, slotId): any
1226
+ local promise = context.hasSaveSlots:PromiseGetSlotMetadata(slotId)
1227
+ if not PromiseTestUtils.awaitSettled(promise, 10) then
1228
+ expect("metadata hung").toEqual("metadata settled")
1229
+ return nil
1230
+ end
1231
+ return promise:Wait()
1232
+ end
1233
+
1234
+ it("increments PlayCount to 1 the first time a slot is selected", function()
1235
+ local context = setup()
1236
+
1237
+ local slotId = createAndSelect(context, 1)
1238
+ expect(getMetadata(context, slotId).PlayCount).toEqual(1)
1239
+
1240
+ context.destroy()
1241
+ end)
1242
+
1243
+ it("counts a fresh session each time a slot is re-selected", function()
1244
+ local context = setup()
1245
+
1246
+ local firstSlotId = createAndSelect(context, 1)
1247
+ local secondSlotId = createAndSelect(context, 2)
1248
+
1249
+ -- Returning to the first slot is a second session for it, a first for the second slot
1250
+ local reselectPromise = context.hasSaveSlots:PromiseSelectSlot(firstSlotId)
1251
+ if not PromiseTestUtils.awaitSettled(reselectPromise, 10) then
1252
+ expect("reselect hung").toEqual("reselect settled")
1253
+ context.destroy()
1254
+ return
1255
+ end
1256
+ reselectPromise:Yield()
1257
+
1258
+ expect(getMetadata(context, firstSlotId).PlayCount).toEqual(2)
1259
+ expect(getMetadata(context, secondSlotId).PlayCount).toEqual(1)
1260
+
1261
+ context.destroy()
1262
+ end)
1263
+
1264
+ it("accrues elapsed wall time into TimePlayed and LastSessionLength for the active slot", function()
1265
+ local context = setup()
1266
+
1267
+ local slotId = createAndSelect(context, 1)
1268
+
1269
+ -- Rewind the live session's clock so a flush observes ~120s elapsed without waiting on it. The
1270
+ -- flush adds now - lastFlush, so the total is >= 120 (real time may nudge it a second higher).
1271
+ local tracker: any = context.hasSaveSlots
1272
+ tracker._playSessionStart = os.time() - 120
1273
+ tracker._playSessionLastFlush = os.time() - 120
1274
+ tracker:_flushPlaytime()
1275
+
1276
+ local metadata = getMetadata(context, slotId)
1277
+ expect(metadata.TimePlayed ~= nil and metadata.TimePlayed >= 120).toEqual(true)
1278
+ expect(metadata.LastSessionLength ~= nil and metadata.LastSessionLength >= 120).toEqual(true)
1279
+
1280
+ context.destroy()
1281
+ end)
1282
+
1283
+ it("does not accrue time before any slot is selected", function()
1284
+ local context = setup()
1285
+
1286
+ -- No active slot -> the session is closed, so a flush lands nowhere
1287
+ local tracker: any = context.hasSaveSlots
1288
+ tracker:_flushPlaytime()
1289
+
1290
+ local createPromise = context.hasSaveSlots:PromiseCreateSlot(1)
1291
+ if not PromiseTestUtils.awaitSettled(createPromise, 10) then
1292
+ expect("create hung").toEqual("create settled")
1293
+ context.destroy()
1294
+ return
1295
+ end
1296
+ local _, slotId = createPromise:Yield()
1297
+
1298
+ expect(getMetadata(context, slotId).TimePlayed).toBeNil()
1299
+
1300
+ context.destroy()
1301
+ end)
1302
+
1303
+ it("stops accruing into a slot once it is deselected", function()
1304
+ local context = setup()
1305
+
1306
+ local slotId = createAndSelect(context, 1)
1307
+
1308
+ local tracker: any = context.hasSaveSlots
1309
+ tracker._playSessionStart = os.time() - 120
1310
+ tracker._playSessionLastFlush = os.time() - 120
1311
+ tracker:_flushPlaytime()
1312
+
1313
+ local deselectPromise = context.hasSaveSlots:PromiseDeselectSlot()
1314
+ if not PromiseTestUtils.awaitSettled(deselectPromise, 10) then
1315
+ expect("deselect hung").toEqual("deselect settled")
1316
+ context.destroy()
1317
+ return
1318
+ end
1319
+ deselectPromise:Yield()
1320
+
1321
+ local afterDeselect = getMetadata(context, slotId).TimePlayed
1322
+
1323
+ -- A flush after deselection lands nowhere: the session is closed, so the total must not move
1324
+ tracker._playSessionStart = os.time() - 120
1325
+ tracker._playSessionLastFlush = os.time() - 120
1326
+ tracker:_flushPlaytime()
1327
+ expect(getMetadata(context, slotId).TimePlayed).toEqual(afterDeselect)
1328
+
1329
+ context.destroy()
1330
+ end)
1331
+ end)
1332
+
1333
+ describe("HasSaveSlots summary providers", function()
1334
+ -- Selecting a slot drives the reactive summary aggregation; poll the slot metadata until the
1335
+ -- Summary settles into the expected shape.
1336
+ local function readSummary(context: any, slotId): any
1337
+ local promise = context.hasSaveSlots:PromiseGetSlotMetadata(slotId)
1338
+ if not PromiseTestUtils.awaitSettled(promise, 10) then
1339
+ return nil
1340
+ end
1341
+ local metadata = promise:Wait()
1342
+ return metadata and metadata.Summary
1343
+ end
1344
+
1345
+ local function awaitSummary(context: any, slotId, predicate: (any) -> boolean): (boolean, any)
1346
+ local last
1347
+ local ok = PromiseTestUtils.awaitValue(function()
1348
+ last = readSummary(context, slotId)
1349
+ return last ~= nil and predicate(last)
1350
+ end, 10)
1351
+ return ok, last
1352
+ end
1353
+
1354
+ local function createAndSelect(context: any): any
1355
+ local createPromise = context.hasSaveSlots:PromiseCreateSlot(1)
1356
+ if not PromiseTestUtils.awaitSettled(createPromise, 10) then
1357
+ return nil
1358
+ end
1359
+ local _, slotId = createPromise:Yield()
1360
+
1361
+ local selectPromise = context.hasSaveSlots:PromiseSelectSlot(slotId)
1362
+ if not PromiseTestUtils.awaitSettled(selectPromise, 10) then
1363
+ return nil
1364
+ end
1365
+ selectPromise:Yield()
1366
+
1367
+ return slotId
1368
+ end
1369
+
1370
+ it("aggregates every registered provider into the Summary, keyed by name", function()
1371
+ local context = setup()
1372
+
1373
+ context.hasSaveSlots:RegisterSummaryProvider("coins", function()
1374
+ return Rx.of(100)
1375
+ end)
1376
+ context.hasSaveSlots:RegisterSummaryProvider("world", function()
1377
+ return Rx.of(3)
1378
+ end)
1379
+
1380
+ local slotId = createAndSelect(context)
1381
+ local matched, summary = awaitSummary(context, slotId, function(s)
1382
+ return s.coins == 100 and s.world == 3
1383
+ end)
1384
+
1385
+ expect(matched).toEqual(true)
1386
+ expect(summary.coins).toEqual(100)
1387
+ expect(summary.world).toEqual(3)
1388
+
1389
+ context.destroy()
1390
+ end)
1391
+
1392
+ it("drops a provider's key from the Summary once it is unregistered", function()
1393
+ local context = setup()
1394
+
1395
+ context.hasSaveSlots:RegisterSummaryProvider("coins", function()
1396
+ return Rx.of(100)
1397
+ end)
1398
+ local unregisterWorld = context.hasSaveSlots:RegisterSummaryProvider("world", function()
1399
+ return Rx.of(3)
1400
+ end)
1401
+
1402
+ local slotId = createAndSelect(context)
1403
+ expect((awaitSummary(context, slotId, function(s)
1404
+ return s.coins == 100 and s.world == 3
1405
+ end))).toEqual(true)
1406
+
1407
+ unregisterWorld()
1408
+
1409
+ local matched, summary = awaitSummary(context, slotId, function(s)
1410
+ return s.coins == 100 and s.world == nil
1411
+ end)
1412
+ expect(matched).toEqual(true)
1413
+ expect(summary.world).toBeNil()
1414
+
1415
+ context.destroy()
1416
+ end)
1417
+
1418
+ it("isolates a provider that errors when called so the others still contribute", function()
1419
+ local context = setup()
1420
+
1421
+ context.hasSaveSlots:RegisterSummaryProvider("good", function()
1422
+ return Rx.of(1)
1423
+ end)
1424
+ context.hasSaveSlots:RegisterSummaryProvider("bad", function()
1425
+ error("provider boom")
1426
+ end)
1427
+
1428
+ local slotId = createAndSelect(context)
1429
+ local matched, summary = awaitSummary(context, slotId, function(s)
1430
+ return s.good == 1
1431
+ end)
1432
+
1433
+ expect(matched).toEqual(true)
1434
+ expect(summary.bad).toBeNil()
1435
+
1436
+ context.destroy()
1437
+ end)
1438
+
1439
+ it("isolates a provider whose stream fails so the others still contribute", function()
1440
+ local context = setup()
1441
+
1442
+ context.hasSaveSlots:RegisterSummaryProvider("good", function()
1443
+ return Rx.of(1)
1444
+ end)
1445
+ context.hasSaveSlots:RegisterSummaryProvider("bad", function()
1446
+ return Observable.new(function(sub)
1447
+ sub:Fail("stream boom")
1448
+ return nil
1449
+ end)
1450
+ end)
1451
+
1452
+ local slotId = createAndSelect(context)
1453
+ local matched, summary = awaitSummary(context, slotId, function(s)
1454
+ return s.good == 1
1455
+ end)
1456
+
1457
+ expect(matched).toEqual(true)
1458
+ expect(summary.bad).toBeNil()
1459
+
1460
+ context.destroy()
1461
+ end)
1462
+
1463
+ it("clears the Summary when every provider is unregistered", function()
1464
+ local context = setup()
1465
+
1466
+ local unregister = context.hasSaveSlots:RegisterSummaryProvider("coins", function()
1467
+ return Rx.of(100)
1468
+ end)
1469
+
1470
+ local slotId = createAndSelect(context)
1471
+ expect((awaitSummary(context, slotId, function(s)
1472
+ return s.coins == 100
1473
+ end))).toEqual(true)
1474
+
1475
+ unregister()
1476
+
1477
+ local cleared = PromiseTestUtils.awaitValue(function()
1478
+ return readSummary(context, slotId) == nil
1479
+ end, 10)
1480
+ expect(cleared).toEqual(true)
1481
+
1482
+ context.destroy()
1483
+ end)
1484
+ end)
1485
+
702
1486
  describe("HasSaveSlots against a fake player (datastore down)", function()
703
1487
  it("PromiseSlotsLoaded rejects fast instead of hanging when datastores are down", function()
704
1488
  local mock = DataStoreMock.new()