import { it, beforeEach, afterEach, mock } from 'node:test' import assert from 'node:assert/strict' import { isDeepStrictEqual } from 'node:util' import { console } from 'openspan' import { Connector, type Locator, type Receiver } from '@toa.io/core' import type { Message } from '@toa.io/core/types' import { timeout } from '@toa.io/generic' import { Client, type Fetched } from './Client.ts' import { EVENT } from '@toa.io/definitions/extensions.configuration' import type { Host } from './Factory.ts' class Remote extends Connector { public readonly invoke = mock.fn( async ( _endpoint: string, request: { input: Array<{ component: string; epoch: string }> } ) => request.input.map((pair): Fetched => { const configuration = this.values[pair.component] ?? null const fetched: Fetched = { ...pair, configuration, created: configuration === null ? 0 : (this.created[pair.component] ?? 7) } if (pair.component in this.revisions) fetched.revision = this.revisions[pair.component] return fetched }) ) public values: Record = {} /** `0` serves the values as deployed defaults. */ public created: Record = {} /** Absent for a component, the answer carries no revision, as an older service's does not. */ public revisions: Record = {} } let remote: Remote let receiver: Receiver | null let host: Host let client: Client beforeEach(() => { remote = new Remote() receiver = null host = { remote: mock.fn(async (locator: Locator) => { assert.deepStrictEqual(locator.id, 'configuration.values') return remote }), receive: mock.fn(async (label: string, consumer: Receiver) => { assert.deepStrictEqual(label, EVENT) receiver = consumer return new Connector() }) } as unknown as Host client = new Client(host, { base: 10, max: 20, warn: 2 }) }) afterEach(async () => { await client.disconnect() }) it('should send the requests of one tick as one call', async () => { remote.values = { 'a.one': { foo: 1 }, 'a.two': { foo: 2 } } await client.connect() const [one, two] = await Promise.all([ client.fetch('a.one', 'e1'), client.fetch('a.two', 'e2') ]) assert.deepStrictEqual(one, { configuration: { foo: 1 }, created: 7, revision: null }) assert.deepStrictEqual(two, { configuration: { foo: 2 }, created: 7, revision: null }) assert.strictEqual(remote.invoke.mock.callCount(), 1) assert.deepStrictEqual(remote.invoke.mock.calls[0].arguments[1], { input: [ { component: 'a.one', epoch: 'e1' }, { component: 'a.two', epoch: 'e2' } ] }) }) it('should keep asking until served', async () => { await client.connect() const fetching = client.fetch('a.one', 'e1') const deadline = Date.now() + 1000 while (remote.invoke.mock.calls.length < 2 && Date.now() < deadline) await timeout(5) assert.ok(remote.invoke.mock.calls.length >= 2) remote.values = { 'a.one': { foo: 1 } } assert.deepStrictEqual((await fetching).configuration, { foo: 1 }) }) it('should hand a created object to its subscribers', async () => { await client.connect() const listener = mock.fn() const other = mock.fn() client.subscribe('a.one', 'e1', listener) client.subscribe('a.one', 'e0', other) await receiver!.receive({ payload: { component: 'a.one', epoch: 'e1', configuration: { foo: 2 }, CREATED: 12 } } satisfies Message) assert.ok( listener.mock.calls.some( (call: any) => call.arguments.length === 1 && isDeepStrictEqual(call.arguments[0], { configuration: { foo: 2 }, created: 12, revision: null }) ) ) assert.strictEqual(other.mock.callCount(), 0) assert.strictEqual(remote.invoke.mock.callCount(), 0) // a reset holds deployed defaults, and says of which revision await receiver!.receive({ payload: { component: 'a.one', epoch: 'e1', configuration: { foo: 'deployed' }, revision: 'r1', CREATED: 13 } } satisfies Message) assert.deepStrictEqual(listener.mock.calls.at(-1)?.arguments[0], { configuration: { foo: 'deployed' }, created: 13, revision: 'r1' }) client.unsubscribe('a.one', 'e1', listener) await receiver!.receive({ payload: { component: 'a.one', epoch: 'e1', configuration: { foo: 3 }, CREATED: 14 } } satisfies Message) assert.strictEqual(listener.mock.callCount(), 2) }) it('should refuse deployed defaults of another revision until served its own', async () => { const warn = mock.method(console, 'warn', () => undefined) try { remote.values = { 'a.one': { foo: 'previous' } } remote.created = { 'a.one': 0 } remote.revisions = { 'a.one': 'r0' } await client.connect() const fetching = client.fetch('a.one', 'e1', 'r1') await rounds(3) remote.values = { 'a.one': { foo: 'deployed' } } remote.revisions = { 'a.one': 'r1' } assert.deepStrictEqual(await fetching, { configuration: { foo: 'deployed' }, created: 0, revision: 'r1' }) const refusals = warn.mock.calls.filter( (call) => call.arguments[0] === 'Configuration of another revision refused' ) assert.ok(refusals.length >= 1) assert.deepStrictEqual(refusals[0].arguments[1], { component: 'a.one', epoch: 'e1', expected: 'r1', received: 'r0' }) assert.ok( !warn.mock.calls.some((call) => call.arguments[0] === 'Waiting for configuration'), 'a refused component is reported as refused, not as waiting' ) } finally { warn.mock.restore() } }) it('should report a refusal on the first and every n-th time', async () => { const warn = mock.method(console, 'warn', () => undefined) try { remote.values = { 'a.one': { foo: 'previous' } } remote.created = { 'a.one': 0 } remote.revisions = { 'a.one': 'r0' } await client.connect() const fetching = client.fetch('a.one', 'e1', 'r1') await rounds(5) const answered = remote.invoke.mock.callCount() remote.revisions = { 'a.one': 'r1' } await fetching const refused = warn.mock.calls.filter( (call) => call.arguments[0] === 'Configuration of another revision refused' ).length // warn is 2 here: the first, the third, the fifth... assert.ok(answered >= 5) assert.ok( refused >= Math.floor(answered / 2) && refused <= Math.ceil(answered / 2) + 1 ) } finally { warn.mock.restore() } }) it('should refuse deployed defaults an older service answers without a revision', async () => { const warn = mock.method(console, 'warn', () => undefined) try { remote.values = { 'a.one': { foo: 'previous' } } remote.created = { 'a.one': 0 } await client.connect() const fetching = client.fetch('a.one', 'e1', 'r1') await rounds(2) const refusal = warn.mock.calls.find( (call) => call.arguments[0] === 'Configuration of another revision refused' ) assert.deepStrictEqual(refusal?.arguments[1], { component: 'a.one', epoch: 'e1', expected: 'r1', received: null }) remote.revisions = { 'a.one': 'r1' } await fetching } finally { warn.mock.restore() } }) it('should take a created object whatever the revision', async () => { remote.values = { 'a.one': { foo: 'created' } } remote.revisions = { 'a.one': null } await client.connect() assert.deepStrictEqual(await client.fetch('a.one', 'e1', 'r1'), { configuration: { foo: 'created' }, created: 7, revision: null }) }) it('should refuse deployed defaults a reset brought back of another revision', async () => { const warn = mock.method(console, 'warn', () => undefined) try { remote.values = { 'a.one': { foo: 'previous' } } remote.created = { 'a.one': 9 } remote.revisions = { 'a.one': 'r0' } await client.connect() const fetching = client.fetch('a.one', 'e1', 'r1') await rounds(2) assert.ok( warn.mock.calls.some( (call) => call.arguments[0] === 'Configuration of another revision refused' ) ) remote.values = { 'a.one': { foo: 'deployed' } } remote.revisions = { 'a.one': 'r1' } assert.deepStrictEqual(await fetching, { configuration: { foo: 'deployed' }, created: 9, revision: 'r1' }) } finally { warn.mock.restore() } }) it('should take deployed defaults of any revision when asked without one', async () => { remote.values = { 'a.one': { foo: 'served' } } remote.created = { 'a.one': 0 } remote.revisions = { 'a.one': 'r0' } await client.connect() assert.deepStrictEqual(await client.fetch('a.one', 'e1'), { configuration: { foo: 'served' }, created: 0, revision: 'r0' }) }) /** Until the remote has been asked as many times more. */ async function rounds(count: number): Promise { const target = remote.invoke.mock.callCount() + count const deadline = Date.now() + 2000 while (remote.invoke.mock.callCount() < target && Date.now() < deadline) await timeout(5) assert.ok(remote.invoke.mock.callCount() >= target, `not asked ${count} more times`) }