// The entry point's contract with core (MODULE_API.md §2.2). // // Slice 0 registers nothing, so there is very little behaviour to assert — and // the rules that DO apply are the ones that would otherwise be discovered on an // operator's install: registering synchronously, never awaiting, never touching // a database, never mutating what it was handed. Those hold for every slice // after this one too, which is why they are tested against the entry point // rather than against whatever it happens to register today. const test = require('node:test') const assert = require('node:assert') const register = require('../index') const { fakeCtx, fakeApi } = require('./_fakes') test('exports a single register function', () => { assert.strictEqual(typeof register, 'function') }) test('registers synchronously and returns nothing to await', () => { const result = register(fakeCtx(), fakeApi()) // Not `assert.strictEqual(result, undefined)` alone: a module that returned a // promise would be a module whose registration core silently never waits for. assert.ok(!result || typeof result.then !== 'function', 'register() must not return a thenable') }) test('touches no database at registration time', () => { const ctx = fakeCtx() register(ctx, fakeApi()) assert.deepStrictEqual(ctx.db.query.calls, [], 'register() queried the database') }) test('registers nothing in slice 0', () => { const api = fakeApi() register(fakeCtx(), api) assert.strictEqual(api.record.routes, null) assert.strictEqual(api.record.streams, null) assert.deepStrictEqual(api.record.extensions, []) assert.deepStrictEqual(api.record.legs, []) assert.deepStrictEqual(api.record.hooks, {}) }) test('takes a frozen ctx and does not try to write to it', () => { const ctx = fakeCtx() assert.ok(Object.isFrozen(ctx)) // Core freezes one level deep; a module that assigned to ctx would throw here // in strict mode and fail silently outside it. Either way it must not. assert.doesNotThrow(() => register(ctx, fakeApi())) }) test('logs through ctx.log, never through console', () => { const ctx = fakeCtx() register(ctx, fakeApi()) assert.strictEqual(ctx.logs.length, 1, 'expected exactly one logger to be taken') const { log } = ctx.logs[0] assert.strictEqual(log.info.calls.length, 1) assert.strictEqual(log.info.calls[0][0], 'registered') }) test('carries no hidden state between calls', () => { // Core calls register() exactly once, and the `once()` guard that enforces // that lives in core's `api` — not here. What this asserts is the module's // own half of it: registering into a second `api` produces the same result as // the first, so nothing is memoised at file scope where a re-register would // silently do less than it appears to. const first = fakeApi() const second = fakeApi() register(fakeCtx(), first) register(fakeCtx(), second) assert.deepStrictEqual(second.record, first.record) })