Skip to content

Commit fefaa51

Browse files
committed
test(metadata): the R5-A control seeds the database loader from a real file-system item
Epic ruling 6801410166 pin 4 names a file-system item: the control now runs the priming pass over the plugin's own FilesystemLoader (a template file under a temporary rootDir, the host's packageId stamped) beside a DatabaseLoader, and pins the exact writes: one sys_metadata row and its history row, no update. Claude-Session: https://claude.ai/code/session_01Utp6CZu9RRx2o8sFUUpEPU Co-authored-by: Claude <noreply@anthropic.com>
1 parent e8c7447 commit fefaa51

1 file changed

Lines changed: 24 additions & 12 deletions

File tree

‎packages/metadata/src/loaders/database-loader.test.ts‎

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import type { IDataDriver } from '@objectstack/spec/contracts';
66
import { MetadataManager } from '../metadata-manager.js';
77
import { MemoryLoader } from './memory-loader.js';
88
import { MetadataPlugin } from '../plugin.js';
9+
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
10+
import { tmpdir } from 'node:os';
11+
import { join } from 'node:path';
912

1013
// Suppress logger output during tests. Stable object (not a fresh one per
1114
// `createLogger()` call) so the #5108 block can assert on what `list()` says
@@ -1905,7 +1908,8 @@ describe('[#31699 R4-A] the save of a bundled type writes its own member\'s row'
19051908
* BEFORE (measured at cfb8f300, same fixtures): a package's `en-US` row beside
19061909
* a package-less `en-US` row was overwritten with the package-less body
19071910
* (version 3, one update); beside a package-less `fr-FR` row, a package-bound
1908-
* copy of it was inserted; a non-`datasource:` loader's item seeded one row.
1911+
* copy of it was inserted; a file-system item seeded one row (the same with
1912+
* the source skip ablated, and after it).
19091913
*/
19101914
describe('[#31699 R5-A] the priming pass never writes back a row it read from the same loader', () => {
19111915
const ALPHA = 'com.alpha.app';
@@ -1920,12 +1924,13 @@ describe('[#31699 R5-A] the priming pass never writes back a row it read from th
19201924
((await driver.find('sys_metadata', { where: { type: 'email_template' } } as never)) as Array<Record<string, any>>)
19211925
.map((row) => [row.package_id ?? null, row.bundle_member ?? null, JSON.parse(row.metadata).subject])
19221926
.sort((a, b) => String(a).localeCompare(String(b)));
1923-
/** The real pass, over exactly `loaders` (the manager's default loaders removed). */
1924-
const bootPass = async (...loaders: Array<DatabaseLoader | MemoryLoader>) => {
1925-
const plugin = new MetadataPlugin({ watch: false, config: { bootstrap: 'eager' } });
1927+
/** The real pass over `loaders`; the plugin's own file-system loader is kept only with a `rootDir`. */
1928+
const bootPass = async (loaders: DatabaseLoader[], options: { rootDir?: string; packageId?: string } = {}) => {
1929+
const plugin = new MetadataPlugin({ ...options, watch: false, config: { bootstrap: 'eager' } });
19261930
const manager = (plugin as any).manager as MetadataManager;
1931+
const fileSystem = options.rootDir ? [...(manager as any).loaders.values()] : [];
19271932
(manager as any).loaders.clear();
1928-
for (const loader of loaders) manager.registerLoader(loader);
1933+
for (const loader of [...fileSystem, ...loaders]) manager.registerLoader(loader);
19291934
await (plugin as any)._loadFromFileSystem({ logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() } });
19301935
};
19311936

@@ -1937,7 +1942,7 @@ describe('[#31699 R5-A] the priming pass never writes back a row it read from th
19371942
await seed('alpha_en', ALPHA, 'en-US', 'alpha own');
19381943
await seed('env_en', null, 'en-US', 'env older');
19391944
const before = await stored();
1940-
await bootPass(new DatabaseLoader({ driver }));
1945+
await bootPass([new DatabaseLoader({ driver })]);
19411946
expect(await stored()).toEqual(before);
19421947
expect(driver.update).not.toHaveBeenCalled();
19431948
expect(driver.create).toHaveBeenCalledTimes(2);
@@ -1947,16 +1952,23 @@ describe('[#31699 R5-A] the priming pass never writes back a row it read from th
19471952
await seed('alpha_en', ALPHA, 'en-US', 'alpha own');
19481953
await seed('env_fr', null, 'fr-FR', 'env fr');
19491954
const before = await stored();
1950-
await bootPass(new DatabaseLoader({ driver }));
1955+
await bootPass([new DatabaseLoader({ driver })]);
19511956
expect(await stored()).toEqual(before);
19521957
expect(driver.update).not.toHaveBeenCalled();
19531958
expect(driver.create).toHaveBeenCalledTimes(2);
19541959
});
19551960

1956-
it('CONTROL: an item read from another loader still seeds the database loader', async () => {
1957-
const source = new MemoryLoader();
1958-
await source.save('email_template', 'welcome', { name: 'welcome', locale: 'en-US', subject: 'from source', bodyHtml: '<p/>', _packageId: ALPHA });
1959-
await bootPass(source, new DatabaseLoader({ driver }));
1960-
expect(await stored()).toEqual([[ALPHA, null, 'from source']]);
1961+
it('CONTROL: a file-system item still seeds the database loader', async () => {
1962+
const rootDir = mkdtempSync(join(tmpdir(), 'r5a-'));
1963+
try {
1964+
mkdirSync(join(rootDir, 'email_template'));
1965+
writeFileSync(join(rootDir, 'email_template', 'welcome.email-template.json'), JSON.stringify({ name: 'welcome', locale: 'en-US', subject: 'from file', bodyHtml: '<p/>' }));
1966+
await bootPass([new DatabaseLoader({ driver })], { rootDir, packageId: ALPHA });
1967+
expect(await stored()).toEqual([[ALPHA, null, 'from file']]);
1968+
expect(vi.mocked(driver.create).mock.calls.map(([table]) => table)).toEqual(['sys_metadata', 'sys_metadata_history']);
1969+
expect(driver.update).not.toHaveBeenCalled();
1970+
} finally {
1971+
rmSync(rootDir, { recursive: true, force: true });
1972+
}
19611973
});
19621974
});

0 commit comments

Comments
 (0)