agenthub/tests/mdns.test.ts

44 lines
1.3 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest';
import { startMdnsAdvertise } from '../src/server/mdns.js';
import type { Bonjour, Service } from 'bonjour-service';
describe('mDNS advertise', () => {
it('republishes after an IP change and stops the old service first', async () => {
let ip = '192.168.1.10';
const calls: string[] = [];
const published: Array<{ txt?: Record<string, string> }> = [];
const createBonjour = () =>
({
publish(opts: { txt?: Record<string, string> }) {
calls.push(`publish:${opts.txt?.address ?? ''}`);
published.push(opts);
return {
on: vi.fn(),
stop: vi.fn(() => calls.push('stop')),
} as unknown as Service;
},
destroy: vi.fn(() => calls.push('destroy')),
}) as unknown as Bonjour;
const handle = startMdnsAdvertise({
port: 3377,
pollMs: 25,
getIp: () => ip,
createBonjour,
});
expect(handle).toBeDefined();
expect(published[0]?.txt?.address).toBe('192.168.1.10');
ip = '192.168.1.44';
await new Promise((resolve) => setTimeout(resolve, 70));
expect(published).toHaveLength(2);
expect(published[1]?.txt?.address).toBe('192.168.1.44');
expect(calls).toEqual(['publish:192.168.1.10', 'stop', 'destroy', 'publish:192.168.1.44']);
handle?.stop();
});
});