|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect } from 'vitest'; |
| 4 | +import { InMemoryRepository } from '../src/in-memory-repository.js'; |
| 5 | +import { LayeredRepository } from '../src/layered-repository.js'; |
| 6 | +import type { MetaRef, MetadataEvent } from '../src/types.js'; |
| 7 | + |
| 8 | +const ref = (name: string): MetaRef => ({ |
| 9 | + org: 'system', |
| 10 | + project: 'test', |
| 11 | + branch: 'main', |
| 12 | + type: 'view', |
| 13 | + name, |
| 14 | +}); |
| 15 | + |
| 16 | +const sleep = (ms: number) => new Promise<void>((r) => setTimeout(r, ms)); |
| 17 | + |
| 18 | +describe('LayeredRepository', () => { |
| 19 | + it('get: top layer shadows lower layers', async () => { |
| 20 | + const builtins = new InMemoryRepository(); |
| 21 | + const userspace = new InMemoryRepository(); |
| 22 | + await builtins.put(ref('a'), { from: 'builtin' }, { parentVersion: null, actor: 't' }); |
| 23 | + await userspace.put(ref('a'), { from: 'user' }, { parentVersion: null, actor: 't' }); |
| 24 | + |
| 25 | + const layered = new LayeredRepository({ |
| 26 | + layers: [ |
| 27 | + { label: 'user', repo: userspace }, |
| 28 | + { label: 'system', repo: builtins, readOnly: true }, |
| 29 | + ], |
| 30 | + }); |
| 31 | + const got = await layered.get(ref('a')); |
| 32 | + expect(got?.body).toEqual({ from: 'user' }); |
| 33 | + }); |
| 34 | + |
| 35 | + it('get: falls through to lower layers when top is empty', async () => { |
| 36 | + const builtins = new InMemoryRepository(); |
| 37 | + const userspace = new InMemoryRepository(); |
| 38 | + await builtins.put(ref('only_in_builtin'), { v: 1 }, { parentVersion: null, actor: 't' }); |
| 39 | + |
| 40 | + const layered = new LayeredRepository({ |
| 41 | + layers: [ |
| 42 | + { label: 'user', repo: userspace }, |
| 43 | + { label: 'system', repo: builtins, readOnly: true }, |
| 44 | + ], |
| 45 | + }); |
| 46 | + const got = await layered.get(ref('only_in_builtin')); |
| 47 | + expect(got?.body).toEqual({ v: 1 }); |
| 48 | + }); |
| 49 | + |
| 50 | + it('put: routes to the topmost writable layer', async () => { |
| 51 | + const builtins = new InMemoryRepository(); |
| 52 | + const userspace = new InMemoryRepository(); |
| 53 | + const layered = new LayeredRepository({ |
| 54 | + layers: [ |
| 55 | + { label: 'user', repo: userspace }, |
| 56 | + { label: 'system', repo: builtins, readOnly: true }, |
| 57 | + ], |
| 58 | + }); |
| 59 | + await layered.put(ref('written'), { v: 1 }, { parentVersion: null, actor: 't' }); |
| 60 | + expect(await userspace.get(ref('written'))).not.toBeNull(); |
| 61 | + expect(await builtins.get(ref('written'))).toBeNull(); |
| 62 | + }); |
| 63 | + |
| 64 | + it('put: throws if no writable layer exists', async () => { |
| 65 | + const ro = new InMemoryRepository(); |
| 66 | + const layered = new LayeredRepository({ |
| 67 | + layers: [{ label: 'system', repo: ro, readOnly: true }], |
| 68 | + }); |
| 69 | + await expect( |
| 70 | + layered.put(ref('x'), { v: 1 }, { parentVersion: null, actor: 't' }), |
| 71 | + ).rejects.toThrow(/no writable layer/); |
| 72 | + }); |
| 73 | + |
| 74 | + it('list: deduplicates by refKey, preferring the top layer', async () => { |
| 75 | + const builtins = new InMemoryRepository(); |
| 76 | + const userspace = new InMemoryRepository(); |
| 77 | + await builtins.put(ref('shared'), { from: 'builtin' }, { parentVersion: null, actor: 't' }); |
| 78 | + await builtins.put(ref('only_builtin'), { v: 1 }, { parentVersion: null, actor: 't' }); |
| 79 | + await userspace.put(ref('shared'), { from: 'user' }, { parentVersion: null, actor: 't' }); |
| 80 | + await userspace.put(ref('only_user'), { v: 1 }, { parentVersion: null, actor: 't' }); |
| 81 | + |
| 82 | + const layered = new LayeredRepository({ |
| 83 | + layers: [ |
| 84 | + { label: 'user', repo: userspace }, |
| 85 | + { label: 'system', repo: builtins, readOnly: true }, |
| 86 | + ], |
| 87 | + }); |
| 88 | + const names: string[] = []; |
| 89 | + for await (const header of layered.list({ type: 'view' })) { |
| 90 | + names.push(header.ref.name); |
| 91 | + } |
| 92 | + expect(new Set(names)).toEqual(new Set(['shared', 'only_user', 'only_builtin'])); |
| 93 | + // 'shared' must come from user (top) → headers should be unique. |
| 94 | + expect(names.length).toBe(3); |
| 95 | + }); |
| 96 | + |
| 97 | + it('watch: tags events with <layer>:<source>', async () => { |
| 98 | + const a = new InMemoryRepository(); |
| 99 | + const b = new InMemoryRepository(); |
| 100 | + const layered = new LayeredRepository({ |
| 101 | + layers: [ |
| 102 | + { label: 'top', repo: a }, |
| 103 | + { label: 'bottom', repo: b }, |
| 104 | + ], |
| 105 | + }); |
| 106 | + const iter = layered.watch({ org: 'system', project: 'test', branch: 'main' })[Symbol.asyncIterator](); |
| 107 | + const collected: MetadataEvent[] = []; |
| 108 | + const done = (async () => { |
| 109 | + for (let i = 0; i < 2; i++) { |
| 110 | + const r = await iter.next(); |
| 111 | + if (r.done) return; |
| 112 | + collected.push(r.value as MetadataEvent); |
| 113 | + } |
| 114 | + })(); |
| 115 | + await sleep(10); |
| 116 | + await a.put(ref('x'), { from: 'top' }, { parentVersion: null, actor: 't' }); |
| 117 | + await b.put(ref('y'), { from: 'bottom' }, { parentVersion: null, actor: 't' }); |
| 118 | + await Promise.race([done, sleep(1000)]); |
| 119 | + await iter.return?.(undefined); |
| 120 | + expect(collected).toHaveLength(2); |
| 121 | + const sources = collected.map((e) => e.source).sort(); |
| 122 | + expect(sources).toEqual(['bottom:in-memory', 'top:in-memory']); |
| 123 | + }); |
| 124 | + |
| 125 | + it('watch: closing the layered iterator closes all child iterators', async () => { |
| 126 | + const a = new InMemoryRepository(); |
| 127 | + const b = new InMemoryRepository(); |
| 128 | + const layered = new LayeredRepository({ |
| 129 | + layers: [{ label: 'a', repo: a }, { label: 'b', repo: b }], |
| 130 | + }); |
| 131 | + const iter = layered.watch({ org: 'system', project: 'test', branch: 'main' })[Symbol.asyncIterator](); |
| 132 | + // Schedule a next() that will park (no events yet). |
| 133 | + const pending = iter.next(); |
| 134 | + await sleep(10); |
| 135 | + // Closing must unblock pending; otherwise this test will time out. |
| 136 | + await iter.return?.(undefined); |
| 137 | + const result = await pending; |
| 138 | + expect(result.done).toBe(true); |
| 139 | + }); |
| 140 | + |
| 141 | + it('history: merges from all layers, sorted by ts', async () => { |
| 142 | + const a = new InMemoryRepository({ now: () => new Date('2025-01-01T00:00:00Z') }); |
| 143 | + const b = new InMemoryRepository({ now: () => new Date('2025-01-02T00:00:00Z') }); |
| 144 | + await a.put(ref('item'), { who: 'a' }, { parentVersion: null, actor: 't' }); |
| 145 | + await b.put(ref('item'), { who: 'b' }, { parentVersion: null, actor: 't' }); |
| 146 | + const layered = new LayeredRepository({ |
| 147 | + layers: [{ label: 'a', repo: a }, { label: 'b', repo: b }], |
| 148 | + }); |
| 149 | + const events: MetadataEvent[] = []; |
| 150 | + for await (const evt of layered.history(ref('item'))) events.push(evt); |
| 151 | + expect(events).toHaveLength(2); |
| 152 | + expect(events[0]!.source.startsWith('a:')).toBe(true); |
| 153 | + expect(events[1]!.source.startsWith('b:')).toBe(true); |
| 154 | + }); |
| 155 | +}); |
0 commit comments