| 1 | import { test, before } from 'node:test' |
| 2 | import assert from 'node:assert/strict' |
| 3 | import { mkdtempSync, rmSync } from 'node:fs' |
| 4 | import { join, dirname } from 'node:path' |
| 5 | import { fileURLToPath, pathToFileURL } from 'node:url' |
| 6 | import { build } from 'esbuild' |
| 7 | |
| 8 | const here = dirname(fileURLToPath(import.meta.url)) |
| 9 | let settingsAdapter |
| 10 | let SettingsConflictError |
| 11 | |
| 12 | before(async () => { |
| 13 | const dir = mkdtempSync(join(here, '..', 'node_modules', '.cw-settings-')) |
| 14 | const outfile = join(dir, 'settings.mjs') |
| 15 | await build({ entryPoints: [join(here, '../src/dsh/settings.ts')], outfile, bundle: true, platform: 'node', format: 'esm', packages: 'external', absWorkingDir: join(here, '..'), logLevel: 'silent' }) |
| 16 | ;({ settingsAdapter, SettingsConflictError } = await import(pathToFileURL(outfile).href)) |
| 17 | process.on('exit', () => rmSync(dir, { recursive: true, force: true })) |
| 18 | }) |
| 19 | |
| 20 | function service(answers, pluginName = 'fixture.plugin', { ticketed = true } = {}) { |
| 21 | const calls = [] |
| 22 | const host = { |
| 23 | tier: 'plugin', |
| 24 | ownerOf: () => ({ pluginName }), |
| 25 | brokerTicket: (ctx, name) => { |
| 26 | if (!ticketed) throw new Error(`the ${name} broker is only available to a live tool invocation the core gave a ticket for it`) |
| 27 | return { invocation: {}, ticket: 'ticket-1' } |
| 28 | }, |
| 29 | broker: async (ctx, name, method, input) => { |
| 30 | calls.push({ name, method, input }) |
| 31 | const answer = answers[method] |
| 32 | if (answer instanceof Error) throw answer |
| 33 | return answer |
| 34 | }, |
| 35 | } |
| 36 | const instance = Object.create(settingsAdapter.define(host).prototype) |
| 37 | instance.ctx = {} |
| 38 | return { settings: instance, calls } |
| 39 | } |
| 40 | |
| 41 | const row = { ns: 'fixture.plugin', writable: true, roots: ['model'], value: { model: 'small' }, revision: 'r1', applied: true, applies: 'reload' } |
| 42 | |
| 43 | test('settings read and edit go to the settings broker as typed methods', async () => { |
| 44 | const { settings, calls } = service({ |
| 45 | describe: row, |
| 46 | update: { written: true, conflict: false, revision: 'r2', applies: 'reload' }, |
| 47 | replace: { written: true, conflict: false, revision: 'r3', applies: 'reload' }, |
| 48 | mutate: { written: false, conflict: false, revision: 'r3', applies: 'reload' }, |
| 49 | }) |
| 50 | const described = await settings.describe() |
| 51 | assert.deepEqual(described[0], row) |
| 52 | assert.equal(described[1].ns, 'locale') |
| 53 | assert.equal(described[1].writable, false) |
| 54 | assert.equal(typeof described[1].value.preference, 'string') |
| 55 | assert.deepEqual(await settings.update('fixture.plugin', { model: 'large' }, 'r1'), { written: true, revision: 'r2', applies: 'reload' }) |
| 56 | await settings.replace('fixture.plugin', { model: 'tiny' }) |
| 57 | assert.equal((await settings.mutate('fixture.plugin', [{ op: 'unset', path: ['model'] }])).written, false) |
| 58 | assert.deepEqual(calls, [ |
| 59 | { name: 'settings', method: 'describe', input: {} }, |
| 60 | { name: 'settings', method: 'update', input: { patch: { model: 'large' }, expected_revision: 'r1' } }, |
| 61 | { name: 'settings', method: 'replace', input: { value: { model: 'tiny' } } }, |
| 62 | { name: 'settings', method: 'mutate', input: { ops: [{ op: 'unset', path: ['model'] }] } }, |
| 63 | ]) |
| 64 | }) |
| 65 | |
| 66 | test('another namespace, a malformed edit and a malformed answer never become a broker write', async () => { |
| 67 | const { settings, calls } = service({ describe: { ns: 'fixture.plugin' }, update: { written: 'yes' } }) |
| 68 | await assert.rejects(settings.update('other.plugin', { model: 'x' }), /only its own settings namespace/) |
| 69 | await assert.rejects(settings.update('', { model: 'x' }), /non-empty string/) |
| 70 | await assert.rejects(settings.update('fixture.plugin', ['model']), /plain object/) |
| 71 | await assert.rejects(settings.replace('fixture.plugin', null), /plain object/) |
| 72 | await assert.rejects(settings.mutate('fixture.plugin', { op: 'set' }), /array of path operations/) |
| 73 | await assert.rejects(settings.update('fixture.plugin', { model: 'x' }, 7), /expected revision/) |
| 74 | assert.deepEqual(calls, []) |
| 75 | await assert.rejects(settings.describe(), /invalid settings broker response/) |
| 76 | await assert.rejects(settings.update('fixture.plugin', { model: 'x' }), /invalid settings broker response/) |
| 77 | }) |
| 78 | |
| 79 | test('a stale edit is a conflict and a broker refusal reaches the plugin as the refusal', async () => { |
| 80 | const stale = service({ update: { written: false, conflict: true, revision: 'r9', applies: 'reload' } }) |
| 81 | await assert.rejects(stale.settings.update('fixture.plugin', { model: 'x' }, 'r1'), (error) => { |
| 82 | assert.ok(error instanceof SettingsConflictError) |
| 83 | assert.equal(error.code, 'SETTINGS_CONFLICT') |
| 84 | assert.equal(error.expected, 'r1') |
| 85 | assert.equal(error.actual, 'r9') |
| 86 | return true |
| 87 | }) |
| 88 | const refused = service({ update: new Error('that setting is outside this plugin\'s reviewed settings'), describe: new Error('the settings broker is not available yet') }) |
| 89 | await assert.rejects(refused.settings.update('fixture.plugin', { internal: 1 }), /outside this plugin's reviewed settings/) |
| 90 | await assert.rejects(refused.settings.describe(), /not available yet/) |
| 91 | }) |
| 92 | |
| 93 | test('describe outside a ticketed tool invocation returns read-only host locale without any broker call', () => { |
| 94 | const { settings, calls } = service({ describe: row }, 'fixture.plugin', { ticketed: false }) |
| 95 | const forms = settings.describe() |
| 96 | assert.ok(Array.isArray(forms)) |
| 97 | assert.equal(forms.length, 1) |
| 98 | assert.equal(forms[0].ns, 'locale') |
| 99 | assert.equal(forms[0].writable, false) |
| 100 | assert.equal(typeof forms[0].value.preference, 'string') |
| 101 | assert.ok(forms[0].value.preference.length > 0) |
| 102 | assert.equal(settings.locale.preference, forms[0].value.preference) |
| 103 | assert.deepEqual(calls, []) |
| 104 | }) |
| 105 |