import assert from 'node:assert/strict' import { registerHooks } from 'node:module' import { test } from 'node:test' const STUB = ` const store = globalThis.__cleanupConfigStore ??= new Map(); export const packageStorage = () => ({ async get(k) { return store.get(k) ?? null }, async set(k, v) { store.set(k, JSON.stringify(v)) }, }); export const packageContext = { appBasePath: '/apps/factory/' }; export async function credential() { throw new Error('no credentials in cleanup config tests') } export async function credentials() { return [] } export const kody = {}; export const workflows = null; ` registerHooks({ resolve(spec, ctx, next) { if (spec === 'kody:runtime') { return { url: `data:text/javascript,${encodeURIComponent(STUB)}`, shortCircuit: true } } return next(spec, ctx) }, }) const config = await import('./config.ts') const STORAGE_KEY = 'factory:cleanup:v1' const store = () => (globalThis as unknown as { __cleanupConfigStore: Map }).__cleanupConfigStore function resetStore() { store().clear() } test('cleanup/config get returns disabled defaults when the store is empty', async () => { resetStore() const item = await config.get({ automation: 'docs-freshness' }) assert.equal(item.automation, 'docs-freshness') assert.equal(item.enabled, false) assert.deepEqual(item.repos, []) const audit = await config.get({ automation: 'general-audit' }) assert.equal(audit.enabled, false) assert.deepEqual(audit.repos, []) }) test('cleanup/config set enables only the named automation and persists', async () => { resetStore() const written = await config.set({ automation: 'docs-freshness', enabled: true, repos: ['kody-hq/kody'], }) assert.equal(written.enabled, true) assert.deepEqual(written.repos, ['kody-hq/kody']) const readback = await config.get({ automation: 'docs-freshness' }) assert.equal(readback.enabled, true) assert.deepEqual(readback.repos, ['kody-hq/kody']) const audit = await config.get({ automation: 'general-audit' }) assert.equal(audit.enabled, false) assert.deepEqual(audit.repos, []) const tests = await config.get({ automation: 'keep-tests-tight' }) assert.equal(tests.enabled, false) const reaper = await config.get({ automation: 'legacy-reaper' }) assert.equal(reaper.enabled, false) const listed = await config.listConfig() assert.equal(listed.automations['docs-freshness']?.enabled, true) assert.deepEqual(listed.automations['docs-freshness']?.repos, ['kody-hq/kody']) assert.equal(listed.automations['general-audit'], undefined) const raw = store().get(STORAGE_KEY) assert.equal(typeof raw, 'string') const parsed = JSON.parse(String(raw)) as { config: { automations: Record } } assert.deepEqual(Object.keys(parsed.config.automations), ['docs-freshness']) }) test('cleanup/config set rejects unknown keys and unknown automations', async () => { resetStore() await assert.rejects( () => config.set({ automation: 'docs-freshness', shadowMode: true } as never), /Unknown config key: shadowMode/, ) await assert.rejects(() => config.get({ automation: 'not-a-job' as never }), /Unknown automation/) })