|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #12696 — `startWatcher()` must pass `atomic` to chokidar EXPLICITLY. |
| 5 | + * |
| 6 | + * chokidar 5's defaults literal assigns `atomic: true` BEFORE the caller's |
| 7 | + * options are spread in (`node_modules/chokidar/index.js`): |
| 8 | + * |
| 9 | + * const opts = { |
| 10 | + * // Defaults |
| 11 | + * ... |
| 12 | + * atomic: true, // NOTE: overwritten later (depends on usePolling) |
| 13 | + * ..._opts, |
| 14 | + * ... |
| 15 | + * }; |
| 16 | + * ... |
| 17 | + * // Editor atomic write normalization enabled by default with fs.watch |
| 18 | + * if (opts.atomic === undefined) |
| 19 | + * opts.atomic = !opts.usePolling; |
| 20 | + * |
| 21 | + * so the "overwritten later" comment is aspirational: the correction can |
| 22 | + * only run when the caller omits `atomic` AND that omission left |
| 23 | + * `opts.atomic === undefined`, but it never does — the defaults literal |
| 24 | + * already assigned `true`, and the caller's spread has no `atomic` key to |
| 25 | + * override it with. The branch is dead. |
| 26 | + * |
| 27 | + * The consequence for THIS pin: the RESOLVED value chokidar hands back |
| 28 | + * (`watcher.options.atomic`) is `true` whether `startWatcher()` declares it |
| 29 | + * or not — a merged-value read cannot tell "declared here" apart from |
| 30 | + * "inherited a dead branch that happens to agree today". A pin written |
| 31 | + * against that merged read would stay green across the exact regression it |
| 32 | + * exists to catch: a future chokidar release that fixes the ordering (making |
| 33 | + * the correction real) would then silently flip this repository's watcher to |
| 34 | + * `atomic: false` under `usePolling` — losing the 100ms unlink-coalescing |
| 35 | + * deferral and the `DOT_RE` editor-temp matcher from a live delivery path — |
| 36 | + * and nothing here would notice. |
| 37 | + * |
| 38 | + * So this pin does not read the merged option. It reads the ACTUAL call |
| 39 | + * `startWatcher()` makes to `chokidar.watch()`, via a spy that lets the real |
| 40 | + * call through unchanged (this pins DECLARATION, not delivery — every other |
| 41 | + * watcher behaviour in this package must keep working identically). That is |
| 42 | + * a runtime observation of what this repository hands off, not a grep of the |
| 43 | + * call-site literal, and it is the one thing whose presence a future |
| 44 | + * chokidar default cannot silently override. |
| 45 | + * |
| 46 | + * ⛔ No wall-clock wait anywhere in this file, deliberately (see |
| 47 | + * `watch-dot-root.test.ts` for the standing prohibition and its history of |
| 48 | + * merge-queue ejections). None is needed: the root is created by `mkdtemp()` |
| 49 | + * before `start()` runs, so `start()` arms the watcher SYNCHRONOUSLY inside |
| 50 | + * its own call (see its comment on #7000/#9339) — `chokidar.watch()` is |
| 51 | + * invoked, and the spy has recorded the call, by the time `start()`'s |
| 52 | + * promise resolves. This case never touches the 100ms unlink window itself |
| 53 | + * (out of scope for #12696 — see the card's "carry forward" section). |
| 54 | + */ |
| 55 | + |
| 56 | +import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; |
| 57 | +import fs from 'node:fs/promises'; |
| 58 | +import path from 'node:path'; |
| 59 | +import os from 'node:os'; |
| 60 | +import chokidar from 'chokidar'; |
| 61 | +import { FileSystemRepository } from '../src/index.js'; |
| 62 | + |
| 63 | +describe('FileSystemRepository — startWatcher() declares `atomic` explicitly (#12696)', () => { |
| 64 | + let root: string; |
| 65 | + let repo: FileSystemRepository | undefined; |
| 66 | + let watchSpy: ReturnType<typeof vi.spyOn>; |
| 67 | + |
| 68 | + beforeEach(async () => { |
| 69 | + root = await fs.mkdtemp(path.join(os.tmpdir(), 'objectstack-fsatomic-')); |
| 70 | + // Let the real call through — nothing here is faked, so every other |
| 71 | + // watcher behaviour this case exercises stays exactly as it runs in |
| 72 | + // production. |
| 73 | + watchSpy = vi.spyOn(chokidar, 'watch'); |
| 74 | + }); |
| 75 | + |
| 76 | + afterEach(async () => { |
| 77 | + if (repo) await repo.close().catch(() => undefined); |
| 78 | + repo = undefined; |
| 79 | + watchSpy.mockRestore(); |
| 80 | + await fs.rm(root, { recursive: true, force: true }); |
| 81 | + }); |
| 82 | + |
| 83 | + it('passes `atomic: true` as an OWN key of the options object handed to chokidar.watch()', async () => { |
| 84 | + repo = new FileSystemRepository({ root, org: 'system', disableWatch: false }); |
| 85 | + await repo.start(); |
| 86 | + |
| 87 | + expect(watchSpy).toHaveBeenCalledTimes(1); |
| 88 | + const [, options] = watchSpy.mock.calls[0] as [string, Record<string, unknown>]; |
| 89 | + |
| 90 | + // The behaviour this pin exists to protect: `atomic` must be an OWN key |
| 91 | + // of the call-site options object, not merely absent-and-coincidentally |
| 92 | + // `true` after chokidar's internal merge (see file header). |
| 93 | + expect(Object.prototype.hasOwnProperty.call(options, 'atomic')).toBe(true); |
| 94 | + expect(options.atomic).toBe(true); |
| 95 | + }); |
| 96 | + |
| 97 | + // Positive control (#12696 ablation) — a SEPARATE case so it runs to |
| 98 | + // completion, and so its verdict, on its own assertions, is independent of |
| 99 | + // whatever the case above does. `usePolling` is unconditionally declared |
| 100 | + // today and untouched by this card's fix; it must stay green under the |
| 101 | + // ablation that removes the explicit `atomic`. If it ever went red too, |
| 102 | + // the spy/harness would be the suspect, not the `atomic` declaration. |
| 103 | + it('[control] passes `usePolling: true` as an OWN key of the same options object', async () => { |
| 104 | + repo = new FileSystemRepository({ root, org: 'system', disableWatch: false }); |
| 105 | + await repo.start(); |
| 106 | + |
| 107 | + expect(watchSpy).toHaveBeenCalledTimes(1); |
| 108 | + const [, options] = watchSpy.mock.calls[0] as [string, Record<string, unknown>]; |
| 109 | + |
| 110 | + expect(Object.prototype.hasOwnProperty.call(options, 'usePolling')).toBe(true); |
| 111 | + expect(options.usePolling).toBe(true); |
| 112 | + }); |
| 113 | +}); |
0 commit comments