perf(test): remove fixed sleeps in async test flows

This commit is contained in:
Peter Steinberger
2026-02-18 17:20:35 +00:00
parent 00e32cf04a
commit 0d25b6a317
3 changed files with 24 additions and 40 deletions

View File

@@ -15,6 +15,14 @@ const getLoggerInfo = vi.fn();
const asString = (value: unknown, fallback: string) =>
typeof value === "string" && value.trim() ? value : fallback;
function enableAdvertiserUnitMode(hostname = "test-host") {
// Allow advertiser to run in unit tests.
delete process.env.VITEST;
process.env.NODE_ENV = "development";
vi.spyOn(os, "hostname").mockReturnValue(hostname);
process.env.OPENCLAW_MDNS_HOSTNAME = hostname;
}
function mockCiaoService(params?: {
advertise?: ReturnType<typeof vi.fn>;
destroy?: ReturnType<typeof vi.fn>;
@@ -105,19 +113,14 @@ describe("gateway bonjour advertiser", () => {
});
it("does not block on advertise and publishes expected txt keys", async () => {
// Allow advertiser to run in unit tests.
delete process.env.VITEST;
process.env.NODE_ENV = "development";
vi.spyOn(os, "hostname").mockReturnValue("test-host");
process.env.OPENCLAW_MDNS_HOSTNAME = "test-host";
process.env.OPENCLAW_MDNS_HOSTNAME = "test-host";
enableAdvertiserUnitMode();
const destroy = vi.fn().mockResolvedValue(undefined);
let resolveAdvertise = () => {};
const advertise = vi.fn().mockImplementation(
async () =>
await new Promise<void>((resolve) => {
setTimeout(resolve, 250);
resolveAdvertise = resolve;
}),
);
mockCiaoService({ advertise, destroy });
@@ -147,6 +150,8 @@ describe("gateway bonjour advertiser", () => {
// We don't await `advertise()`, but it should still be called for each service.
expect(advertise).toHaveBeenCalledTimes(1);
resolveAdvertise();
await Promise.resolve();
await started.stop();
expect(destroy).toHaveBeenCalledTimes(1);
@@ -154,11 +159,7 @@ describe("gateway bonjour advertiser", () => {
});
it("omits cliPath and sshPort in minimal mode", async () => {
// Allow advertiser to run in unit tests.
delete process.env.VITEST;
process.env.NODE_ENV = "development";
vi.spyOn(os, "hostname").mockReturnValue("test-host");
enableAdvertiserUnitMode();
const destroy = vi.fn().mockResolvedValue(undefined);
const advertise = vi.fn().mockResolvedValue(undefined);
@@ -179,12 +180,7 @@ describe("gateway bonjour advertiser", () => {
});
it("attaches conflict listeners for services", async () => {
// Allow advertiser to run in unit tests.
delete process.env.VITEST;
process.env.NODE_ENV = "development";
vi.spyOn(os, "hostname").mockReturnValue("test-host");
process.env.OPENCLAW_MDNS_HOSTNAME = "test-host";
enableAdvertiserUnitMode();
const destroy = vi.fn().mockResolvedValue(undefined);
const advertise = vi.fn().mockResolvedValue(undefined);
@@ -207,12 +203,7 @@ describe("gateway bonjour advertiser", () => {
});
it("cleans up unhandled rejection handler after shutdown", async () => {
// Allow advertiser to run in unit tests.
delete process.env.VITEST;
process.env.NODE_ENV = "development";
vi.spyOn(os, "hostname").mockReturnValue("test-host");
process.env.OPENCLAW_MDNS_HOSTNAME = "test-host";
enableAdvertiserUnitMode();
const destroy = vi.fn().mockResolvedValue(undefined);
const advertise = vi.fn().mockResolvedValue(undefined);
@@ -240,13 +231,8 @@ describe("gateway bonjour advertiser", () => {
});
it("logs advertise failures and retries via watchdog", async () => {
// Allow advertiser to run in unit tests.
delete process.env.VITEST;
process.env.NODE_ENV = "development";
enableAdvertiserUnitMode();
vi.useFakeTimers();
vi.spyOn(os, "hostname").mockReturnValue("test-host");
process.env.OPENCLAW_MDNS_HOSTNAME = "test-host";
const destroy = vi.fn().mockResolvedValue(undefined);
const advertise = vi
@@ -278,12 +264,7 @@ describe("gateway bonjour advertiser", () => {
});
it("handles advertise throwing synchronously", async () => {
// Allow advertiser to run in unit tests.
delete process.env.VITEST;
process.env.NODE_ENV = "development";
vi.spyOn(os, "hostname").mockReturnValue("test-host");
process.env.OPENCLAW_MDNS_HOSTNAME = "test-host";
enableAdvertiserUnitMode();
const destroy = vi.fn().mockResolvedValue(undefined);
const advertise = vi.fn(() => {