Skip to content

Commit c1840d8

Browse files
committed
fix(cloud-connection): tie the unmanaged-copy record to its base instance; name the record file; say an unkept record
The record now keeps the base's package-store row (`version`, `created_at`), and the purge answers 404 when that row is gone or carries another `created_at`, so a later base under the copy's id never inherits the purge. The purge checks the base is installed before reading the record (a removed base answers 404, not 503), an unreadable record's 503 names the local file to repair, and an install whose record was not kept says so in its answer (`copyRecord`). Claude-Session: https://claude.ai/code/session_01FEkQ5pQJCx8STEq28nr3Ej Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 86246c5 commit c1840d8

3 files changed

Lines changed: 299 additions & 38 deletions

File tree

‎packages/cloud-connection/src/marketplace-install-local-install-modes.test.ts‎

Lines changed: 124 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -654,12 +654,22 @@ describe('install-local — an unmanaged copy\'s sample data, asked for with `se
654654
* `@objectstack/metadata-protocol`'s `package-unmanaged-install.test.ts`),
655655
* (a `sys_metadata` row of the base, as the store holds it), and serves
656656
* each back through the by-name `getMetaItem`. `tables` outlives a mount,
657-
* so a second mount is a restart over the same database.
657+
* so a second mount is a restart over the same database. `packageRows`
658+
* stands in for the `package` service's `sys_packages` rows (`get`), the
659+
* import minting each new base's `created_at`; `packageStore: false`
660+
* mounts a runtime with no `package` service.
658661
*/
659-
function world() {
662+
function world(opts: { packageStore?: boolean } = {}) {
660663
const tables: Record<string, Row[]> = {};
661664
const objects = new Map<string, any>();
662665
const packages = new Map<string, { manifest: Record<string, unknown> }>();
666+
const packageRows = new Map<string, { version: string; created_at: string }>();
667+
let stamp = 0;
668+
const packageGet = vi.fn(async (id: string, version = 'latest') => {
669+
const row = packageRows.get(id);
670+
if (!row || (version !== 'latest' && row.version !== version)) return null;
671+
return { id, ...row };
672+
});
663673
const writes: string[] = [];
664674
let nextId = 0;
665675
const known = (object: string) => {
@@ -735,6 +745,7 @@ describe('install-local — an unmanaged copy\'s sample data, asked for with `se
735745
});
736746
}
737747
packages.set(targetPackageId, { manifest: { id: targetPackageId, namespace: source.namespace } });
748+
packageRows.set(targetPackageId, { version: '1.0.0', created_at: `2026-11-08 07:00:${String(++stamp).padStart(2, '0')}` });
738749
const copied: Array<{ type: string; name: string }> = [];
739750
for (const o of source.objects ?? []) {
740751
objects.set(o.name, o);
@@ -776,6 +787,7 @@ describe('install-local — an unmanaged copy\'s sample data, asked for with `se
776787
importUnmanagedPackage, getMetaItem,
777788
registerUninstallCleanup: (name: string, cleanup: any) => { cleanups.set(name, cleanup); },
778789
},
790+
...(opts.packageStore === false ? {} : { package: { get: packageGet } }),
779791
};
780792
const logger = { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() };
781793
const ctx = {
@@ -801,7 +813,7 @@ describe('install-local — an unmanaged copy\'s sample data, asked for with `se
801813
logger,
802814
};
803815
}
804-
return { tables, writes, seedRows, packages, importUnmanagedPackage, getMetaItem, cleanups, boot };
816+
return { tables, writes, seedRows, packages, packageRows, packageGet, importUnmanagedPackage, getMetaItem, cleanups, boot };
805817
}
806818

807819
it('off by default: the import is not asked for the sample data, no row lands, and the answer says it was skipped', async () => {
@@ -958,14 +970,123 @@ describe('install-local — an unmanaged copy\'s sample data, asked for with `se
958970
await h.install({ manifest: seededCrm(), installMode: 'unmanaged', seedSampleData: true });
959971
expect(JSON.parse(readFileSync(COPY_RECORD(), 'utf8'))).toMatchObject({
960972
baseId: 'com.example.crm-copy', sourcePackageId: 'com.example.crm', seeded: true, copiedBy: INSTALLER,
973+
instance: { version: '1.0.0', createdAt: w.packageRows.get('com.example.crm-copy')!.created_at },
961974
});
962975
const listing = new LocalManifestSource(dir).list();
963976
expect([listing.entries, listing.skipped]).toEqual([[], []]);
964977
writeFileSync(COPY_RECORD(), '{ not json');
965978
const before = w.writes.length;
966979
const res = await h.purge('com.example.crm-copy');
967980
expect(res.status, JSON.stringify(res.payload)).toBe(503);
981+
expect(res.payload.error.code).toBe('SERVICE_UNAVAILABLE');
982+
// The local file to repair, not a store to wait for.
983+
expect(res.payload.error.message).toContain(`Repair ${COPY_RECORD()}`);
984+
expect(res.payload.error.message).not.toContain('Retry');
985+
expect(w.writes.length).toBe(before);
986+
});
987+
988+
it('a removed base answers 404 before its record is read: an unreadable record of a base that is gone is not a 503', async () => {
989+
const w = world();
990+
const h = await w.boot();
991+
await h.install({ manifest: seededCrm(), installMode: 'unmanaged', seedSampleData: true });
992+
writeFileSync(COPY_RECORD(), '{ not json');
993+
w.packages.delete('com.example.crm-copy');
994+
const before = w.writes.length;
995+
const res = await h.purge('com.example.crm-copy');
996+
expect(res.status, JSON.stringify(res.payload)).toBe(404);
997+
expect(res.payload.error.code).toBe('RESOURCE_NOT_FOUND');
998+
expect(w.writes.length).toBe(before);
999+
});
1000+
1001+
// ── [#30808] The record is tied to the base instance, not only to its id ──
1002+
1003+
it('a later base under the copy\'s id is not reached: its package-store row carries another created_at, 404, nothing deleted', async () => {
1004+
const w = world();
1005+
const h = await w.boot();
1006+
await h.install({ manifest: seededCrm(), installMode: 'unmanaged', seedSampleData: true });
1007+
// The copy's base goes without the cleanup running (no cleanup registry,
1008+
// another replica, a failed removal) and a Studio duplicate takes its
1009+
// id: same id, same version, a row of its own. The copy's seed items
1010+
// and rows are still there, so only the instance tie refuses.
1011+
expect(existsSync(COPY_RECORD())).toBe(true);
1012+
w.packageRows.set('com.example.crm-copy', { version: '1.0.0', created_at: '2026-11-08 08:15:00' });
1013+
const before = w.writes.length;
1014+
const rows = w.tables.crm_account!.length;
1015+
const res = await h.purge('com.example.crm-copy');
1016+
expect(res.status, JSON.stringify(res.payload)).toBe(404);
1017+
expect(res.payload.error.code).toBe('RESOURCE_NOT_FOUND');
1018+
expect(w.writes.length).toBe(before);
1019+
expect(w.tables.crm_account!.length).toBe(rows);
1020+
expect(w.packageGet).toHaveBeenLastCalledWith('com.example.crm-copy', '1.0.0');
1021+
});
1022+
1023+
it('a base whose package-store row is gone is not reached (404); a package store that does not answer is a 503 naming no file', async () => {
1024+
const w = world();
1025+
const h = await w.boot();
1026+
await h.install({ manifest: seededCrm(), installMode: 'unmanaged', seedSampleData: true });
1027+
const before = w.writes.length;
1028+
w.packageGet.mockRejectedValueOnce(new Error('sys_packages is unreachable'));
1029+
const unread = await h.purge('com.example.crm-copy');
1030+
expect(unread.status, JSON.stringify(unread.payload)).toBe(503);
1031+
expect(unread.payload.error.message).not.toContain('Repair');
1032+
expect(unread.payload.error.message).not.toContain('sys_packages is unreachable');
1033+
const row = w.packageRows.get('com.example.crm-copy')!;
1034+
w.packageRows.delete('com.example.crm-copy');
1035+
expect((await h.purge('com.example.crm-copy')).status).toBe(404);
9681036
expect(w.writes.length).toBe(before);
1037+
// The same row back: the copy is reached again.
1038+
w.packageRows.set('com.example.crm-copy', row);
1039+
const res = await h.purge('com.example.crm-copy');
1040+
expect(res.status, JSON.stringify(res.payload)).toBe(200);
1041+
expect(res.payload.data).toMatchObject({ deleted: 3, errors: 0 });
1042+
});
1043+
1044+
it('with no `package` service the record holds no instance and the purge reaches the copy by id alone (the stated residual)', async () => {
1045+
const w = world({ packageStore: false });
1046+
const h = await w.boot();
1047+
const installed = await h.install({ manifest: seededCrm(), installMode: 'unmanaged', seedSampleData: true });
1048+
expect(installed.payload.data).not.toHaveProperty('copyRecord');
1049+
expect(JSON.parse(readFileSync(COPY_RECORD(), 'utf8'))).not.toHaveProperty('instance');
1050+
const res = await h.purge('com.example.crm-copy');
1051+
expect(res.status, JSON.stringify(res.payload)).toBe(200);
1052+
expect(res.payload.data).toMatchObject({ deleted: 3, errors: 0 });
1053+
});
1054+
1055+
it('a record that is not kept is said in the install answer (`copyRecord`), and the copy is then out of the purge\'s reach', async () => {
1056+
// The record cannot be written: its directory's name is taken by a file.
1057+
{
1058+
const w = world();
1059+
const h = await w.boot();
1060+
writeFileSync(join(dir, 'unmanaged-copies'), 'not a directory');
1061+
const res = await h.install({ manifest: seededCrm(), installMode: 'unmanaged', seedSampleData: true });
1062+
expect(res.status, JSON.stringify(res.payload)).toBe(200);
1063+
expect(res.payload.data.seeded).toMatchObject({ mode: 'inline', inserted: 3 });
1064+
expect(res.payload.data.copyRecord).toEqual({ written: false, message: expect.stringContaining('was not kept') });
1065+
expect(res.payload.data.copyRecord.message).toContain(join(dir, 'unmanaged-copies'));
1066+
expect((await h.purge('com.example.crm-copy')).status).toBe(404);
1067+
rmSync(join(dir, 'unmanaged-copies'), { force: true });
1068+
}
1069+
// The package store holds no row of the new base: no record is written.
1070+
{
1071+
const w = world();
1072+
const h = await w.boot();
1073+
w.packageGet.mockResolvedValueOnce(null);
1074+
const res = await h.install({ manifest: seededCrm(), installMode: 'unmanaged', seedSampleData: true });
1075+
expect(res.status, JSON.stringify(res.payload)).toBe(200);
1076+
expect(res.payload.data.copyRecord).toEqual({
1077+
written: false, message: expect.stringContaining('the package store did not answer with the new base\'s row'),
1078+
});
1079+
expect(existsSync(COPY_RECORD())).toBe(false);
1080+
expect((await h.purge('com.example.crm-copy')).status).toBe(404);
1081+
}
1082+
// A record that is kept adds no key to the answer.
1083+
{
1084+
const w = world();
1085+
const h = await w.boot();
1086+
rmSync(join(dir, 'unmanaged-copies'), { recursive: true, force: true });
1087+
const res = await h.install({ manifest: seededCrm(), installMode: 'unmanaged', seedSampleData: true });
1088+
expect(res.payload.data).not.toHaveProperty('copyRecord');
1089+
}
9691090
});
9701091

9711092
it('a copy made without the sample data answers that no sample data was copied — not "declares no seed datasets"', async () => {

0 commit comments

Comments
 (0)