|
1 | 1 | import { createEventEmitter } from 'devframe/utils/events' |
2 | 2 | import { describe, expect, it } from 'vitest' |
| 3 | +import { DEVFRAME_EVENTS } from '../events' |
3 | 4 | import { createRpcSharedStateClientHost } from './rpc-shared-state' |
4 | 5 |
|
5 | 6 | function makeFakeRpc() { |
@@ -50,3 +51,106 @@ describe('client shared state', () => { |
50 | 51 | expect(setCalls).toHaveLength(1) // exactly one server-state:set, not two |
51 | 52 | }) |
52 | 53 | }) |
| 54 | + |
| 55 | +describe('initial shared-state snapshots', () => { |
| 56 | + function setup() { |
| 57 | + const { rpc, setCalls } = makeFakeRpc() |
| 58 | + const snapshot = Promise.withResolvers<object | undefined>() |
| 59 | + const handlers = new Map<string, (...args: any[]) => void>() |
| 60 | + rpc.isTrusted = true |
| 61 | + rpc.call = () => snapshot.promise |
| 62 | + rpc.client.register = (definition: any) => handlers.set(definition.name, definition.handler) |
| 63 | + const host = createRpcSharedStateClientHost(rpc) |
| 64 | + return { rpc, host, snapshot, handlers, setCalls } |
| 65 | + } |
| 66 | + |
| 67 | + it('reads a snapshot without writing it back to the node side', async () => { |
| 68 | + const { host, snapshot, setCalls } = setup() |
| 69 | + const state = await host.get('k', { initialValue: { count: 0, localDefault: true } }) |
| 70 | + snapshot.resolve({ count: 1 }) |
| 71 | + await snapshot.promise |
| 72 | + expect(state.value()).toEqual({ count: 1, localDefault: true }) |
| 73 | + expect(setCalls).toEqual([]) |
| 74 | + state.mutate((draft) => { |
| 75 | + draft.count = 2 |
| 76 | + }) |
| 77 | + expect(setCalls[0]?.[1]).toEqual({ count: 2, localDefault: true }) |
| 78 | + }) |
| 79 | + |
| 80 | + it('keeps an update received while the initial snapshot is pending', async () => { |
| 81 | + const { host, snapshot, handlers } = setup() |
| 82 | + const state = await host.get('k', { initialValue: { count: 0 } }) |
| 83 | + handlers.get(DEVFRAME_EVENTS.broadcast.clientStateUpdated)!('k', { count: 2 }, 'newer') |
| 84 | + snapshot.resolve({ count: 1 }) |
| 85 | + await snapshot.promise |
| 86 | + expect(state.value()).toEqual({ count: 2 }) |
| 87 | + }) |
| 88 | + |
| 89 | + it('keeps a local write made while the initial snapshot is pending', async () => { |
| 90 | + const { host, snapshot, setCalls } = setup() |
| 91 | + const state = await host.get('k', { initialValue: { count: 0 } }) |
| 92 | + state.mutate((draft) => { |
| 93 | + draft.count = 2 |
| 94 | + }) |
| 95 | + snapshot.resolve({ count: 1 }) |
| 96 | + await snapshot.promise |
| 97 | + expect(state.value()).toEqual({ count: 2 }) |
| 98 | + expect(setCalls).toHaveLength(1) |
| 99 | + }) |
| 100 | + |
| 101 | + it('keeps a local undo that restores the initial object while the snapshot is pending', async () => { |
| 102 | + const { host, snapshot, setCalls } = setup() |
| 103 | + const state = await host.get('k', { initialValue: { count: 0 } }) |
| 104 | + const initialValue = state.value() |
| 105 | + state.mutate((draft) => { |
| 106 | + draft.count = 2 |
| 107 | + }) |
| 108 | + state.mutate(() => initialValue) |
| 109 | + expect(state.value()).toBe(initialValue) |
| 110 | + expect(setCalls.map(([, value]) => value)).toEqual([{ count: 2 }, { count: 0 }]) |
| 111 | + |
| 112 | + snapshot.resolve({ count: 1 }) |
| 113 | + await snapshot.promise |
| 114 | + expect(state.value()).toBe(initialValue) |
| 115 | + expect(setCalls).toHaveLength(2) |
| 116 | + }) |
| 117 | + |
| 118 | + it('applies the initial snapshot after a mutation that leaves the value unchanged', async () => { |
| 119 | + const { host, snapshot, setCalls } = setup() |
| 120 | + const state = await host.get('k', { initialValue: { count: 0 } }) |
| 121 | + state.mutate((draft) => { |
| 122 | + draft.count = 0 |
| 123 | + }) |
| 124 | + snapshot.resolve({ count: 1 }) |
| 125 | + await snapshot.promise |
| 126 | + expect(state.value()).toEqual({ count: 1 }) |
| 127 | + expect(setCalls).toEqual([]) |
| 128 | + }) |
| 129 | + |
| 130 | + it('forwards a local write triggered by the initial snapshot', async () => { |
| 131 | + const { host, snapshot, setCalls } = setup() |
| 132 | + const state = await host.get('k', { initialValue: { count: 0 } }) |
| 133 | + state.on('updated', (value) => { |
| 134 | + if (value.count === 1) { |
| 135 | + state.mutate((draft) => { |
| 136 | + draft.count = 2 |
| 137 | + }) |
| 138 | + } |
| 139 | + }) |
| 140 | + snapshot.resolve({ count: 1 }) |
| 141 | + await snapshot.promise |
| 142 | + expect(state.value()).toEqual({ count: 2 }) |
| 143 | + expect(setCalls).toHaveLength(1) |
| 144 | + expect(setCalls[0]?.[1]).toEqual({ count: 2 }) |
| 145 | + }) |
| 146 | + |
| 147 | + it('ignores a snapshot after its state is deleted', async () => { |
| 148 | + const { host, snapshot, setCalls } = setup() |
| 149 | + const state = await host.get('k', { initialValue: { count: 0 } }) |
| 150 | + host.delete('k') |
| 151 | + snapshot.resolve({ count: 1 }) |
| 152 | + await snapshot.promise |
| 153 | + expect(state.value()).toEqual({ count: 0 }) |
| 154 | + expect(setCalls).toEqual([]) |
| 155 | + }) |
| 156 | +}) |
0 commit comments