Skip to content

Commit 58664f8

Browse files
committed
test(webapp): load service after pointing env at the redis container
1 parent 3a32635 commit 58664f8

1 file changed

Lines changed: 38 additions & 17 deletions

File tree

‎apps/webapp/test/pauseEnvironment.server.test.ts‎

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { postgresTest } from "@internal/testcontainers";
1+
import { containerTest } from "@internal/testcontainers";
22
import { EnvironmentPauseSource, type PrismaClient } from "@trigger.dev/database";
3+
import type { RedisOptions } from "ioredis";
34
import { describe, expect, vi } from "vitest";
45
import type { AuthenticatedEnvironment } from "~/services/apiAuth.server";
5-
import { authIncludeBase, toAuthenticated } from "~/models/runtimeEnvironment.server";
6-
import { PauseEnvironmentService } from "~/v3/services/pauseEnvironment.server";
76
import {
87
createRuntimeEnvironment,
98
createTestOrgProjectWithMember,
@@ -12,15 +11,34 @@ import {
1211

1312
vi.setConfig({ testTimeout: 60_000 });
1413

14+
// The service's import chain reaches module-level singletons that throw at load
15+
// time when REDIS_HOST/REDIS_PORT are unset (autoIncrementCounter via
16+
// triggerTaskV1), so the env must point at the redis container BEFORE the
17+
// module is imported. Hence dynamic imports; vitest runs each file in its own
18+
// fork, so the env mutation cannot leak into other suites.
19+
async function loadService(redisOptions: RedisOptions) {
20+
process.env.REDIS_HOST = redisOptions.host;
21+
process.env.REDIS_PORT = String(redisOptions.port);
22+
process.env.REDIS_TLS_DISABLED = "true";
23+
const [{ PauseEnvironmentService }, { authIncludeBase, toAuthenticated }] = await Promise.all([
24+
import("~/v3/services/pauseEnvironment.server"),
25+
import("~/models/runtimeEnvironment.server"),
26+
]);
27+
return { PauseEnvironmentService, authIncludeBase, toAuthenticated };
28+
}
29+
30+
type Loaded = Awaited<ReturnType<typeof loadService>>;
31+
1532
async function authEnv(
33+
loaded: Loaded,
1634
prisma: PrismaClient,
1735
environmentId: string
1836
): Promise<AuthenticatedEnvironment> {
1937
const row = await prisma.runtimeEnvironment.findFirstOrThrow({
2038
where: { id: environmentId },
21-
include: authIncludeBase,
39+
include: loaded.authIncludeBase,
2240
});
23-
return toAuthenticated(row);
41+
return loaded.toAuthenticated(row);
2442
}
2543

2644
async function seedProductionEnv(prisma: PrismaClient) {
@@ -35,12 +53,13 @@ async function seedProductionEnv(prisma: PrismaClient) {
3553
}
3654

3755
describe("PauseEnvironmentService", () => {
38-
postgresTest(
56+
containerTest(
3957
"resumes a manually paused env (pauseSource stays null through pause and resume)",
40-
async ({ prisma }) => {
58+
async ({ prisma, redisOptions }) => {
59+
const loaded = await loadService(redisOptions);
4160
const { environment } = await seedProductionEnv(prisma);
42-
const service = new PauseEnvironmentService(prisma);
43-
const env = await authEnv(prisma, environment.id);
61+
const service = new loaded.PauseEnvironmentService(prisma);
62+
const env = await authEnv(loaded, prisma, environment.id);
4463

4564
const paused = await service.call(env, "paused");
4665
expect(paused).toEqual({ success: true, state: "paused" });
@@ -64,17 +83,18 @@ describe("PauseEnvironmentService", () => {
6483
}
6584
);
6685

67-
postgresTest(
86+
containerTest(
6887
"rejects resume of a billing-limit paused env and leaves it paused",
69-
async ({ prisma }) => {
88+
async ({ prisma, redisOptions }) => {
89+
const loaded = await loadService(redisOptions);
7090
const { environment } = await seedProductionEnv(prisma);
7191
await prisma.runtimeEnvironment.update({
7292
where: { id: environment.id },
7393
data: { paused: true, pauseSource: EnvironmentPauseSource.BILLING_LIMIT },
7494
});
7595

76-
const service = new PauseEnvironmentService(prisma);
77-
const env = await authEnv(prisma, environment.id);
96+
const service = new loaded.PauseEnvironmentService(prisma);
97+
const env = await authEnv(loaded, prisma, environment.id);
7898

7999
const result = await service.call(env, "resumed");
80100
expect(result.success).toBe(false);
@@ -89,17 +109,18 @@ describe("PauseEnvironmentService", () => {
89109
}
90110
);
91111

92-
postgresTest(
112+
containerTest(
93113
"manual pause while billing-limit paused is a no-op that preserves pauseSource",
94-
async ({ prisma }) => {
114+
async ({ prisma, redisOptions }) => {
115+
const loaded = await loadService(redisOptions);
95116
const { environment } = await seedProductionEnv(prisma);
96117
await prisma.runtimeEnvironment.update({
97118
where: { id: environment.id },
98119
data: { paused: true, pauseSource: EnvironmentPauseSource.BILLING_LIMIT },
99120
});
100121

101-
const service = new PauseEnvironmentService(prisma);
102-
const env = await authEnv(prisma, environment.id);
122+
const service = new loaded.PauseEnvironmentService(prisma);
123+
const env = await authEnv(loaded, prisma, environment.id);
103124

104125
const result = await service.call(env, "paused");
105126
// Idempotent success without overwriting pauseSource, so billing-limit

0 commit comments

Comments
 (0)