Skip to content

Commit c359746

Browse files
committed
test(messaging): pin that a none written during the digest conversion survives it
Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGufzi4PechTRCTBrfxV8W
1 parent 9f3c338 commit c359746

1 file changed

Lines changed: 95 additions & 1 deletion

File tree

‎packages/services/service-messaging/src/preference-digest-three-states.integration.test.ts‎

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
* column still carries the old physical DEFAULT 'none' (the temporary
1010
* owner-side hook, `preference-digest-unset.ts`, until #36245);
1111
* - the legacy `none` nobody chose becomes empty ONCE, across every
12-
* organization, recorded in `sys_migration`; a `none` written after that
12+
* organization, recorded in `sys_migration`; only the rows read before the
13+
* claim are rewritten, so a `none` written during the pass, or after it,
1314
* survives every later boot (`preference-digest-conversion.ts`).
1415
*/
1516

@@ -91,6 +92,29 @@ async function insert(ql: ObjectQL, organization: string, row: Record<string, un
9192
return String((await ql.insert(PREF, row, sys(organization))).id);
9293
}
9394

95+
/**
96+
* The engine `ql` with one interception: the first `method` call on `object`
97+
* runs `before` first (whatever it throws is that call's failure), then the
98+
* call itself. Every other member is `ql`'s own.
99+
*/
100+
function interceptOnce(ql: ObjectQL, method: 'update' | 'insert', object: string, before: () => Promise<void>): never {
101+
let fired = false;
102+
return new Proxy(ql, {
103+
get(target, prop) {
104+
const value = Reflect.get(target, prop, target);
105+
if (typeof value !== 'function') return value;
106+
if (prop !== method) return value.bind(target);
107+
return async (name: string, ...rest: unknown[]) => {
108+
if (name === object && !fired) {
109+
fired = true;
110+
await before();
111+
}
112+
return (value as (...args: unknown[]) => unknown).call(target, name, ...rest);
113+
};
114+
},
115+
}) as never;
116+
}
117+
94118
describe('an upgraded database: the digest column still carries the physical DEFAULT none', () => {
95119
it('an insert that leaves the digest unset stores it empty, an explicit none stays none', async () => {
96120
const file = dbFile();
@@ -152,6 +176,76 @@ describe('the one-time conversion of the legacy none', () => {
152176
expect(await digestOf(second, 'org_a', fresh)).toBe('none');
153177
});
154178

179+
it('a none written during the pass is not rewritten: only the rows read before the claim are', async () => {
180+
const file = dbFile();
181+
const legacy = await boot(file, LEGACY_PREFERENCE, { seed: true });
182+
const old = await insert(legacy, 'org_a', { user_id: 'u1', topic: '*', channel: '*', enabled: true });
183+
const daily = await insert(legacy, 'org_a', { user_id: 'u2', topic: 'chosen', channel: '*', enabled: true, digest: 'daily' });
184+
await close(legacy);
185+
186+
const ql = await boot(file, NotificationPreference, { hook: true });
187+
let created: string | undefined;
188+
// While the pass is rewriting, the deployment is serving: a new row
189+
// with an explicit none, and a member switching daily to none.
190+
const engine = interceptOnce(ql, 'update', PREF, async () => {
191+
created = await insert(ql, 'org_b', { user_id: 'u2', topic: '*', channel: '*', enabled: true, digest: 'none' });
192+
await ql.update(PREF, { digest: 'none' }, { where: { id: daily }, ...sys('org_a') } as never);
193+
});
194+
expect(await convertLegacyNoneDigestsOnce(engine, silent)).toEqual({ status: 'converted', converted: 1, failed: [] });
195+
expect(created).toBeDefined();
196+
expect(await digestOf(ql, 'org_a', old)).toBeNull();
197+
expect(await digestOf(ql, 'org_b', created!)).toBe('none');
198+
expect(await digestOf(ql, 'org_a', daily)).toBe('none');
199+
});
200+
201+
it('reads the snapshot in pages by id, so every legacy none past the first page is converted', async () => {
202+
const file = dbFile();
203+
const legacy = await boot(file, LEGACY_PREFERENCE, { seed: true });
204+
const rows: Array<[string, string]> = [];
205+
for (const [organization, user, topic] of [['org_a', 'u1', '*'], ['org_a', 'u2', '*'], ['org_b', 'u1', '*'], ['org_b', 'u2', '*'], ['org_a', 'u1', 'other']]) {
206+
rows.push([organization, await insert(legacy, organization, { user_id: user, topic, channel: '*', enabled: true })]);
207+
}
208+
await close(legacy);
209+
210+
const ql = await boot(file, NotificationPreference, { hook: true });
211+
expect(await convertLegacyNoneDigestsOnce(ql as never, silent, { pageSize: 2 })).toEqual({ status: 'converted', converted: 5, failed: [] });
212+
for (const [organization, id] of rows) expect(await digestOf(ql, organization, id)).toBeNull();
213+
});
214+
215+
it('a claim that fails after the read rewrites nothing, and leaves no record', async () => {
216+
const file = dbFile();
217+
const legacy = await boot(file, LEGACY_PREFERENCE, { seed: true });
218+
const row = await insert(legacy, 'org_a', { user_id: 'u1', topic: '*', channel: '*', enabled: true });
219+
await close(legacy);
220+
221+
const ql = await boot(file, NotificationPreference, { hook: true });
222+
const errors: string[] = [];
223+
const engine = interceptOnce(ql, 'insert', 'sys_migration', async () => {
224+
throw new Error('ledger is read-only');
225+
});
226+
const out = await convertLegacyNoneDigestsOnce(engine, { ...silent, error: (m: string) => errors.push(m) });
227+
expect(out).toEqual({ status: 'claim-failed', converted: 0, failed: [] });
228+
expect(errors).toHaveLength(1);
229+
expect(await digestOf(ql, 'org_a', row)).toBe('none');
230+
expect(await ql.findOne('sys_migration', { where: { id: PREFERENCE_DIGEST_NONE_CONVERSION_ID } }, { context: { isSystem: true } as ExecutionContext })).toBeFalsy();
231+
});
232+
233+
it('another node claiming between the read and the claim counts as run, and this pass rewrites nothing', async () => {
234+
const file = dbFile();
235+
const legacy = await boot(file, LEGACY_PREFERENCE, { seed: true });
236+
const row = await insert(legacy, 'org_a', { user_id: 'u1', topic: '*', channel: '*', enabled: true });
237+
await close(legacy);
238+
239+
const ql = await boot(file, NotificationPreference, { hook: true });
240+
// The other node's claim lands first; this node's insert of the same
241+
// ledger id is then refused by the store.
242+
const engine = interceptOnce(ql, 'insert', 'sys_migration', async () => {
243+
await ql.insert('sys_migration', { id: PREFERENCE_DIGEST_NONE_CONVERSION_ID, last_run_at: new Date().toISOString(), blocking: 0 }, { context: { isSystem: true } as ExecutionContext });
244+
});
245+
expect(await convertLegacyNoneDigestsOnce(engine, silent)).toEqual({ status: 'already-run', converted: 0, failed: [] });
246+
expect(await digestOf(ql, 'org_a', row)).toBe('none');
247+
});
248+
155249
it('without a ledger on the kernel it does not run, so nothing it could not record is rewritten', async () => {
156250
const file = dbFile();
157251
const legacy = await boot(file, LEGACY_PREFERENCE, { seed: true, ledger: false });

0 commit comments

Comments
 (0)