Track HiveGrid Cloud planning reference
This commit is contained in:
1 parent
b3d5ea6764
commit
e341c5938a
5784 files changed
+1104929
-3
No files matched your search
@@ -0,0 +1,458 @@
|
||||
import { describe, expect, test } from "vitest";
|
||||
import { loadRequiredWorkerConfig, loadWorkerConfig, WorkerConfigError } from "./index.js";
|
||||
|
||||
const REDIS_WITH_PASSWORD = "rediss://:super-secret-password@redis.example.com:6380/2";
|
||||
const DATABASE_URL = "postgresql://user:db-secret@localhost:5432/app";
|
||||
|
||||
describe("loadRequiredWorkerConfig", () => {
|
||||
test("returns disabled local worker config without Redis", () => {
|
||||
const config = loadRequiredWorkerConfig({});
|
||||
|
||||
expect(config).toMatchObject({
|
||||
appName: "ToolController",
|
||||
nodeEnv: "development",
|
||||
enabled: false,
|
||||
redis: {
|
||||
source: "none",
|
||||
configured: false,
|
||||
parseStatus: "not-provided",
|
||||
},
|
||||
queues: {
|
||||
email: "email",
|
||||
workflow: "workflow",
|
||||
},
|
||||
mailjet: {
|
||||
configured: false,
|
||||
},
|
||||
browserRuntime: {
|
||||
configured: false,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
test("returns enabled worker config with valid BULLMQ Redis", () => {
|
||||
const config = loadRequiredWorkerConfig({
|
||||
WORKER_ENABLED: "true",
|
||||
BULLMQ_REDIS_URL: REDIS_WITH_PASSWORD,
|
||||
DATABASE_URL,
|
||||
WORKER_QUEUE_GROUP: "general",
|
||||
});
|
||||
|
||||
expect(config.enabled).toBe(true);
|
||||
expect(config.redis).toMatchObject({
|
||||
source: "BULLMQ_REDIS_URL",
|
||||
url: REDIS_WITH_PASSWORD,
|
||||
configured: true,
|
||||
parseStatus: "valid",
|
||||
database: 2,
|
||||
});
|
||||
});
|
||||
|
||||
test("throws redacted WorkerConfigError when enabled worker is missing Redis", () => {
|
||||
expect(() => loadRequiredWorkerConfig({ WORKER_ENABLED: "true", DATABASE_URL, WORKER_QUEUE_GROUP: "general" })).toThrow(WorkerConfigError);
|
||||
|
||||
try {
|
||||
loadRequiredWorkerConfig({ WORKER_ENABLED: "true", DATABASE_URL, WORKER_QUEUE_GROUP: "general" });
|
||||
} catch (error) {
|
||||
expect(error).toBeInstanceOf(WorkerConfigError);
|
||||
const workerConfigError = error as WorkerConfigError;
|
||||
expect(workerConfigError.message).toBe("Invalid worker configuration (BULLMQ_REDIS_URL:missing-required).");
|
||||
expect(workerConfigError.details.diagnostics).toEqual([
|
||||
expect.objectContaining({
|
||||
code: "missing-required",
|
||||
name: "BULLMQ_REDIS_URL",
|
||||
message: "BULLMQ_REDIS_URL or REDIS_URL is required when WORKER_ENABLED is true.",
|
||||
}),
|
||||
]);
|
||||
expect(JSON.stringify({ message: workerConfigError.message, details: workerConfigError.details })).not.toContain(REDIS_WITH_PASSWORD);
|
||||
}
|
||||
});
|
||||
|
||||
test("throws redacted WorkerConfigError for invalid Redis URL values", () => {
|
||||
const cases = [
|
||||
{
|
||||
rawUrl: "redis://:super-secret-password@",
|
||||
message: "Invalid worker configuration (BULLMQ_REDIS_URL:invalid-redis-url).",
|
||||
code: "invalid-redis-url",
|
||||
},
|
||||
{
|
||||
rawUrl: "https://:super-secret-password@redis.example.com/0",
|
||||
message: "Invalid worker configuration (BULLMQ_REDIS_URL:invalid-redis-protocol).",
|
||||
code: "invalid-redis-protocol",
|
||||
},
|
||||
{
|
||||
rawUrl: "redis://:super-secret-password@redis.example.com/not-a-db",
|
||||
message: "Invalid worker configuration (BULLMQ_REDIS_URL:invalid-redis-database).",
|
||||
code: "invalid-redis-database",
|
||||
},
|
||||
] as const;
|
||||
|
||||
for (const invalidCase of cases) {
|
||||
try {
|
||||
loadRequiredWorkerConfig({
|
||||
WORKER_ENABLED: "true",
|
||||
BULLMQ_REDIS_URL: invalidCase.rawUrl,
|
||||
DATABASE_URL,
|
||||
WORKER_QUEUE_GROUP: "general",
|
||||
});
|
||||
throw new Error("Expected loadRequiredWorkerConfig to throw.");
|
||||
} catch (error) {
|
||||
expect(error).toBeInstanceOf(WorkerConfigError);
|
||||
const workerConfigError = error as WorkerConfigError;
|
||||
expect(workerConfigError.message).toBe(invalidCase.message);
|
||||
expect(workerConfigError.details.diagnostics).toContainEqual(expect.objectContaining({ code: invalidCase.code }));
|
||||
const errorText = JSON.stringify({ message: workerConfigError.message, details: workerConfigError.details });
|
||||
expect(errorText).not.toContain(invalidCase.rawUrl);
|
||||
expect(errorText).not.toContain("super-secret-password");
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("loadWorkerConfig", () => {
|
||||
test("keeps local default disabled without Redis", () => {
|
||||
const result = loadWorkerConfig({});
|
||||
|
||||
expect(result.ok).toBe(true);
|
||||
expect(result.config).toMatchObject({
|
||||
appName: "ToolController",
|
||||
nodeEnv: "development",
|
||||
enabled: false,
|
||||
concurrency: 1,
|
||||
redis: {
|
||||
source: "none",
|
||||
configured: false,
|
||||
parseStatus: "not-provided",
|
||||
},
|
||||
queues: {
|
||||
email: "email",
|
||||
workflow: "workflow",
|
||||
},
|
||||
mailjet: {
|
||||
configured: false,
|
||||
},
|
||||
browserRuntime: {
|
||||
configured: false,
|
||||
},
|
||||
});
|
||||
expect(result.diagnostics).toEqual([]);
|
||||
});
|
||||
|
||||
test("is ready with BULLMQ Redis, Mailjet, browser runtime and queue defaults", () => {
|
||||
const result = loadWorkerConfig({
|
||||
WORKER_ENABLED: "true",
|
||||
BULLMQ_REDIS_URL: REDIS_WITH_PASSWORD,
|
||||
DATABASE_URL,
|
||||
WORKER_QUEUE_GROUP: "general",
|
||||
REDIS_URL: "redis://fallback.example.com/0",
|
||||
MAILJET_API_KEY: " mailer-public-key ",
|
||||
MAILJET_SECRET_KEY: " mailer-secret-key ",
|
||||
MAILJET_FROM_EMAIL: " support@example.com ",
|
||||
MAILJET_FROM_NAME: " Example Support ",
|
||||
BROWSER_RUNTIME_URL: " https://browser-runtime.example.com ",
|
||||
BROWSER_RUNTIME_API_KEY: " browser-secret-key ",
|
||||
BROWSER_SESSION_TIMEOUT_MS: " 45000 ",
|
||||
WORKER_CONCURRENCY: " 4 ",
|
||||
});
|
||||
|
||||
expect(result.ok).toBe(true);
|
||||
expect(result.config.enabled).toBe(true);
|
||||
expect(result.config.concurrency).toBe(4);
|
||||
expect(result.config.redis).toMatchObject({
|
||||
source: "BULLMQ_REDIS_URL",
|
||||
url: REDIS_WITH_PASSWORD,
|
||||
configured: true,
|
||||
parseStatus: "valid",
|
||||
database: 2,
|
||||
});
|
||||
expect(result.config.mailjet).toEqual({
|
||||
configured: true,
|
||||
apiKey: "mailer-public-key",
|
||||
secretKey: "mailer-secret-key",
|
||||
fromEmail: "support@example.com",
|
||||
fromName: "Example Support",
|
||||
});
|
||||
expect(result.config.browserRuntime).toEqual({
|
||||
configured: true,
|
||||
url: "https://browser-runtime.example.com",
|
||||
apiKey: "browser-secret-key",
|
||||
sessionTimeoutMs: 45000,
|
||||
});
|
||||
expect(result.diagnostics).toEqual([]);
|
||||
});
|
||||
|
||||
test("falls back to REDIS_URL when BULLMQ_REDIS_URL is absent", () => {
|
||||
const result = loadWorkerConfig({
|
||||
WORKER_ENABLED: "true",
|
||||
REDIS_URL: "redis://localhost:6379/0",
|
||||
DATABASE_URL,
|
||||
WORKER_QUEUE_GROUP: "general",
|
||||
EMAIL_QUEUE_NAME: "mailers",
|
||||
WORKFLOW_QUEUE_NAME: "workflow-jobs",
|
||||
});
|
||||
|
||||
expect(result.ok).toBe(true);
|
||||
expect(result.config.redis).toMatchObject({
|
||||
source: "REDIS_URL",
|
||||
configured: true,
|
||||
parseStatus: "valid",
|
||||
database: 0,
|
||||
});
|
||||
expect(result.config.queues).toEqual({
|
||||
email: "mailers",
|
||||
workflow: "workflow-jobs",
|
||||
});
|
||||
});
|
||||
|
||||
test("reports enabled workers without Redis as missing required", () => {
|
||||
const result = loadWorkerConfig({ WORKER_ENABLED: "true", DATABASE_URL, WORKER_QUEUE_GROUP: "general" });
|
||||
|
||||
expect(result.ok).toBe(false);
|
||||
expect(result.config.enabled).toBe(true);
|
||||
expect(result.config.redis).toMatchObject({
|
||||
source: "none",
|
||||
configured: false,
|
||||
parseStatus: "not-provided",
|
||||
});
|
||||
expect(result.diagnostics).toContainEqual(
|
||||
expect.objectContaining({
|
||||
code: "missing-required",
|
||||
name: "BULLMQ_REDIS_URL",
|
||||
message: "BULLMQ_REDIS_URL or REDIS_URL is required when WORKER_ENABLED is true.",
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
test("reports enabled workers without DATABASE_URL as missing required", () => {
|
||||
const result = loadWorkerConfig({
|
||||
WORKER_ENABLED: "true",
|
||||
BULLMQ_REDIS_URL: REDIS_WITH_PASSWORD,
|
||||
WORKER_QUEUE_GROUP: "general",
|
||||
});
|
||||
|
||||
expect(result.ok).toBe(false);
|
||||
expect(result.config.enabled).toBe(true);
|
||||
expect(result.diagnostics).toContainEqual(
|
||||
expect.objectContaining({
|
||||
code: "missing-required",
|
||||
name: "DATABASE_URL",
|
||||
message: "DATABASE_URL is required when WORKER_ENABLED is true.",
|
||||
})
|
||||
);
|
||||
expect(JSON.stringify(result.diagnostics)).not.toContain("db-secret");
|
||||
});
|
||||
|
||||
test("reports enabled workers without WORKER_QUEUE_GROUP as missing required", () => {
|
||||
const result = loadWorkerConfig({
|
||||
WORKER_ENABLED: "true",
|
||||
BULLMQ_REDIS_URL: REDIS_WITH_PASSWORD,
|
||||
DATABASE_URL,
|
||||
});
|
||||
|
||||
expect(result.ok).toBe(false);
|
||||
expect(result.config.enabled).toBe(true);
|
||||
expect(result.diagnostics).toContainEqual(
|
||||
expect.objectContaining({
|
||||
code: "missing-required",
|
||||
name: "WORKER_QUEUE_GROUP",
|
||||
message: "WORKER_QUEUE_GROUP is required when WORKER_ENABLED is true.",
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
test("reports invalid WORKER_QUEUE_GROUP values", () => {
|
||||
const result = loadWorkerConfig({
|
||||
WORKER_ENABLED: "true",
|
||||
BULLMQ_REDIS_URL: REDIS_WITH_PASSWORD,
|
||||
DATABASE_URL,
|
||||
WORKER_QUEUE_GROUP: "all",
|
||||
});
|
||||
|
||||
expect(result.ok).toBe(false);
|
||||
expect(result.diagnostics).toContainEqual(
|
||||
expect.objectContaining({
|
||||
code: "invalid-worker-queue-group",
|
||||
name: "WORKER_QUEUE_GROUP",
|
||||
message: "WORKER_QUEUE_GROUP must be one of: general, mail, import, webhook, plugin.",
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
test("reports invalid Redis protocol without leaking URL values", () => {
|
||||
const rawUrl = "https://:super-secret-password@redis.example.com/0";
|
||||
const result = loadWorkerConfig({
|
||||
WORKER_ENABLED: "true",
|
||||
BULLMQ_REDIS_URL: rawUrl,
|
||||
DATABASE_URL,
|
||||
WORKER_QUEUE_GROUP: "general",
|
||||
});
|
||||
|
||||
expect(result.ok).toBe(false);
|
||||
expect(result.config.redis).toMatchObject({
|
||||
source: "BULLMQ_REDIS_URL",
|
||||
configured: false,
|
||||
parseStatus: "invalid-protocol",
|
||||
});
|
||||
expect(result.diagnostics).toContainEqual(expect.objectContaining({ code: "invalid-redis-protocol" }));
|
||||
expect(JSON.stringify(result.diagnostics)).not.toContain(rawUrl);
|
||||
expect(JSON.stringify(result.diagnostics)).not.toContain("super-secret-password");
|
||||
});
|
||||
|
||||
test("reports invalid REDIS_URL fallback diagnostics with the selected key name", () => {
|
||||
const result = loadWorkerConfig({
|
||||
WORKER_ENABLED: "true",
|
||||
REDIS_URL: "https://:super-secret-password@redis.example.com/0",
|
||||
DATABASE_URL,
|
||||
WORKER_QUEUE_GROUP: "general",
|
||||
});
|
||||
|
||||
expect(result.ok).toBe(false);
|
||||
expect(result.config.redis).toMatchObject({
|
||||
source: "REDIS_URL",
|
||||
configured: false,
|
||||
parseStatus: "invalid-protocol",
|
||||
});
|
||||
expect(result.diagnostics).toContainEqual(expect.objectContaining({ code: "invalid-redis-protocol", name: "REDIS_URL" }));
|
||||
expect(JSON.stringify(result.diagnostics)).not.toContain("super-secret-password");
|
||||
});
|
||||
|
||||
test("reports invalid browser URL and timeout without leaking provider values", () => {
|
||||
const rawBrowserUrl = "ftp://browser-secret-token@browser.example.com/session";
|
||||
const result = loadWorkerConfig({
|
||||
BROWSER_RUNTIME_URL: rawBrowserUrl,
|
||||
BROWSER_RUNTIME_API_KEY: "browser-api-secret",
|
||||
BROWSER_SESSION_TIMEOUT_MS: "not-a-timeout",
|
||||
});
|
||||
|
||||
expect(result.ok).toBe(false);
|
||||
expect(result.config.browserRuntime).toMatchObject({
|
||||
configured: false,
|
||||
apiKey: "browser-api-secret",
|
||||
});
|
||||
expect(result.diagnostics).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({ code: "invalid-url-protocol", name: "BROWSER_RUNTIME_URL" }),
|
||||
expect.objectContaining({ code: "invalid-timeout", name: "BROWSER_SESSION_TIMEOUT_MS" }),
|
||||
])
|
||||
);
|
||||
expect(JSON.stringify(result.diagnostics)).not.toContain(rawBrowserUrl);
|
||||
expect(JSON.stringify(result.diagnostics)).not.toContain("browser-secret-token");
|
||||
expect(JSON.stringify(result.diagnostics)).not.toContain("browser-api-secret");
|
||||
});
|
||||
|
||||
test("reports invalid worker concurrency values without leaking unrelated env values", () => {
|
||||
const cases = ["0", "-1", "1.5", "not-a-number", String(Number.MAX_SAFE_INTEGER + 1)] as const;
|
||||
|
||||
for (const rawConcurrency of cases) {
|
||||
const result = loadWorkerConfig({
|
||||
WORKER_ENABLED: "true",
|
||||
BULLMQ_REDIS_URL: REDIS_WITH_PASSWORD,
|
||||
DATABASE_URL,
|
||||
WORKER_QUEUE_GROUP: "general",
|
||||
WORKER_CONCURRENCY: rawConcurrency,
|
||||
BROWSER_RUNTIME_API_KEY: "browser-api-secret",
|
||||
});
|
||||
|
||||
expect(result.ok).toBe(false);
|
||||
expect(result.config.enabled).toBe(true);
|
||||
expect(result.config.concurrency).toBe(1);
|
||||
expect(result.diagnostics).toContainEqual(
|
||||
expect.objectContaining({
|
||||
code: "invalid-concurrency",
|
||||
name: "WORKER_CONCURRENCY",
|
||||
message: "WORKER_CONCURRENCY must be a positive safe integer.",
|
||||
})
|
||||
);
|
||||
expect(JSON.stringify(result.diagnostics)).not.toContain("browser-api-secret");
|
||||
}
|
||||
});
|
||||
|
||||
test("loadRequiredWorkerConfig blocks enabled startup with invalid worker concurrency", () => {
|
||||
expect(() =>
|
||||
loadRequiredWorkerConfig({
|
||||
WORKER_ENABLED: "true",
|
||||
BULLMQ_REDIS_URL: REDIS_WITH_PASSWORD,
|
||||
DATABASE_URL,
|
||||
WORKER_QUEUE_GROUP: "general",
|
||||
WORKER_CONCURRENCY: "0",
|
||||
})
|
||||
).toThrow(WorkerConfigError);
|
||||
|
||||
try {
|
||||
loadRequiredWorkerConfig({
|
||||
WORKER_ENABLED: "true",
|
||||
BULLMQ_REDIS_URL: REDIS_WITH_PASSWORD,
|
||||
DATABASE_URL,
|
||||
WORKER_QUEUE_GROUP: "general",
|
||||
WORKER_CONCURRENCY: "0",
|
||||
});
|
||||
} catch (error) {
|
||||
expect(error).toBeInstanceOf(WorkerConfigError);
|
||||
const workerConfigError = error as WorkerConfigError;
|
||||
expect(workerConfigError.message).toBe("Invalid worker configuration (WORKER_CONCURRENCY:invalid-concurrency).");
|
||||
expect(workerConfigError.details.diagnostics).toEqual([
|
||||
expect.objectContaining({
|
||||
code: "invalid-concurrency",
|
||||
name: "WORKER_CONCURRENCY",
|
||||
message: "WORKER_CONCURRENCY must be a positive safe integer.",
|
||||
}),
|
||||
]);
|
||||
expect(JSON.stringify({ message: workerConfigError.message, details: workerConfigError.details })).not.toContain(REDIS_WITH_PASSWORD);
|
||||
}
|
||||
});
|
||||
|
||||
test("reports partial Mailjet config as redacted missing provider fields", () => {
|
||||
const result = loadWorkerConfig({
|
||||
MAILJET_API_KEY: "mailer-public-key",
|
||||
});
|
||||
|
||||
expect(result.ok).toBe(false);
|
||||
expect(result.config.mailjet).toMatchObject({
|
||||
configured: false,
|
||||
apiKey: "mailer-public-key",
|
||||
});
|
||||
expect(result.diagnostics).toContainEqual(
|
||||
expect.objectContaining({
|
||||
code: "missing-required",
|
||||
name: "MAILJET_FROM_EMAIL",
|
||||
message: "MAILJET_FROM_EMAIL is required.",
|
||||
})
|
||||
);
|
||||
expect(result.diagnostics).toContainEqual(
|
||||
expect.objectContaining({
|
||||
code: "missing-required",
|
||||
name: "MAILJET_SECRET_KEY",
|
||||
message: "MAILJET_SECRET_KEY is required.",
|
||||
})
|
||||
);
|
||||
expect(JSON.stringify(result.diagnostics)).not.toContain("mailer-public-key");
|
||||
});
|
||||
|
||||
test("reports invalid Redis database index and invalid URL construction without throwing", () => {
|
||||
const invalidDatabase = loadWorkerConfig({
|
||||
WORKER_ENABLED: "true",
|
||||
BULLMQ_REDIS_URL: "redis://:super-secret-password@redis.example.com/not-a-db",
|
||||
DATABASE_URL,
|
||||
WORKER_QUEUE_GROUP: "general",
|
||||
});
|
||||
|
||||
expect(invalidDatabase.ok).toBe(false);
|
||||
expect(invalidDatabase.config.redis.parseStatus).toBe("invalid-database");
|
||||
expect(invalidDatabase.diagnostics).toContainEqual(expect.objectContaining({ code: "invalid-redis-database" }));
|
||||
|
||||
const invalidUrl = loadWorkerConfig({
|
||||
WORKER_ENABLED: "true",
|
||||
BULLMQ_REDIS_URL: "redis://:super-secret-password@",
|
||||
DATABASE_URL,
|
||||
WORKER_QUEUE_GROUP: "general",
|
||||
});
|
||||
|
||||
expect(invalidUrl.ok).toBe(false);
|
||||
expect(invalidUrl.config.redis.parseStatus).toBe("invalid-url");
|
||||
expect(invalidUrl.diagnostics).toContainEqual(expect.objectContaining({ code: "invalid-redis-url" }));
|
||||
|
||||
const diagnosticText = JSON.stringify([...invalidDatabase.diagnostics, ...invalidUrl.diagnostics]);
|
||||
expect(diagnosticText).not.toContain("redis://:super-secret-password@");
|
||||
expect(diagnosticText).not.toContain("super-secret-password");
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user