All files / blong-gogo/src handler.name.test.ts

0% Statements 0/73
0% Branches 0/1
0% Functions 0/1
0% Lines 0/73

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74                                                                                                                                                   
/**
 * Test to verify that handlers get unique names matching their filenames
 */

import {handler} from '@feasibleone/blong/types';
import assert from 'node:assert';
import {describe, it} from 'node:test';

describe('Handler naming', () => {
    it('should preserve explicit handler names when they match expected name', () => {
        const namedHandler = handler(function myHandler({config: _config}) {
            return {send() {}};
        });

        assert.strictEqual(
            (namedHandler as {name: string}).name,
            'myHandler',
            'Named handler should preserve its name',
        );
    });

    it('should allow setting name on anonymous handlers', () => {
        const anonymousHandler = handler(({config: _config}) => {
            return {send() {}};
        });

        // Simulate what Watch.ts does when loading handlers
        Object.defineProperty(anonymousHandler, 'name', {
            value: 'send',
            configurable: true,
            enumerable: false,
        });

        assert.strictEqual(
            (anonymousHandler as {name: string}).name,
            'send',
            'Handler name should be settable',
        );
    });

    it('should ensure names are unique per file', () => {
        const handler1 = handler(({config: _config}) => ({send() {}}));
        const handler2 = handler(({config: _config}) => ({receive() {}}));

        // Simulate loading from different files
        Object.defineProperty(handler1, 'name', {value: 'send', configurable: true});
        Object.defineProperty(handler2, 'name', {value: 'receive', configurable: true});

        assert.strictEqual((handler1 as {name: string}).name, 'send');
        assert.strictEqual((handler2 as {name: string}).name, 'receive');
        assert.notStrictEqual(
            (handler1 as {name: string}).name,
            (handler2 as {name: string}).name,
            'Handlers should have unique names',
        );
    });

    it('should detect mismatch between handler name and expected name', () => {
        // This test verifies that Watch.ts would throw an error for mismatched names
        const namedHandler = handler(function wrongName({config: _config}) {
            return {send() {}};
        });

        // In Watch.ts, this would trigger an error like:
        // "Handler name mismatch in 'send.ts': function is named 'wrongName' but file is named 'send.ts'"

        const actualName = (namedHandler as {name: string}).name;
        const expectedName = 'send';

        assert.notStrictEqual(actualName, expectedName, 'Mismatch should be detectable');
        assert.strictEqual(actualName, 'wrongName', 'Handler should preserve explicit name');
    });
});