dopaminejs 3.4.0 → 3.5.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.
package/dist/testing.js CHANGED
@@ -1,5 +1,5 @@
1
- import { n as e } from "./host-DOvdGHZL.js";
2
- import { n as t, s as n } from "./DataService-CydwGT_D.js";
1
+ import { n as e } from "./host-_essSMef.js";
2
+ import { n as t, s as n } from "./DataService-QQBNqJtN.js";
3
3
  import { describe as r, it as i } from "vitest";
4
4
  import { existsSync as a, readFileSync as o, readdirSync as s } from "node:fs";
5
5
  import { dirname as c, join as l, resolve as u } from "node:path";
@@ -34,7 +34,8 @@ var f = {
34
34
  },
35
35
  player_reset: {},
36
36
  save_failed: { error: /* @__PURE__ */ Error("save failed") },
37
- load_failed: { reason: "corrupt" }
37
+ load_failed: { reason: "corrupt" },
38
+ load_recovered: { key: "player" }
38
39
  }, p = [
39
40
  "confetti",
40
41
  "sparkle",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dopaminejs",
3
- "version": "3.4.0",
3
+ "version": "3.5.0",
4
4
  "description": "Progression mechanics for web apps: XP, levels, achievements and timezone-correct daily streaks, with the particles and sound to make them land. Zero dependencies.",
5
5
  "type": "module",
6
6
  "main": "./dist/dopamine.cjs",
package/spec/README.md CHANGED
@@ -29,7 +29,7 @@ caught up goes red.
29
29
  | `clock` | Required. Start time, local `YYYY-MM-DDTHH:MM:SS`. No offset. |
30
30
  | `config` | Optional. See below. |
31
31
  | `device` | Optional. A device id stored before `init`, so the document has known keys. Without it a random id is made. |
32
- | `storage` | Optional. `{ "failWrites": true }` makes every storage write throw, to exercise `save_failed`. Reads still work and `savedJson`/`savedRaw` are seeded first. |
32
+ | `storage` | Optional. `{ "failWrites": true }` makes every storage write throw, to exercise `save_failed`. `{ "failReads": N }` makes the first N reads of the player throw, to exercise `load_failed` with reason `read` and the retry. `{ "failDeviceReads": N }` does the same for the first N reads of the device id, and `{ "failDeviceWrites": true }` makes every write of the device id throw. The count lasts for the whole case, so a `reload` reads normally once it is used up. Writes and other reads work, and `savedJson`/`savedRaw` are seeded first. |
33
33
  | `savedJson` | Optional. Any JSON value, stored as `JSON.stringify(value)`. |
34
34
  | `savedRaw` | Optional. A string stored as is, for corrupt data. |
35
35
  | `afterInit` | Optional. Expectation checked after the first `init`. |
@@ -64,6 +64,7 @@ UTC.
64
64
  takes it as `RewardSystem` config. A save that already holds a built-in keeps
65
65
  it and its reward XP when the option is off.
66
66
  - `streakDays`: `"any"` (default) or `"record"`. With `any`, `record` and `addXP` mark today as a play day. With `record`, only `record` does. XP from an achievement reward never marks a day.
67
+ - `loadRetryMs`: a number of 0 or more. After a failed read, a blocked save retries the read at most this often, measured on the case clock. Default 30000. See "After a failed read".
67
68
  - `achievements`: extra achievements by id, `{ name, description?, icon?, xp?, when }`.
68
69
 
69
70
  `when` replaces the check function. One of:
@@ -94,12 +95,14 @@ except for `setClock`, where it is accepted and checked if present.
94
95
  | op | args | Does |
95
96
  |---|---|---|
96
97
  | `setClock` | `at` | Moves the clock. |
98
+ | `init` | none | Calls `init` again on the same reward system, like a retry after a failed read. |
99
+ | `failReads` | `count` | The next `count` reads of the player throw. No `expect`. |
97
100
  | `reload` | `device?` | Builds a new reward system on the same storage and runs `init`, like a page load. `device` stores a new device id first, as a second device sharing the save. |
98
101
  | `addXP` | `amount`, `reason?` | `addXP(amount, reason)`. |
99
102
  | `record` | `activity`, `result?` | `record(activity, result)`. A missing `result` means the default. `null` is passed as `null`. Returns `true`. |
100
103
  | `unlockAchievement` | `id` | `unlockAchievement(id)`. |
101
104
  | `markAchievementsSeen` | `ids` | `markAchievementsSeen(ids)`. |
102
- | `reset` | `overwrite?` | `reset({ overwrite })`, or `reset()` when `overwrite` is missing. Replaces the player with an empty document, keeps the device id, saves, and returns the new snapshot. Fires `player_reset` after the save. With `overwrite` false and a failed load, nothing is saved: the stored document stays as it was and later writes stay blocked. After a load that worked, `overwrite` false saves like a plain reset. |
105
+ | `reset` | `overwrite?` | `reset({ overwrite })`, or `reset()` when `overwrite` is missing. Replaces the player with an empty document, keeps the device id, saves, and returns the new snapshot. Fires `player_reset` after the save. With `overwrite` false and a failed load, nothing is saved: the stored document stays as it was and a later write may retry the read and merge the new session into whatever it returns. After a load that worked, `overwrite` false saves like a plain reset. |
103
106
  | `getXPForNextLevel` | none | Reads `{ total, needed, progress }`. |
104
107
  | `export` | `as` | `export()`. Keeps the document under the name `as`. |
105
108
  | `import` | `doc` or `from`, `silent?` | `import(doc, { silent })`. `from` names a kept document. Returns the snapshot. Achievement ids with no definition are kept but fire no event. |
@@ -123,6 +126,7 @@ Events are collected from the start of each step (and from construction for
123
126
  | `doc` | Subset match against `export()`, the saved document. |
124
127
  | `docExact` | Like `playerExact`, against `export()`. |
125
128
  | `absent` | Dotted paths under the snapshot that must not exist as own keys. |
129
+ | `docAbsent` | Dotted paths under `export()` that must not exist as own keys. |
126
130
  | `achievements` | Ids in `player.achievements`, as a set. |
127
131
  | `events` | The complete ordered list of events since the step began. Each is `{ "event": name, ...payload fields }`, matched as a subset. The list length must match. |
128
132
  | `returns` | Subset match against the operation's return value. |
@@ -131,7 +135,7 @@ Events are collected from the start of each step (and from construction for
131
135
  | `throws` | An error with this name (`TypeError`, `DopamineError`) is raised. State checks still run afterwards. |
132
136
  | `code` | With `throws`: the `code` of the `DopamineError`. |
133
137
 
134
- An empty `player`, `playerExact`, `doc`, `docExact`, `returns`, `returnsExact` or `absent`, or an empty `expect`,
138
+ An empty `player`, `playerExact`, `doc`, `docExact`, `returns`, `returnsExact`, `absent` or `docAbsent`, or an empty `expect`,
135
139
  is an error because it checks nothing. An empty `events` or `achievements`
136
140
  list is a real check (nothing fired, nothing unlocked).
137
141
 
@@ -144,11 +148,11 @@ CI also runs the conformance file once in `America/Los_Angeles`
144
148
  (`DOPAMINE_TEST_TZ`), so the daylight saving case can fail.
145
149
 
146
150
  Event names: `xp_gained`, `level_up`, `achievement_unlocked`, `personal_best`
147
- (payload `{ activity, score }`), `streak_updated`, `player_reset` (empty payload), `save_failed` and
148
- `load_failed`. `load_failed` is covered for a corrupt value and for a document
151
+ (payload `{ activity, score }`), `streak_updated`, `player_reset` (empty payload), `save_failed`,
152
+ `load_failed` and `load_recovered` (payload `{ key }`). `load_failed` is covered for a corrupt value, for a document
149
153
  the core refuses (`reason` is `corrupt`, `unsupported_version` or
150
- `too_many_devices`). A saved value that is valid JSON but not an object counts as corrupt, and is kept
151
- under `player_corrupt`. A read that fails in storage cannot be made by a fixture.
154
+ `too_many_devices`) and for a read that fails in storage (`reason: 'read'`, made with `storage.failReads`). A saved value that is valid JSON but not an object counts as corrupt, and is kept
155
+ under `player_corrupt`.
152
156
 
153
157
  Payloads are frozen copies, and `snapshot()` is a frozen deep copy.
154
158
 
@@ -159,7 +163,7 @@ Fixture paths use `.` as the separator, so keys in fixtures contain no dots.
159
163
 
160
164
  1. Load every `spec/fixtures/*.json`.
161
165
  2. For each case, build a clock at the local time given, seed storage,
162
- construct the system with the config and the clock, subscribe to the seven
166
+ construct the system with the config and the clock, subscribe to the nine
163
167
  events, run `init`.
164
168
  3. Check `afterInit`, then run each step and check its `expect`.
165
169
  4. Fail on any unknown op, step key, expectation key or condition. A fixture
@@ -184,6 +188,7 @@ a save join by taking the larger number per key. That is what `merge` does.
184
188
  | `achievements` | Union. The earliest `unlockedAt` decides the entry and its stored `reward`. `seen` is true if either side has it true. |
185
189
  | `name` | From the side with the later `lastPlayedAt`, ties take the larger string. |
186
190
  | `createdAt`, `lastPlayedAt` | Min of the set values, max. |
191
+ | Any other top level field | One side only: kept. Both: the larger number if both are finite numbers, else the larger canonical JSON text. No recency. |
187
192
 
188
193
  XP is derived: the sum of `xpBy`, plus the stored `reward` of every unlocked
189
194
  achievement, minus the sum of `xpSpentBy`, and not below 0. An achievement's
@@ -199,6 +204,78 @@ with reason `too_many_devices`. A document with a `v` other than 3 is refused
199
204
  with `unsupported_version`. A value that is not an object is `invalid_document`. Counters and sums stop at
200
205
  `Number.MAX_SAFE_INTEGER`. A negative or non-finite value in a map (`xpBy`, `sumsBy` and the rest) is dropped.
201
206
 
207
+ ### After a failed read
208
+
209
+ When the read of the saved player fails (`load_failed` with reason `read`), the
210
+ session starts with an empty player and saving is off, so the real player is
211
+ never replaced. Progress made in that session is kept for the real player:
212
+
213
+ - Before a blocked save, the core retries the read, at most once per
214
+ `loadRetryMs` (default 30000) of the clock. The failed read at `init` counts as
215
+ the last read. Nothing is read on a save that is not blocked.
216
+ - The retry only succeeds when the read works and the value is a player that
217
+ `validate` accepts, or nothing is saved. A value that is not an object, a
218
+ `v` other than 3, a value that is not valid JSON, a document that would hold more than 20 devices once merged,
219
+ and a read that fails again all leave saving off and the session as it is. They
220
+ fire no event.
221
+ - On success the session player is merged into the saved one, saved, `loadFailed`
222
+ goes back to false and `load_recovered` fires after the write. The merge is not
223
+ the plain `merge`, because the session started empty: its counters for this
224
+ device are new progress.
225
+
226
+ | Field | Rule after a retry |
227
+ |---|---|
228
+ | `xpBy`, `xpSpentBy`, `gamesBy`, `playsBy`, `sumsBy` | This device's entries are added (saved plus session). Other devices' entries are the max, as in `merge`. |
229
+ | `best` | Max. |
230
+ | `level` | Max, and never below the curve for the merged XP. |
231
+ | `longest` | Max. |
232
+ | `playDates` | Union. The streak is derived from it, so it counts from the saved last play date and today's visit is one day, or none when the saved player already has today. |
233
+ | `achievements` | As in `merge`. The earliest `unlockedAt` and its `reward` win, `seen` is true if either has it, and a reward counts once. |
234
+ | `name` | The saved player's. |
235
+ | Unknown top level fields | The saved player's, as read. |
236
+ | `createdAt`, `lastPlayedAt` | As in `merge`. |
237
+
238
+ Everything read goes through `validate`, and achievement ids are looked up by own
239
+ property. If the device id could not be read at `init`, the retry reads it again
240
+ and moves the session's entries under the stored id, so no second device is made.
241
+ `fixtures/recovery.json` pins these rules.
242
+
243
+ Only the write that starts a retry waits for it. A second `addXP` or `record`
244
+ that lands while the read is in flight returns at once, writes nothing and
245
+ starts no other read. A read that never settles therefore stalls only the call
246
+ that started it. The session is merged as it stands when the read returns.
247
+
248
+ An `import` while the read is failed counts as known progress, not a gain, for
249
+ this device's counters. The core remembers the imported counters as a baseline,
250
+ so after a retry each of this device's counters is
251
+ `max(saved, imported) + (session - imported)`. A backup of this device that is
252
+ ahead of the saved player is kept, and the backup is never added twice. An
253
+ import at or below what the session already holds for a counter changes nothing,
254
+ so importing the session's own `export()` does not count it twice. An `init()`
255
+ that fails after one that worked treats the player it loaded as the baseline,
256
+ because that progress is already stored. If the device id is only found at the
257
+ retry and the session already holds entries under that stored id (from an
258
+ import, or from a player the session read), the core cannot tell them from new
259
+ gains and moving them would count them twice, so the retry fails closed:
260
+ nothing is saved and the session stays blocked. A session holding no entries
261
+ under the stored id, including one that only imported nothing, zeros or other
262
+ devices, recovers. The way out of a blocked session is `reset({ overwrite: false })`,
263
+ which drops the session and keeps the stored player. `init()` is the other
264
+ way, and it stays blocked if its read fails again. `reset()` with no option
265
+ replaces the stored player with an empty one and keeps no backup.
266
+ `init()` that reads, `reset()` and a recovery forget the baseline.
267
+
268
+ The `xp_gained` event and the return value of the `addXP` that recovers describe
269
+ the merged player when the call is a direct `addXP` (`record` reports its batch total as before): `total`, `newLevel` and `leveledUp` come from it, and
270
+ `level_up` fires only when this gain raises the merged level, compared with the merged player before the gain. A player who was already at that level gets no `level_up`.
271
+
272
+ `init()` called again behaves as before: it reads, and on success replaces the
273
+ session player with the saved one.
274
+
275
+ Two tabs: each tab merges its own session once. The second tab reads what the
276
+ first saved, so nothing is lost and each session's progress is added once, but
277
+ nothing links the two sessions. This is a known limit.
278
+
202
279
  ### From 2.x
203
280
 
204
281
  A save with no `v` moves under the fixed device key `legacy`, never a real
@@ -207,7 +284,10 @@ unlocked, so the legacy entry is the old `xp` minus those rewards, not below 0.
207
284
  Two devices that read the same 2.x save get the same entries and merge without
208
285
  doubling. 2.x progress made separately on two devices before migration merges
209
286
  by max, not by sum. `playDates` is rebuilt from `streak.current` and
210
- `streak.lastPlayDate`. A level above the curve is kept.
287
+ `streak.lastPlayDate`. A level above the curve is kept. A `lastPlayDate` after
288
+ today counts as today, so a clock change keeps the streak. One that is not a
289
+ real day counts as no date: there is no current run, and the larger of
290
+ `current` and `longest` stays as `longest`.
211
291
 
212
292
  ## Quirks pinned on purpose
213
293
 
@@ -219,11 +299,30 @@ rule change and needs a fixture change.
219
299
  `streak_updated` for a player with no earlier play. Loading a save counts no
220
300
  day.
221
301
  - Only `record` marks a day as played. `addXP` does not.
222
- - Numbers must be finite numbers. A numeric string is dropped to the default.
302
+ - A number is a finite number, or a plain decimal string (`"250"`, `" 12 "`,
303
+ `"-1.5"`) in every field that expects a number. Counts must also be whole and
304
+ none may be negative. Anything else falls back to the default: a blank
305
+ string, `"Infinity"`, `"0x10"`, `"0b11"`, `"0o7"`, `"1e3"`, `"+5"`, `"5."`
306
+ and any other text.
223
307
  - Dates must be real calendar days. `9999-99-99` and `2026-02-30` are dropped.
224
- - A play date after today is kept and ignored for the current streak.
225
- - Fields the core does not know are not carried.
226
- - `__proto__`, `constructor` and `prototype` are never kept as a key.
227
- - Achievement ids the core does not define are kept.
308
+ - A play date after today is kept and ignored for the current streak. In a 2.x
309
+ save, `streak.lastPlayDate` after today counts as today.
310
+ - Fields the core does not know are kept as read, at the top level of the
311
+ document, and are not part of the player view. Intended: a newer version
312
+ wrote them, and dropping them loses data. `merge` joins them by a fixed
313
+ rule. A field on one side only is kept. On both sides, two finite numbers
314
+ keep the larger value, and anything else keeps the one with the larger
315
+ canonical JSON text (keys sorted). Recency is not used: no field has its own
316
+ timestamp, and a rule based on `lastPlayedAt` gives a different result
317
+ depending on how three saves are grouped. The rule above is the same in any
318
+ order, any grouping and when repeated.
319
+ - `__proto__`, `constructor` and `prototype` are never kept as a key, at the
320
+ top level or inside a kept value.
321
+ - Achievement ids the core does not define are kept. Intended: a mod or a
322
+ newer version may define them, so dropping one loses a real unlock.
323
+ - A 2.x streak of 0 with a real date counts that date as one play day. Intended:
324
+ no play is lost, so the same day gives 1 and the next day gives 2.
325
+ - `load_recovered` fires after the merged player is written. If that write fails,
326
+ `save_failed` fires too and the merged player stays in memory.
228
327
  - Extra numbers in a `record` result add up per device. A value that is not a
229
328
  positive finite number adds 0.
@@ -1773,6 +1773,351 @@
1773
1773
  }
1774
1774
  }
1775
1775
  ]
1776
+ },
1777
+ {
1778
+ "name": "the string 0x10 is not a number",
1779
+ "clock": "2026-08-07T09:00:00",
1780
+ "device": "me",
1781
+ "note": "Only plain decimal strings are read. Hex, binary, octal, exponent, a sign of + and a trailing dot fall back to the default.",
1782
+ "steps": [
1783
+ {
1784
+ "op": "validate",
1785
+ "doc": {
1786
+ "v": 3,
1787
+ "level": "0x10"
1788
+ },
1789
+ "as": "v",
1790
+ "expect": {
1791
+ "returnsExact": {
1792
+ "level": 1
1793
+ }
1794
+ }
1795
+ }
1796
+ ]
1797
+ },
1798
+ {
1799
+ "name": "the string 0b11 is not a number",
1800
+ "clock": "2026-08-07T09:00:00",
1801
+ "device": "me",
1802
+ "note": "Only plain decimal strings are read. Hex, binary, octal, exponent, a sign of + and a trailing dot fall back to the default.",
1803
+ "steps": [
1804
+ {
1805
+ "op": "validate",
1806
+ "doc": {
1807
+ "v": 3,
1808
+ "level": "0b11"
1809
+ },
1810
+ "as": "v",
1811
+ "expect": {
1812
+ "returnsExact": {
1813
+ "level": 1
1814
+ }
1815
+ }
1816
+ }
1817
+ ]
1818
+ },
1819
+ {
1820
+ "name": "the string 0o7 is not a number",
1821
+ "clock": "2026-08-07T09:00:00",
1822
+ "device": "me",
1823
+ "note": "Only plain decimal strings are read. Hex, binary, octal, exponent, a sign of + and a trailing dot fall back to the default.",
1824
+ "steps": [
1825
+ {
1826
+ "op": "validate",
1827
+ "doc": {
1828
+ "v": 3,
1829
+ "level": "0o7"
1830
+ },
1831
+ "as": "v",
1832
+ "expect": {
1833
+ "returnsExact": {
1834
+ "level": 1
1835
+ }
1836
+ }
1837
+ }
1838
+ ]
1839
+ },
1840
+ {
1841
+ "name": "the string 1e3 is not a number",
1842
+ "clock": "2026-08-07T09:00:00",
1843
+ "device": "me",
1844
+ "note": "Only plain decimal strings are read. Hex, binary, octal, exponent, a sign of + and a trailing dot fall back to the default.",
1845
+ "steps": [
1846
+ {
1847
+ "op": "validate",
1848
+ "doc": {
1849
+ "v": 3,
1850
+ "level": "1e3"
1851
+ },
1852
+ "as": "v",
1853
+ "expect": {
1854
+ "returnsExact": {
1855
+ "level": 1
1856
+ }
1857
+ }
1858
+ }
1859
+ ]
1860
+ },
1861
+ {
1862
+ "name": "the string +5 is not a number",
1863
+ "clock": "2026-08-07T09:00:00",
1864
+ "device": "me",
1865
+ "note": "Only plain decimal strings are read. Hex, binary, octal, exponent, a sign of + and a trailing dot fall back to the default.",
1866
+ "steps": [
1867
+ {
1868
+ "op": "validate",
1869
+ "doc": {
1870
+ "v": 3,
1871
+ "level": "+5"
1872
+ },
1873
+ "as": "v",
1874
+ "expect": {
1875
+ "returnsExact": {
1876
+ "level": 1
1877
+ }
1878
+ }
1879
+ }
1880
+ ]
1881
+ },
1882
+ {
1883
+ "name": "the string 5. is not a number",
1884
+ "clock": "2026-08-07T09:00:00",
1885
+ "device": "me",
1886
+ "note": "Only plain decimal strings are read. Hex, binary, octal, exponent, a sign of + and a trailing dot fall back to the default.",
1887
+ "steps": [
1888
+ {
1889
+ "op": "validate",
1890
+ "doc": {
1891
+ "v": 3,
1892
+ "level": "5."
1893
+ },
1894
+ "as": "v",
1895
+ "expect": {
1896
+ "returnsExact": {
1897
+ "level": 1
1898
+ }
1899
+ }
1900
+ }
1901
+ ]
1902
+ },
1903
+ {
1904
+ "name": "the string 1e21 is not a number",
1905
+ "clock": "2026-08-07T09:00:00",
1906
+ "device": "me",
1907
+ "note": "Only plain decimal strings are read. Hex, binary, octal, exponent, a sign of + and a trailing dot fall back to the default.",
1908
+ "steps": [
1909
+ {
1910
+ "op": "validate",
1911
+ "doc": {
1912
+ "v": 3,
1913
+ "level": "1e21"
1914
+ },
1915
+ "as": "v",
1916
+ "expect": {
1917
+ "returnsExact": {
1918
+ "level": 1
1919
+ }
1920
+ }
1921
+ }
1922
+ ]
1923
+ },
1924
+ {
1925
+ "name": "plain decimal strings are read",
1926
+ "clock": "2026-08-07T09:00:00",
1927
+ "device": "me",
1928
+ "note": "Whitespace around a plain decimal is fine. A negative amount is dropped as before.",
1929
+ "steps": [
1930
+ {
1931
+ "op": "validate",
1932
+ "doc": {
1933
+ "v": 3,
1934
+ "level": " 12 ",
1935
+ "xpBy": {
1936
+ "a": "1.5",
1937
+ "b": "-3"
1938
+ }
1939
+ },
1940
+ "as": "v",
1941
+ "expect": {
1942
+ "returnsExact": {
1943
+ "level": 12,
1944
+ "xpBy": {
1945
+ "a": 1.5
1946
+ }
1947
+ }
1948
+ }
1949
+ }
1950
+ ]
1951
+ },
1952
+ {
1953
+ "name": "an unknown field on one side is kept",
1954
+ "clock": "2026-08-07T09:00:00",
1955
+ "device": "me",
1956
+ "note": "No rule joins a field the core does not know, so a field on one side stays.",
1957
+ "steps": [
1958
+ {
1959
+ "op": "merge",
1960
+ "a": {
1961
+ "v": 3,
1962
+ "theme": "dark"
1963
+ },
1964
+ "b": {
1965
+ "v": 3
1966
+ },
1967
+ "as": "m",
1968
+ "expect": {
1969
+ "returns": {
1970
+ "theme": "dark"
1971
+ }
1972
+ }
1973
+ },
1974
+ {
1975
+ "op": "merge",
1976
+ "a": {
1977
+ "v": 3
1978
+ },
1979
+ "b": {
1980
+ "v": 3,
1981
+ "theme": "dark"
1982
+ },
1983
+ "as": "n",
1984
+ "expect": {
1985
+ "returns": {
1986
+ "theme": "dark"
1987
+ },
1988
+ "same": [
1989
+ [
1990
+ "m",
1991
+ "n"
1992
+ ]
1993
+ ]
1994
+ }
1995
+ }
1996
+ ]
1997
+ },
1998
+ {
1999
+ "name": "unknown numbers join by value",
2000
+ "clock": "2026-08-07T09:00:00",
2001
+ "device": "me",
2002
+ "note": "99 against 100 gives 100, not the larger text. The order of the sides does not matter.",
2003
+ "steps": [
2004
+ {
2005
+ "op": "merge",
2006
+ "a": {
2007
+ "v": 3,
2008
+ "coins": 99
2009
+ },
2010
+ "b": {
2011
+ "v": 3,
2012
+ "coins": 100
2013
+ },
2014
+ "as": "ab",
2015
+ "expect": {
2016
+ "returns": {
2017
+ "coins": 100
2018
+ }
2019
+ }
2020
+ },
2021
+ {
2022
+ "op": "merge",
2023
+ "a": {
2024
+ "v": 3,
2025
+ "coins": 100
2026
+ },
2027
+ "b": {
2028
+ "v": 3,
2029
+ "coins": 99
2030
+ },
2031
+ "as": "ba",
2032
+ "expect": {
2033
+ "returns": {
2034
+ "coins": 100
2035
+ },
2036
+ "same": [
2037
+ [
2038
+ "ab",
2039
+ "ba"
2040
+ ]
2041
+ ]
2042
+ }
2043
+ }
2044
+ ]
2045
+ },
2046
+ {
2047
+ "name": "unknown strings and objects join by larger text, newer save does not win",
2048
+ "clock": "2026-08-07T09:00:00",
2049
+ "device": "me",
2050
+ "note": "No field has its own time, so recency is not used. The larger canonical JSON text wins whichever side is newer, and the order of the sides does not matter.",
2051
+ "steps": [
2052
+ {
2053
+ "op": "merge",
2054
+ "a": {
2055
+ "v": 3,
2056
+ "lastPlayedAt": 2,
2057
+ "theme": "dark",
2058
+ "opts": {
2059
+ "b": 1,
2060
+ "a": 2
2061
+ }
2062
+ },
2063
+ "b": {
2064
+ "v": 3,
2065
+ "lastPlayedAt": 1,
2066
+ "theme": "light",
2067
+ "opts": {
2068
+ "a": 2,
2069
+ "b": 3
2070
+ }
2071
+ },
2072
+ "as": "ab",
2073
+ "expect": {
2074
+ "returns": {
2075
+ "theme": "light",
2076
+ "opts": {
2077
+ "a": 2,
2078
+ "b": 3
2079
+ }
2080
+ }
2081
+ }
2082
+ },
2083
+ {
2084
+ "op": "merge",
2085
+ "a": {
2086
+ "v": 3,
2087
+ "lastPlayedAt": 1,
2088
+ "theme": "light",
2089
+ "opts": {
2090
+ "a": 2,
2091
+ "b": 3
2092
+ }
2093
+ },
2094
+ "b": {
2095
+ "v": 3,
2096
+ "lastPlayedAt": 2,
2097
+ "theme": "dark",
2098
+ "opts": {
2099
+ "b": 1,
2100
+ "a": 2
2101
+ }
2102
+ },
2103
+ "as": "ba",
2104
+ "expect": {
2105
+ "returns": {
2106
+ "theme": "light",
2107
+ "opts": {
2108
+ "a": 2,
2109
+ "b": 3
2110
+ }
2111
+ },
2112
+ "same": [
2113
+ [
2114
+ "ab",
2115
+ "ba"
2116
+ ]
2117
+ ]
2118
+ }
2119
+ }
2120
+ ]
1776
2121
  }
1777
2122
  ]
1778
2123
  }