import { respecfully, iReckon, ifWeMight, ifWe // DO NOT REMOVE ifWeMight or ifWe even if no longer used. We constantly change these when focusing tests } from '@ibgib/helper-gib/dist/respec-gib/respec-gib.mjs'; const maam = `[${import.meta.url}]`, sir = maam; import { extractErrorMsg } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs'; import { GLOBAL_LOG_A_LOT } from '../../core-constants.mjs'; import { KeystoneProfileBuilder } from './keystone-profile-builder.mjs'; const logalot = GLOBAL_LOG_A_LOT; await respecfully(sir, 'KeystoneProfileBuilder', async () => { await ifWe(sir, 'compile configs for primary-profile.dev (low security test primary profile)', async () => { const lc = `[compile configs for primary-profile.dev]`; try { if (logalot) { console.log(`${lc} starting...`); } const builder = KeystoneProfileBuilder.buildKeystone('primary-profile.dev') .withUsername('alice') .withEmails(['alice@example.com']) .withDescription('Alice Primary Identity'); const configs = await builder.compileConfigs(); const frameDetails = builder.getFrameDetails(); iReckon(sir, configs.length).asTo('primary-profile.dev has 1 manage pool config').isGonnaBe(1); iReckon(sir, configs[0].id).asTo('pool id is manage').isGonnaBe('manage'); iReckon(sir, configs[0].rounds).asTo('dev profile has 1 round').isGonnaBe(1); iReckon(sir, configs[0].algo).asTo('dev profile algo is SHA-256').isGonnaBe('SHA-256'); iReckon(sir, frameDetails.username).asTo('frameDetails username is alice').isGonnaBe('alice'); } catch (error) { console.error(`${lc} ${extractErrorMsg(error)}`); throw error; } }); await ifWe(sir, 'compile configs for primary-profile.staging (medium security primary profile)', async () => { const lc = `[compile configs for primary-profile.staging]`; try { const builder = KeystoneProfileBuilder.buildKeystone('primary-profile.staging') .withUsername('alice') .withEmail('alice@example.com') .withDescription('Alice Staging Identity'); const configs = await builder.compileConfigs(); iReckon(sir, configs.length).asTo('primary-profile.staging has 1 manage pool config').isGonnaBe(1); iReckon(sir, configs[0].id).asTo('pool id is manage').isGonnaBe('manage'); iReckon(sir, configs[0].rounds).asTo('staging profile has 3 rounds').isGonnaBe(3); iReckon(sir, configs[0].algo).asTo('staging profile algo is SHA-512').isGonnaBe('SHA-512'); } catch (error) { console.error(`${lc} ${extractErrorMsg(error)}`); throw error; } }); await ifWe(sir, 'compile configs for primary-profile.prod (high security primary profile)', async () => { const lc = `[compile configs for primary-profile.prod]`; try { const builder = KeystoneProfileBuilder.buildKeystone('primary-profile.prod') .withUsername('alice') .withEmail('alice@example.com') .withDescription('Alice Production Identity'); const configs = await builder.compileConfigs(); iReckon(sir, configs.length).asTo('primary-profile.prod has 1 manage pool config').isGonnaBe(1); iReckon(sir, configs[0].id).asTo('pool id is manage').isGonnaBe('manage'); iReckon(sir, configs[0].rounds).asTo('prod profile has 6 rounds').isGonnaBe(6); iReckon(sir, configs[0].algo).asTo('prod profile algo is SHA-512').isGonnaBe('SHA-512'); } catch (error) { console.error(`${lc} ${extractErrorMsg(error)}`); throw error; } }); await ifWe(sir, 'compile configs for sync-profile.dev (low security test sync profile)', async () => { const lc = `[compile configs for sync-profile.dev]`; try { if (logalot) { console.log(`${lc} starting...`); } const builder = KeystoneProfileBuilder.buildKeystone('sync-profile.dev') .withUsername('alice-sync') .withDescription('Alice Sync Delegate'); const configs = await builder.compileConfigs(); iReckon(sir, configs.length).asTo('sync-profile.dev has 4 pool configs').isGonnaBe(4); const poolIds = configs.map(c => c.id); iReckon(sir, poolIds.includes('sync')).asTo('contains sync pool').isGonnaBeTrue(); iReckon(sir, poolIds.includes('manage')).asTo('contains manage pool').isGonnaBeTrue(); iReckon(sir, poolIds.includes('connect')).asTo('contains connect pool').isGonnaBeTrue(); iReckon(sir, poolIds.includes('default')).asTo('contains default sign pool').isGonnaBeTrue(); } catch (error) { console.error(`${lc} ${extractErrorMsg(error)}`); throw error; } }); await ifWe(sir, 'compile configs for sync-profile.prod (high security sync profile)', async () => { const lc = `[compile configs for sync-profile.prod]`; try { const builder = KeystoneProfileBuilder.buildKeystone('sync-profile.prod') .withUsername('alice-sync-prod') .withDescription('Alice Sync Delegate Prod'); const configs = await builder.compileConfigs(); iReckon(sir, configs.length).asTo('sync-profile.prod has 4 pool configs').isGonnaBe(4); const salts = configs.map(c => c.salt); const uniqueSalts = new Set(salts); iReckon(sir, uniqueSalts.size).asTo('unique salts count').isGonnaBe(4); const details = builder.getFrameDetails(); iReckon(sir, details.username).asTo('username').isGonnaBe('alice-sync-prod'); } catch (error) { console.error(`${lc} ${extractErrorMsg(error)}`); throw error; } }); await ifWe(sir, 'supports customized overrides', async () => { const lc = `[supports customized overrides]`; try { const builder = KeystoneProfileBuilder.buildKeystone('sync-profile.staging') .customizePool('sync', { behavior: { size: 15, targetBindingCount: 4 } }); const configs = await builder.compileConfigs(); const syncPool = configs.find(c => c.id === 'sync')!; iReckon(sir, syncPool.behavior.size).asTo('customized size').isGonnaBe(15); iReckon(sir, syncPool.behavior.targetBindingCount).asTo('customized target binding').isGonnaBe(4); } catch (error) { console.error(`${lc} ${extractErrorMsg(error)}`); throw error; } }); await ifWe(sir, 'throw error when building dev profile under NODE_ENV=production', async () => { const lc = `[throw error when building dev profile under NODE_ENV=production]`; const origEnv = process.env.NODE_ENV; try { process.env.NODE_ENV = 'production'; let errorCaught = false; try { KeystoneProfileBuilder.buildKeystone('primary-profile.dev'); } catch (e: any) { if (e.message.includes('Security Violation')) { errorCaught = true; } } iReckon(sir, errorCaught).asTo('dev profile rejected in production').isGonnaBeTrue(); } finally { process.env.NODE_ENV = origEnv; } }); await ifWe(sir, 'throw error when building unknown profile name', async () => { const lc = `[throw error when building unknown profile name]`; try { let errorCaught = false; try { KeystoneProfileBuilder.buildKeystone('nonexistent-profile' as any); } catch (e: any) { if (e.message.includes('Unknown keystone profile name')) { errorCaught = true; } } iReckon(sir, errorCaught).asTo('unknown profile rejected').isGonnaBeTrue(); } catch (error) { console.error(`${lc} ${extractErrorMsg(error)}`); throw error; } }); });