/** * @module cosmology/workspace/cosmology-workspace-service.respec * * Respec suite for CosmologyWorkspaceService. * Tests strict fail-fast init* vs ensure* contracts for Cosmos, Galaxy, and Branch spaces, * and programmatic direct commitItems mutations. */ import { respecfully, ifWe, ifWeMight, iReckon, // keep both ifWe and ifWeMight per RESPEC_GIB_GUIDANCE.md } from '@ibgib/helper-gib/dist/respec-gib/respec-gib.mjs'; import { createInMemoryTempMetaspaceAndSpace } from '../../witness/space/metaspace/metaspace-innerspace/metaspace-innerspace.mjs'; import { CosmologyWorkspaceService } from './cosmology-workspace-service.mjs'; import { GalaxyPresetType } from '../galaxy/galaxy-configs.mjs'; import { getIbGibAddr } from '@ibgib/ts-gib/dist/helper.mjs'; import { getIbGibFromSpace } from '../../common/other/ibgib-helper.mjs'; const lc = `[cosmology-workspace-service.respec.mts]`; await respecfully(lc, `CosmologyWorkspaceService Core Specs`, async () => { await respecfully(lc, `Cosmos Fail-Fast Lifecycle`, async () => { const { metaspace, space: localSpace } = await createInMemoryTempMetaspaceAndSpace(); const workspaceSvc = new CosmologyWorkspaceService({ metaspace, localSpace, cosmosName: 'cosmos-alpha', }); await ifWe(lc, `initializes a brand-new Cosmos and lists it`, async () => { const cosmos = await workspaceSvc.initCosmos({ cosmosName: 'cosmos-alpha', description: 'First Alpha Cosmos', }); iReckon(lc, cosmos).isGonnaBeTruthy(); iReckon(lc, cosmos.data.cosmosName).willEqual('cosmos-alpha'); const all = await workspaceSvc.listCosmoses(); iReckon(lc, all.length).willEqual(1); iReckon(lc, all[0].data.cosmosName).willEqual('cosmos-alpha'); }); await ifWe(lc, `fails fast when initCosmos is called on an existing Cosmos`, async () => { let error: any = null; try { await workspaceSvc.initCosmos({ cosmosName: 'cosmos-alpha', }); } catch (err: any) { error = err; } iReckon(lc, error).isGonnaBeTruthy(); iReckon(lc, error.message).includes('already initialized'); }); await ifWe(lc, `ensureCosmos resolves an existing Cosmos`, async () => { const cosmos = await workspaceSvc.ensureCosmos({ cosmosName: 'cosmos-alpha' }); iReckon(lc, cosmos.data.cosmosName).willEqual('cosmos-alpha'); // Default cosmosName resolution const defaultCosmos = await workspaceSvc.ensureCosmos(); iReckon(lc, defaultCosmos.data.cosmosName).willEqual('cosmos-alpha'); }); await ifWe(lc, `ensureCosmos fails fast on a non-existent Cosmos`, async () => { let error: any = null; try { await workspaceSvc.ensureCosmos({ cosmosName: 'cosmos-non-existent' }); } catch (err: any) { error = err; } iReckon(lc, error).isGonnaBeTruthy(); iReckon(lc, error.message).includes('not found in localSpace'); }); }); await respecfully(lc, `Galaxy Fail-Fast Lifecycle`, async () => { const { metaspace, space: localSpace } = await createInMemoryTempMetaspaceAndSpace(); const workspaceSvc = new CosmologyWorkspaceService({ metaspace, localSpace, cosmosName: 'cosmos-beta', galaxyName: 'code-1', }); await workspaceSvc.initCosmos({ cosmosName: 'cosmos-beta' }); await ifWe(lc, `initializes a new Galaxy in Cosmos with dedicated branch space`, async () => { const galRes = await workspaceSvc.initGalaxy({ cosmosName: 'cosmos-beta', galaxyName: 'code-1', discriminator: GalaxyPresetType.code, }); iReckon(lc, galRes.galaxyIbGib).isGonnaBeTruthy(); iReckon(lc, galRes.branchSpace).isGonnaBeTruthy(); iReckon(lc, galRes.branchIbGib).isGonnaBeTruthy(); iReckon(lc, galRes.sqGalaxyAddr).isGonnaBeTruthy(); const galaxies = await workspaceSvc.listGalaxies({ cosmosName: 'cosmos-beta' }); iReckon(lc, galaxies.length).willEqual(1); iReckon(lc, galaxies[0].galaxyName).willEqual('code-1'); }); await ifWe(lc, `fails fast when initGalaxy is called on an existing Galaxy`, async () => { let error: any = null; try { await workspaceSvc.initGalaxy({ cosmosName: 'cosmos-beta', galaxyName: 'code-1', }); } catch (err: any) { error = err; } iReckon(lc, error).isGonnaBeTruthy(); iReckon(lc, error.message).includes('already exists in Cosmos'); }); await ifWe(lc, `ensureGalaxy resolves an existing Galaxy`, async () => { const res = await workspaceSvc.ensureGalaxy({ cosmosName: 'cosmos-beta', galaxyName: 'code-1', }); iReckon(lc, res.galaxyEntry.galaxyName).willEqual('code-1'); iReckon(lc, res.galaxyIbGib.data.galaxyName).willEqual('code-1'); }); await ifWe(lc, `ensureGalaxy fails fast on a non-existent Galaxy`, async () => { let error: any = null; try { await workspaceSvc.ensureGalaxy({ cosmosName: 'cosmos-beta', galaxyName: 'galaxy-phantom', }); } catch (err: any) { error = err; } iReckon(lc, error).isGonnaBeTruthy(); iReckon(lc, error.message).includes('not found in Cosmos'); }); }); await respecfully(lc, `Branch Fail-Fast Lifecycle & ensureBranch`, async () => { const { metaspace, space: localSpace } = await createInMemoryTempMetaspaceAndSpace(); const workspaceSvc = new CosmologyWorkspaceService({ metaspace, localSpace, cosmosName: 'cosmos-gamma', galaxyName: 'code-1', branchName: 'main', }); await workspaceSvc.initCosmos({ cosmosName: 'cosmos-gamma' }); await workspaceSvc.initGalaxy({ cosmosName: 'cosmos-gamma', galaxyName: 'code-1', branchName: 'main', }); await ifWe(lc, `ensureBranch resolves the default main branch and its dedicated space`, async () => { const branchRes = await workspaceSvc.ensureBranch({ cosmosName: 'cosmos-gamma', galaxyName: 'code-1', branchName: 'main', }); iReckon(lc, branchRes.branchInfo.branchName).willEqual('main'); iReckon(lc, branchRes.branchSpace).isGonnaBeTruthy(); iReckon(lc, branchRes.branchIbGib.data.name).willEqual('main'); }); await ifWe(lc, `ensureBranch fails fast on a non-existent branch`, async () => { let error: any = null; try { await workspaceSvc.ensureBranch({ cosmosName: 'cosmos-gamma', galaxyName: 'code-1', branchName: 'non-existent-feature', }); } catch (err: any) { error = err; } iReckon(lc, error).isGonnaBeTruthy(); iReckon(lc, error.message).includes('not found in Galaxy'); }); }); await respecfully(lc, `Programmatic commitItems Direct In-Band Mutation`, async () => { const { metaspace, space: localSpace } = await createInMemoryTempMetaspaceAndSpace(); const workspaceSvc = new CosmologyWorkspaceService({ metaspace, localSpace, cosmosName: 'cosmos-delta', galaxyName: 'notes-1', branchName: 'main', }); await workspaceSvc.initCosmos({ cosmosName: 'cosmos-delta' }); await workspaceSvc.initGalaxy({ cosmosName: 'cosmos-delta', galaxyName: 'notes-1', branchName: 'main', }); await ifWe(lc, `programmatically commits multiple files into branch space without worktree projection`, async () => { const commitRes = await workspaceSvc.commitItems({ cosmosName: 'cosmos-delta', galaxyName: 'notes-1', branchName: 'main', items: [ { path: 'notes/todo.md', content: '# My Tasks\n- [ ] Task 1' }, { path: 'config.json', content: JSON.stringify({ version: 1, theme: 'dark' }) }, ], message: 'Initial in-band programmatic commit', author: 'test-agent', }); iReckon(lc, commitRes.commitAddr).isGonnaBeTruthy(); iReckon(lc, commitRes.commentAddr).isGonnaBeTruthy(); iReckon(lc, commitRes.committedPaths.length).willEqual(2); iReckon(lc, commitRes.committedPaths).includes('notes/todo.md'); iReckon(lc, commitRes.committedPaths).includes('config.json'); // Verify the branch advanced const { branchIbGib } = await workspaceSvc.ensureBranch({ cosmosName: 'cosmos-delta', galaxyName: 'notes-1', branchName: 'main', }); iReckon(lc, getIbGibAddr({ ibGib: branchIbGib })).willEqual(commitRes.commitAddr); iReckon(lc, branchIbGib.rel8ns?.b2tfs_item?.[0]).willEqual(commitRes.rootItemAddr); }); await ifWe(lc, `programmatically updates a file and deletes another in a subsequent commit`, async () => { const commitRes = await workspaceSvc.commitItems({ cosmosName: 'cosmos-delta', galaxyName: 'notes-1', branchName: 'main', items: [ { path: 'notes/todo.md', content: '# My Tasks\n- [x] Task 1 completed!' }, { path: 'config.json', action: 'delete' }, ], message: 'Update todo and delete config', author: 'test-agent', }); iReckon(lc, commitRes.committedPaths).includes('notes/todo.md'); iReckon(lc, commitRes.deletedPaths).includes('config.json'); }); }); });