test: wait for Home Assistant API readiness

This commit is contained in:
ajnart committed 2026-08-13 19:51:44 +02:00
1 parent 7833cca86b
commit 6d3f7e3e36
1 file changed
+18 -14
@@ -41,6 +41,7 @@ describe("Home Assistant integration", () => {
test("Test connection should work", async () => { test("Test connection should work", async () => {
// Arrange // Arrange
const startedContainer = await prepareHomeAssistantContainerAsync(); const startedContainer = await prepareHomeAssistantContainerAsync();
try {
const homeAssistantIntegration = createHomeAssistantIntegration(startedContainer); const homeAssistantIntegration = createHomeAssistantIntegration(startedContainer);
// Act // Act
@@ -48,13 +49,14 @@ describe("Home Assistant integration", () => {
// Assert // Assert
expect(result.success).toBe(true); expect(result.success).toBe(true);
} finally {
// Cleanup
await startedContainer.stop(); await startedContainer.stop();
}, 30_000); // Timeout of 30 seconds }
}, 60_000);
test("Test connection should fail with wrong credentials", async () => { test("Test connection should fail with wrong credentials", async () => {
// Arrange // Arrange
const startedContainer = await prepareHomeAssistantContainerAsync(); const startedContainer = await prepareHomeAssistantContainerAsync();
try {
const homeAssistantIntegration = createHomeAssistantIntegration(startedContainer, "wrong-api-key"); const homeAssistantIntegration = createHomeAssistantIntegration(startedContainer, "wrong-api-key");
// Act // Act
@@ -66,10 +68,10 @@ describe("Home Assistant integration", () => {
expect(result.error).toBeInstanceOf(TestConnectionError); expect(result.error).toBeInstanceOf(TestConnectionError);
expect(result.error.type).toBe("authorization"); expect(result.error.type).toBe("authorization");
} finally {
// Cleanup
await startedContainer.stop(); await startedContainer.stop();
}, 30_000); // Timeout of 30 seconds }
}, 60_000);
}); });
const prepareHomeAssistantContainerAsync = async () => { const prepareHomeAssistantContainerAsync = async () => {
@@ -77,27 +79,29 @@ const prepareHomeAssistantContainerAsync = async () => {
const startedContainer = await homeAssistantContainer.start(); const startedContainer = await homeAssistantContainer.start();
await startedContainer.exec(["unzip", "-o", "/tmp/config.zip", "-d", "/config"]); await startedContainer.exec(["unzip", "-o", "/tmp/config.zip", "-d", "/config"]);
await startedContainer.restart(); await startedContainer.restart();
await waitForHomeAssistantAsync(startedContainer); await waitForHomeAssistantApiAsync(startedContainer);
return startedContainer; return startedContainer;
}; };
const waitForHomeAssistantAsync = async (container: StartedTestContainer) => { const waitForHomeAssistantApiAsync = async (container: StartedTestContainer) => {
const url = `http://${container.getHost()}:${container.getMappedPort(8123)}/api/config`; const url = `http://${container.getHost()}:${container.getMappedPort(8123)}/api/config`;
const deadline = Date.now() + 45_000;
for (let attempt = 0; attempt < 40; attempt++) { while (Date.now() < deadline) {
try { try {
const response = await fetch(url, { const response = await fetch(url, {
headers: { Authorization: `Bearer ${DEFAULT_API_KEY}` }, headers: { Authorization: `Bearer ${DEFAULT_API_KEY}` },
signal: AbortSignal.timeout(1_000),
}); });
if (response.ok) return; if (response.ok) return;
} catch { } catch {
// Home Assistant is still restarting. // Home Assistant closes connections while restarting. Retry until the deadline.
}
await new Promise((resolve) => setTimeout(resolve, 250));
} }
await new Promise((resolve) => setTimeout(resolve, 500)); await container.stop();
} throw new Error("Home Assistant API did not become ready after restart");
throw new Error("Home Assistant did not become ready after restart");
}; };
const createHomeAssistantContainer = () => { const createHomeAssistantContainer = () => {