|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +vi.mock("~/services/logger.server", () => ({ |
| 4 | + logger: { |
| 5 | + debug: vi.fn(), |
| 6 | + info: vi.fn(), |
| 7 | + warn: vi.fn(), |
| 8 | + error: vi.fn(), |
| 9 | + }, |
| 10 | +})); |
| 11 | + |
| 12 | +import { SkipIfActiveConcern } from "../../app/runEngine/concerns/skipIfActive.server"; |
| 13 | +import type { TriggerTaskRequest } from "../../app/runEngine/types"; |
| 14 | + |
| 15 | +type MockPrisma = { |
| 16 | + $queryRaw: ReturnType<typeof vi.fn>; |
| 17 | + taskRun: { findUnique: ReturnType<typeof vi.fn> }; |
| 18 | +}; |
| 19 | + |
| 20 | +function buildRequest(overrides: { |
| 21 | + skipIfActive?: boolean; |
| 22 | + tags?: string | string[]; |
| 23 | + taskId?: string; |
| 24 | + environmentId?: string; |
| 25 | +}): TriggerTaskRequest { |
| 26 | + return { |
| 27 | + taskId: overrides.taskId ?? "ezderm-notes-fetch", |
| 28 | + friendlyId: "run_test", |
| 29 | + environment: { |
| 30 | + id: overrides.environmentId ?? "env_123", |
| 31 | + organizationId: "org_1", |
| 32 | + projectId: "proj_1", |
| 33 | + } as TriggerTaskRequest["environment"], |
| 34 | + body: { |
| 35 | + payload: {}, |
| 36 | + options: { |
| 37 | + skipIfActive: overrides.skipIfActive, |
| 38 | + tags: overrides.tags, |
| 39 | + }, |
| 40 | + }, |
| 41 | + options: {}, |
| 42 | + } as TriggerTaskRequest; |
| 43 | +} |
| 44 | + |
| 45 | +function mockPrisma(initial?: { |
| 46 | + existing?: Array<{ id: string }>; |
| 47 | + run?: { id: string } | null; |
| 48 | +}): MockPrisma { |
| 49 | + return { |
| 50 | + $queryRaw: vi.fn().mockResolvedValue(initial?.existing ?? []), |
| 51 | + taskRun: { findUnique: vi.fn().mockResolvedValue(initial?.run ?? null) }, |
| 52 | + }; |
| 53 | +} |
| 54 | + |
| 55 | +describe("SkipIfActiveConcern", () => { |
| 56 | + it("returns wasSkipped=false when the flag is not set", async () => { |
| 57 | + const prisma = mockPrisma(); |
| 58 | + const concern = new SkipIfActiveConcern(prisma as never); |
| 59 | + |
| 60 | + const result = await concern.handleTriggerRequest( |
| 61 | + buildRequest({ skipIfActive: undefined, tags: ["connector:abc"] }) |
| 62 | + ); |
| 63 | + |
| 64 | + expect(result).toEqual({ wasSkipped: false }); |
| 65 | + expect(prisma.$queryRaw).not.toHaveBeenCalled(); |
| 66 | + }); |
| 67 | + |
| 68 | + it("returns wasSkipped=false when skipIfActive=false", async () => { |
| 69 | + const prisma = mockPrisma(); |
| 70 | + const concern = new SkipIfActiveConcern(prisma as never); |
| 71 | + |
| 72 | + const result = await concern.handleTriggerRequest( |
| 73 | + buildRequest({ skipIfActive: false, tags: ["connector:abc"] }) |
| 74 | + ); |
| 75 | + |
| 76 | + expect(result).toEqual({ wasSkipped: false }); |
| 77 | + expect(prisma.$queryRaw).not.toHaveBeenCalled(); |
| 78 | + }); |
| 79 | + |
| 80 | + it("returns wasSkipped=false when no tags are supplied (tag scope required)", async () => { |
| 81 | + const prisma = mockPrisma(); |
| 82 | + const concern = new SkipIfActiveConcern(prisma as never); |
| 83 | + |
| 84 | + const result = await concern.handleTriggerRequest( |
| 85 | + buildRequest({ skipIfActive: true, tags: undefined }) |
| 86 | + ); |
| 87 | + |
| 88 | + expect(result).toEqual({ wasSkipped: false }); |
| 89 | + expect(prisma.$queryRaw).not.toHaveBeenCalled(); |
| 90 | + }); |
| 91 | + |
| 92 | + it("returns wasSkipped=false when no active run matches", async () => { |
| 93 | + const prisma = mockPrisma({ existing: [] }); |
| 94 | + const concern = new SkipIfActiveConcern(prisma as never); |
| 95 | + |
| 96 | + const result = await concern.handleTriggerRequest( |
| 97 | + buildRequest({ skipIfActive: true, tags: ["connector:abc"] }) |
| 98 | + ); |
| 99 | + |
| 100 | + expect(result).toEqual({ wasSkipped: false }); |
| 101 | + expect(prisma.$queryRaw).toHaveBeenCalledTimes(1); |
| 102 | + expect(prisma.taskRun.findUnique).not.toHaveBeenCalled(); |
| 103 | + }); |
| 104 | + |
| 105 | + it("returns wasSkipped=true with the existing run when an active run matches all tags", async () => { |
| 106 | + const existingRun = { id: "run_existing", status: "EXECUTING", runTags: ["connector:abc"] }; |
| 107 | + const prisma = mockPrisma({ existing: [{ id: existingRun.id }], run: existingRun }); |
| 108 | + const concern = new SkipIfActiveConcern(prisma as never); |
| 109 | + |
| 110 | + const result = await concern.handleTriggerRequest( |
| 111 | + buildRequest({ skipIfActive: true, tags: ["connector:abc"] }) |
| 112 | + ); |
| 113 | + |
| 114 | + expect(result).toMatchObject({ wasSkipped: true, run: existingRun }); |
| 115 | + expect(prisma.$queryRaw).toHaveBeenCalledTimes(1); |
| 116 | + expect(prisma.taskRun.findUnique).toHaveBeenCalledWith({ where: { id: "run_existing" } }); |
| 117 | + }); |
| 118 | + |
| 119 | + it("normalizes a single tag string into an array", async () => { |
| 120 | + const prisma = mockPrisma({ |
| 121 | + existing: [{ id: "run_x" }], |
| 122 | + run: { id: "run_x", status: "PENDING", runTags: ["connector:abc"] }, |
| 123 | + }); |
| 124 | + const concern = new SkipIfActiveConcern(prisma as never); |
| 125 | + |
| 126 | + const result = await concern.handleTriggerRequest( |
| 127 | + // @ts-expect-error Zod allows both string and string[] for tags |
| 128 | + buildRequest({ skipIfActive: true, tags: "connector:abc" }) |
| 129 | + ); |
| 130 | + |
| 131 | + expect(result.wasSkipped).toBe(true); |
| 132 | + }); |
| 133 | + |
| 134 | + it("recovers gracefully when the row disappears between the probe and the fetch", async () => { |
| 135 | + const prisma = mockPrisma({ existing: [{ id: "run_gone" }], run: null }); |
| 136 | + const concern = new SkipIfActiveConcern(prisma as never); |
| 137 | + |
| 138 | + const result = await concern.handleTriggerRequest( |
| 139 | + buildRequest({ skipIfActive: true, tags: ["connector:abc"] }) |
| 140 | + ); |
| 141 | + |
| 142 | + expect(result).toEqual({ wasSkipped: false }); |
| 143 | + }); |
| 144 | +}); |
0 commit comments