Skip to content

Commit

Permalink
api: Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
victorges committed Apr 26, 2024
1 parent 86e61fa commit eec5d11
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
@@ -1,6 +1,8 @@
import serverPromise from "../test-server";
import { TestClient, clearDatabase } from "../test-helpers";
import { sleep } from "../util";
import { jobsDb } from "../store";
import { Webhook } from "../schema/types";

let server;
let mockAdminUser;
Expand Down Expand Up @@ -288,8 +290,22 @@ describe("controllers/webhook", () => {
});
expect(setActiveRes).toBeDefined();
expect(setActiveRes.status).toBe(204);
// const setActiveResJson = await setActiveRes.json()
// expect(setActiveResJson).toBeDefined()

let hooksCalled = 0;
for (let i = 0; i < 5 && hooksCalled < 2; i++) {
await sleep(500);

hooksCalled = 0;
for (const id of [webhookResJson.id, webhookResJson2.id]) {
const res = await client.get(`/webhook/${id}`);
expect(res.status).toBe(200);
const { status } = (await res.json()) as Webhook;
if (status.lastTriggeredAt >= now) {
hooksCalled++;
}
}
}
expect(hooksCalled).toBe(2);
}, 20000);

it("records webhook logs", async () => {
Expand Down
7 changes: 3 additions & 4 deletions packages/api/src/webhooks/cannon.test.ts
Expand Up @@ -286,8 +286,7 @@ describe("webhook cannon", () => {
expect(callCount).toBe(1);
expect(receivedEvent).toBe("stream.started");

// at this point the semaphore is re-acquired cause we just waited (acquired) above

sem = semaphore();
await server.queue.publishWebhook("events.stream.idle", {
type: "webhook_event",
id: "webhook_test_42",
Expand All @@ -302,6 +301,7 @@ describe("webhook cannon", () => {
expect(receivedEvent).toBe("stream.idle");

// does not receive some random event
sem = semaphore();
await server.queue.publishWebhook("events.stream.unknown" as any, {
type: "webhook_event",
id: "webhook_test_93",
Expand All @@ -311,8 +311,7 @@ describe("webhook cannon", () => {
userId: nonAdminUser.id,
});

const err = await sem.wait(1000).catch((err) => err);
expect(err?.message).toBe("timeout");
await sem.wait(1000);
expect(callCount).toBe(2);
});

Expand Down

0 comments on commit eec5d11

Please sign in to comment.