|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #7150 — the watcher must see changes under a **dot-rooted** watch root. |
| 5 | + * |
| 6 | + * `MetadataPlugin` attaches the repository at `REPO_SUBDIR = |
| 7 | + * '.objectstack/metadata'` under the project root, so in the layout the |
| 8 | + * product actually ships the watched root path contains a dot segment. |
| 9 | + * chokidar applies its `ignored` matcher to the watched root itself, not only |
| 10 | + * to entries found underneath it, so the old bare dotfile regex |
| 11 | + * (`/(^|[\\/])\../`) matched that segment and the whole watch was inert: |
| 12 | + * `getWatched()` returned `{}` and no event ever fired. The consumer side is |
| 13 | + * live code — `MetadataManager.setRepository()` subscribes to `repo.watch({})` |
| 14 | + * and invalidates the registry and `list()` cache per event — so the source |
| 15 | + * could never fire for the layout that is in use. |
| 16 | + * |
| 17 | + * The two cases below are the two halves of the watcher's promise, which the |
| 18 | + * fix has to satisfy together: **see everything under the root** (case 1) and |
| 19 | + * **ignore the repository's own bookkeeping** (case 2). A fix that only |
| 20 | + * widened the matcher would pass case 1 and fail case 2. |
| 21 | + * |
| 22 | + * Sibling pin: `no-root-on-attach.test.ts` deliberately uses a root that is |
| 23 | + * NOT under a dot-directory, because it measures watcher *arming* (#7000). |
| 24 | + * Do not repurpose it — it has to keep measuring what it measures. |
| 25 | + */ |
| 26 | + |
| 27 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 28 | +import fs from 'node:fs/promises'; |
| 29 | +import path from 'node:path'; |
| 30 | +import os from 'node:os'; |
| 31 | +import type { MetaRef, MetadataEvent } from '@objectstack/metadata-core'; |
| 32 | +import { FileSystemRepository } from '../src/index.js'; |
| 33 | + |
| 34 | +const ref = (name: string): MetaRef => ({ org: 'system', type: 'view', name }); |
| 35 | +const sleep = (ms: number) => new Promise<void>((r) => setTimeout(r, ms)); |
| 36 | + |
| 37 | +describe('FileSystemRepository watcher — dot-rooted watch root (#7150)', () => { |
| 38 | + /** Stands in for the project directory. */ |
| 39 | + let base: string; |
| 40 | + /** Stands in for `<project>/.objectstack/metadata` — the production layout. */ |
| 41 | + let root: string; |
| 42 | + let repo: FileSystemRepository | undefined; |
| 43 | + |
| 44 | + beforeEach(async () => { |
| 45 | + base = await fs.mkdtemp(path.join(os.tmpdir(), 'objectstack-fs7150-')); |
| 46 | + root = path.join(base, '.objectstack', 'metadata'); |
| 47 | + }); |
| 48 | + |
| 49 | + afterEach(async () => { |
| 50 | + if (repo) await repo.close().catch(() => undefined); |
| 51 | + repo = undefined; |
| 52 | + await fs.rm(base, { recursive: true, force: true }); |
| 53 | + }); |
| 54 | + |
| 55 | + /** |
| 56 | + * Drain `repo.watch()` into an array for the life of the case. `since: 999` |
| 57 | + * skips log replay, so everything collected here came from the watcher. |
| 58 | + */ |
| 59 | + function collectEvents(r: FileSystemRepository): { |
| 60 | + events: MetadataEvent[]; |
| 61 | + first: Promise<void>; |
| 62 | + stop: () => Promise<void>; |
| 63 | + } { |
| 64 | + const iter = r.watch({ org: 'system' }, 999)[Symbol.asyncIterator](); |
| 65 | + const events: MetadataEvent[] = []; |
| 66 | + let resolveFirst!: () => void; |
| 67 | + const first = new Promise<void>((res) => { resolveFirst = res; }); |
| 68 | + let stopped = false; |
| 69 | + void (async () => { |
| 70 | + while (!stopped) { |
| 71 | + const next = await iter.next(); |
| 72 | + if (next.done) return; |
| 73 | + events.push(next.value as MetadataEvent); |
| 74 | + resolveFirst(); |
| 75 | + } |
| 76 | + })(); |
| 77 | + return { |
| 78 | + events, |
| 79 | + first, |
| 80 | + stop: async () => { stopped = true; await iter.return?.(undefined); }, |
| 81 | + }; |
| 82 | + } |
| 83 | + |
| 84 | + it('sees an external edit when the root is under a dot-directory', async () => { |
| 85 | + repo = new FileSystemRepository({ root, org: 'system' }); // watcher ENABLED |
| 86 | + await repo.start(); |
| 87 | + |
| 88 | + // The first write materializes the root and arms the watcher (#7000). |
| 89 | + await repo.put(ref('case_grid'), { label: 'original' }, { |
| 90 | + parentVersion: null, |
| 91 | + actor: 'tester', |
| 92 | + }); |
| 93 | + |
| 94 | + const sink = collectEvents(repo); |
| 95 | + // Past the 200ms self-write suppression window of the put above. |
| 96 | + await sleep(400); |
| 97 | + |
| 98 | + // An out-of-process writer: a hand edit, or a `git checkout` bringing |
| 99 | + // metadata JSON in. Before the fix this produced nothing, ever. |
| 100 | + await fs.writeFile( |
| 101 | + path.join(root, 'view', 'case_grid.json'), |
| 102 | + JSON.stringify({ label: 'externally edited' }, null, 2), |
| 103 | + ); |
| 104 | + |
| 105 | + await Promise.race([sink.first, sleep(8000)]); |
| 106 | + await sink.stop(); |
| 107 | + |
| 108 | + expect(sink.events).toHaveLength(1); |
| 109 | + expect(sink.events[0]!.op).toBe('update'); |
| 110 | + expect(sink.events[0]!.ref.name).toBe('case_grid'); |
| 111 | + expect(sink.events[0]!.source).toBe('fs'); |
| 112 | + expect(sink.events[0]!.actor).toBe('fs'); |
| 113 | + }, 30_000); |
| 114 | + |
| 115 | + it('still ignores dot entries UNDER the root, including its own bookkeeping', async () => { |
| 116 | + repo = new FileSystemRepository({ root, org: 'system' }); // watcher ENABLED |
| 117 | + await repo.start(); |
| 118 | + await repo.put(ref('seed'), { label: 'seed' }, { parentVersion: null, actor: 'tester' }); |
| 119 | + |
| 120 | + const sink = collectEvents(repo); |
| 121 | + await sleep(400); |
| 122 | + |
| 123 | + // Noise that must stay invisible. `.cache/x.json` and `view/.scratch.json` |
| 124 | + // are the measured leaks of "drop the matcher and let `parseItemPath` |
| 125 | + // decide": that guard rejects the single name `.objectstack`, so these two |
| 126 | + // parse as type `.cache` and as an item named `.scratch` respectively — |
| 127 | + // while `scanHeads` skips every dot entry on boot. `.objectstack/.log/` |
| 128 | + // is the repository's own change log. |
| 129 | + await fs.mkdir(path.join(root, '.cache'), { recursive: true }); |
| 130 | + await fs.writeFile(path.join(root, '.cache', 'x.json'), '{"junk":true}'); |
| 131 | + await fs.writeFile(path.join(root, 'view', '.scratch.json'), '{"junk":true}'); |
| 132 | + await fs.appendFile( |
| 133 | + path.join(root, '.objectstack', '.log', 'main.jsonl'), |
| 134 | + '{"not":"an event"}\n', |
| 135 | + ); |
| 136 | + |
| 137 | + // Give the 1s poll several turns to deliver anything it was going to. |
| 138 | + await sleep(4000); |
| 139 | + expect(sink.events).toEqual([]); |
| 140 | + |
| 141 | + // Control: the watcher is genuinely alive, so the emptiness above is the |
| 142 | + // interesting kind and not a watcher that never armed. |
| 143 | + await fs.writeFile( |
| 144 | + path.join(root, 'view', 'seed.json'), |
| 145 | + JSON.stringify({ label: 'really edited' }, null, 2), |
| 146 | + ); |
| 147 | + await Promise.race([sink.first, sleep(8000)]); |
| 148 | + await sink.stop(); |
| 149 | + |
| 150 | + expect(sink.events).toHaveLength(1); |
| 151 | + expect(sink.events[0]!.ref.name).toBe('seed'); |
| 152 | + expect(sink.events[0]!.op).toBe('update'); |
| 153 | + }, 30_000); |
| 154 | +}); |
0 commit comments