|
1 | 1 | import { describe, it, beforeEach } from 'vitest'; |
2 | 2 | import { Agent, Store, core, commits, JSCryptoProvider } from './index.js'; |
3 | 3 |
|
4 | | -/** Creates a fresh Store with the given agent, restoring any offline data. */ |
| 4 | +/** Creates a fresh Store with the given agent. */ |
5 | 5 | function freshStore(agent: Agent): Store { |
6 | 6 | const store = new Store({ serverUrl: 'https://example.com' }); |
7 | 7 | store.setAgent(agent); |
8 | | - store.restoreOfflineResources(); |
9 | 8 |
|
10 | 9 | return store; |
11 | 10 | } |
12 | 11 |
|
13 | | -describe('Offline persistence across reloads', () => { |
| 12 | +describe('Offline persistence', () => { |
14 | 13 | let agent: Agent; |
15 | 14 |
|
16 | 15 | beforeEach(async () => { |
17 | | - localStorage.clear(); |
18 | 16 | const keys = await Agent.generateKeyPair(); |
19 | 17 | const provider = new JSCryptoProvider(keys.privateKey); |
20 | 18 | agent = new Agent(provider, `did:ad:agent:${keys.publicKey}`); |
21 | 19 | }); |
22 | 20 |
|
23 | | - it('resource subject stays the same after reload + re-edit', async ({ |
24 | | - expect, |
25 | | - }) => { |
26 | | - // Session 1: create drive + doc, save offline |
27 | | - const store1 = freshStore(agent); |
28 | | - const drive = await store1.createDrive('Test Drive'); |
29 | | - expect(drive.get(core.properties.write)).toContain(agent.subject); |
30 | | - |
31 | | - const doc = await store1.newResource({ |
32 | | - parent: drive.subject, |
33 | | - propVals: { [core.properties.name]: 'My Doc' }, |
34 | | - }); |
35 | | - await doc.save(); |
36 | | - const subject = doc.subject; |
37 | | - expect(subject).toMatch(/^did:ad:/); |
38 | | - |
39 | | - // Session 2: reload, edit, save |
40 | | - const store2 = freshStore(agent); |
41 | | - const doc2 = store2.getResourceLoading(subject); |
42 | | - expect(doc2.get(core.properties.name)).toBe('My Doc'); |
43 | | - |
44 | | - await doc2.set(core.properties.name, 'Updated', false); |
45 | | - await doc2.save(); |
46 | | - expect(doc2.subject).toBe(subject); |
47 | | - |
48 | | - // Session 3: reload, verify latest edit persisted |
49 | | - const store3 = freshStore(agent); |
50 | | - const doc3 = store3.getResourceLoading(subject); |
51 | | - expect(doc3.get(core.properties.name)).toBe('Updated'); |
52 | | - }); |
53 | | - |
54 | | - it('multiple edits across reloads all persist', async ({ expect }) => { |
55 | | - const store1 = freshStore(agent); |
56 | | - const res = await store1.newResource({ |
57 | | - noParent: true, |
58 | | - propVals: { [core.properties.name]: 'v1' }, |
59 | | - }); |
60 | | - await res.save(); |
61 | | - const subject = res.subject; |
62 | | - |
63 | | - for (const version of ['v2', 'v3', 'v4']) { |
64 | | - const store = freshStore(agent); |
65 | | - const r = store.getResourceLoading(subject); |
66 | | - await r.set(core.properties.name, version, false); |
67 | | - await r.save(); |
68 | | - expect(r.subject).toBe(subject); |
69 | | - } |
70 | | - |
71 | | - const storeFinal = freshStore(agent); |
72 | | - const rFinal = storeFinal.getResourceLoading(subject); |
73 | | - expect(rFinal.get(core.properties.name)).toBe('v4'); |
74 | | - }); |
75 | | - |
76 | 21 | it('offline save sets createdAt for sorting', async ({ expect }) => { |
77 | 22 | const store = freshStore(agent); |
78 | 23 | const drive = await store.createDrive('Timestamp Test'); |
|
0 commit comments