Update hivegrid-cloud reference to latest main
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e98d38e716
commit
7b21524c86
3781 files changed
+373484
-225607
No files matched your search
@@ -109,4 +109,18 @@ describe("resolveRedisConnectionOptions", () => {
|
||||
expect(invalidDatabaseError).toMatchObject({ message: "BULLMQ_REDIS_URL database index must be a non-negative integer." });
|
||||
expectRedacted(invalidDatabaseError);
|
||||
});
|
||||
|
||||
test("defaults the port to 6379 and leaves db/tls unset when the URL omits them", () => {
|
||||
const parsed = parseRedisUrl("redis://redis.internal");
|
||||
|
||||
expect(parsed).toMatchObject({
|
||||
host: "redis.internal",
|
||||
port: 6379,
|
||||
maxRetriesPerRequest: null,
|
||||
});
|
||||
expect(parsed.db).toBeUndefined();
|
||||
expect(parsed.tls).toBeUndefined();
|
||||
expect(parsed.password).toBeUndefined();
|
||||
expect(parsed.username).toBeUndefined();
|
||||
});
|
||||
});
|
||||
@@ -10,7 +10,7 @@ function assertExportDescriptor(descriptor: QueueJobDescriptor<typeof directQueu
|
||||
|
||||
describe("export queue contract", () => {
|
||||
test("exports a stable exports queue name through direct and public surfaces", () => {
|
||||
expect(directQueueNames.exports).toBe("toolctrl.exports");
|
||||
expect(directQueueNames.exports).toBe("hivegrid.exports");
|
||||
expect(publicQueueNames.exports).toBe(directQueueNames.exports);
|
||||
});
|
||||
|
||||
|
||||
@@ -16,8 +16,8 @@ import { queueNames, type QueueName } from "./names.js";
|
||||
* 1 — head-of-line-blocking is acceptable inside this group
|
||||
* because all jobs are heavy; we isolate them so they don't
|
||||
* stall the rest of the system.
|
||||
* - `webhook` — external API integrations (AI chat, browser automation)
|
||||
* that are I/O-bound and benefit from high parallelism.
|
||||
* - `webhook` — external API integrations (browser automation) that are
|
||||
* I/O-bound and benefit from high parallelism.
|
||||
* - `plugin` — third-party WebAssembly plugin invocations (Phase 06
|
||||
* #21). Each job spins up a fresh WASM sandbox with hard
|
||||
* memory + fuel limits; isolation from other groups is
|
||||
@@ -31,7 +31,7 @@ export const WORKER_QUEUE_GROUPS = {
|
||||
general: [queueNames.notifications, queueNames.maintenance, queueNames.fileScan, queueNames.filePreview],
|
||||
mail: [queueNames.email],
|
||||
import: [queueNames.imports, queueNames.exports],
|
||||
webhook: [queueNames.aiChatbot, queueNames.browserAutomation],
|
||||
webhook: [queueNames.browserAutomation],
|
||||
plugin: [queueNames.pluginInvoke],
|
||||
} as const satisfies Record<string, readonly QueueName[]>;
|
||||
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
export { defaultJobOptions } from "./types.js";
|
||||
export type { TypedProcessor, TypedQueue, TypedQueueEvents, TypedWorker } from "./types.js";
|
||||
export {
|
||||
buildAiBrowserTaskJob,
|
||||
buildAiChatCompletionJob,
|
||||
buildAiEmbeddingJob,
|
||||
buildBrowserSessionCleanupJob,
|
||||
buildBrowserSessionRunJob,
|
||||
buildBrowserUsageFlushJob,
|
||||
@@ -27,13 +24,6 @@ export {
|
||||
} from "./producers.js";
|
||||
export type { EnqueueObserver } from "./producers.js";
|
||||
export type {
|
||||
AiBrowserTaskJobDescriptor,
|
||||
AiBrowserTaskJobInput,
|
||||
AiChatCompletionJobDescriptor,
|
||||
AiChatCompletionJobInput,
|
||||
AiEmbeddingJobDescriptor,
|
||||
AiEmbeddingJobInput,
|
||||
AiProducerJobDescriptor,
|
||||
BrowserAutomationProducerJobDescriptor,
|
||||
BrowserSessionCleanupJobDescriptor,
|
||||
BrowserSessionCleanupJobInput,
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
import { describe, expect, test } from "vitest";
|
||||
import { isQueueName, queueNames } from "./names.js";
|
||||
|
||||
describe("isQueueName", () => {
|
||||
test("accepts every declared queue name", () => {
|
||||
for (const name of Object.values(queueNames)) {
|
||||
expect(isQueueName(name)).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
test("rejects values that are not registered queue names", () => {
|
||||
expect(isQueueName("hivegrid.unknown")).toBe(false);
|
||||
expect(isQueueName("email")).toBe(false);
|
||||
expect(isQueueName("")).toBe(false);
|
||||
expect(isQueueName("HIVEGRID.EMAIL")).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -1,14 +1,13 @@
|
||||
export const queueNames = {
|
||||
email: "toolctrl.email",
|
||||
notifications: "toolctrl.notifications",
|
||||
maintenance: "toolctrl.maintenance",
|
||||
aiChatbot: "toolctrl.ai-chatbot",
|
||||
browserAutomation: "toolctrl.browser-automation",
|
||||
fileScan: "toolctrl.file-scan",
|
||||
filePreview: "toolctrl.file-preview",
|
||||
exports: "toolctrl.exports",
|
||||
imports: "toolctrl.imports",
|
||||
pluginInvoke: "toolctrl.plugin-invoke",
|
||||
email: "hivegrid.email",
|
||||
notifications: "hivegrid.notifications",
|
||||
maintenance: "hivegrid.maintenance",
|
||||
browserAutomation: "hivegrid.browser-automation",
|
||||
fileScan: "hivegrid.file-scan",
|
||||
filePreview: "hivegrid.file-preview",
|
||||
exports: "hivegrid.exports",
|
||||
imports: "hivegrid.imports",
|
||||
pluginInvoke: "hivegrid.plugin-invoke",
|
||||
} as const;
|
||||
|
||||
export type QueueKey = keyof typeof queueNames;
|
||||
|
||||
@@ -80,11 +80,26 @@ export type CleanupJob = {
|
||||
| "temporary-files"
|
||||
| "expired-export-artifacts"
|
||||
| "dsgvo-grace-period-scan"
|
||||
| "ai-cost-stripe-report"
|
||||
| "ai-cost-billing-report"
|
||||
| "outbox-done-retention"
|
||||
| "workflow-execution-stalled-reconcile"
|
||||
| "task-archive-retention"
|
||||
| "task-reminder-sweep";
|
||||
| "task-reminder-sweep"
|
||||
| "integrations-token-refresh"
|
||||
| "integrations-health-sweep"
|
||||
| "calendar-token-refresh"
|
||||
| "calendar-webhook-renewal"
|
||||
| "calendar-webhook-drain"
|
||||
| "calendar-internal-write-sync"
|
||||
| "email-inbox-token-refresh"
|
||||
| "email-inbox-webhook-renewal"
|
||||
| "email-inbox-webhook-drain"
|
||||
| "email-inbox-initial-sync"
|
||||
| "email-inbox-stale-poll"
|
||||
| "email-inbox-event-cleanup"
|
||||
| "hr-upload-reminder-dispatch"
|
||||
| "email-domain-probe"
|
||||
| "lead-retention-sweep";
|
||||
olderThanHours?: number;
|
||||
olderThanMinutes?: number;
|
||||
take?: number;
|
||||
@@ -92,20 +107,6 @@ export type CleanupJob = {
|
||||
metadata?: Record<string, unknown>;
|
||||
};
|
||||
|
||||
export type AiJob = {
|
||||
kind: "ai.chat-completion" | "ai.embedding" | "ai.browser-task";
|
||||
tenantId?: string;
|
||||
userId?: string;
|
||||
conversationId?: string;
|
||||
model?: string;
|
||||
messages?: readonly {
|
||||
role: "system" | "user" | "assistant" | "tool";
|
||||
content: string;
|
||||
}[];
|
||||
task?: Record<string, unknown>;
|
||||
metadata?: Record<string, unknown>;
|
||||
};
|
||||
|
||||
export type BrowserAutomationJob = {
|
||||
kind: "browser.session-run" | "browser.session-cleanup" | "usage.flush";
|
||||
tenantId?: string;
|
||||
@@ -237,7 +238,6 @@ export type QueueJobPayloads = {
|
||||
[queueNames.email]: TransactionalMailJob | WebhookDeliveryJob;
|
||||
[queueNames.notifications]: NotificationJob;
|
||||
[queueNames.maintenance]: CleanupJob;
|
||||
[queueNames.aiChatbot]: AiJob;
|
||||
[queueNames.browserAutomation]: BrowserAutomationJob;
|
||||
[queueNames.fileScan]: FileScanJob;
|
||||
[queueNames.filePreview]: FilePreviewJob;
|
||||
@@ -250,7 +250,6 @@ export type QueueJobNames = {
|
||||
[queueNames.email]: "send-transactional-email" | "deliver-webhook";
|
||||
[queueNames.notifications]: "send-notification";
|
||||
[queueNames.maintenance]: "run-cleanup" | "dsgvo-grace-period-scan";
|
||||
[queueNames.aiChatbot]: "chat-completion" | "create-embedding" | "browser-task";
|
||||
[queueNames.browserAutomation]: "browser-session-run" | "browser-session-cleanup" | "usage-flush";
|
||||
[queueNames.fileScan]: "scan-file";
|
||||
[queueNames.filePreview]: "generate-file-preview";
|
||||
|
||||
@@ -133,6 +133,29 @@ describe("resolveJobPriorityForTenant", () => {
|
||||
await expect(resolveJobPriorityForTenant(null)).resolves.toBe(JOB_PRIORITY_CRITICAL);
|
||||
});
|
||||
|
||||
test("feature-flag JOB_PRIORITY_ENABLED=0 also neutralises plan-based priority", async () => {
|
||||
setTenantPlanLookup(async () => "enterprise");
|
||||
process.env.JOB_PRIORITY_ENABLED = "0";
|
||||
await expect(resolveJobPriorityForTenant("tenant_ent")).resolves.toBe(JOB_PRIORITY_NORMAL);
|
||||
});
|
||||
|
||||
test("feature-flag JOB_PRIORITY_ENABLED=true keeps plan-based priority active", async () => {
|
||||
setTenantPlanLookup(async () => "enterprise");
|
||||
process.env.JOB_PRIORITY_ENABLED = "true";
|
||||
await expect(resolveJobPriorityForTenant("tenant_ent")).resolves.toBe(JOB_PRIORITY_CRITICAL);
|
||||
});
|
||||
|
||||
test("stringifies non-Error lookup failures before logging", async () => {
|
||||
const warn = vi.fn();
|
||||
setTenantPlanLookup(async () => {
|
||||
// eslint-disable-next-line @typescript-eslint/only-throw-error
|
||||
throw "redis socket closed";
|
||||
});
|
||||
await expect(resolveJobPriorityForTenant("tenant_1", { logger: { warn } })).resolves.toBe(JOB_PRIORITY_NORMAL);
|
||||
expect(warn).toHaveBeenCalledTimes(1);
|
||||
expect(warn.mock.calls[0]?.[0]).toMatchObject({ tenantId: "tenant_1", error: "redis socket closed" });
|
||||
});
|
||||
|
||||
test("setTenantPlanLookup / getTenantPlanLookup round-trip", () => {
|
||||
expect(getTenantPlanLookup()).toBeNull();
|
||||
const lookup: TenantPlanLookup = async () => "pro";
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
import { afterEach, describe, expect, test, vi } from "vitest";
|
||||
import {
|
||||
buildFilePreviewJob,
|
||||
getEnqueueObserver,
|
||||
queueNames,
|
||||
setEnqueueObserver,
|
||||
type EnqueueObserver,
|
||||
} from "./index.js";
|
||||
|
||||
describe("buildFilePreviewJob validation", () => {
|
||||
const validInput = {
|
||||
fileId: "file_1",
|
||||
tenantId: "tenant_1",
|
||||
bucket: "uploads",
|
||||
storageKey: "tenants/tenant_1/files/file_1/original",
|
||||
mimeType: "image/heic",
|
||||
} as const;
|
||||
|
||||
test("rejects a missing or blank fileId", () => {
|
||||
expect(() => buildFilePreviewJob({ ...validInput, fileId: "" })).toThrow("File preview jobs require fileId.");
|
||||
expect(() => buildFilePreviewJob({ ...validInput, fileId: " " })).toThrow("File preview jobs require fileId.");
|
||||
});
|
||||
|
||||
test("rejects a missing or blank tenantId", () => {
|
||||
expect(() => buildFilePreviewJob({ ...validInput, tenantId: "" })).toThrow("File preview jobs require tenantId.");
|
||||
expect(() => buildFilePreviewJob({ ...validInput, tenantId: " " })).toThrow("File preview jobs require tenantId.");
|
||||
});
|
||||
|
||||
test("builds a descriptor for a valid input", () => {
|
||||
expect(buildFilePreviewJob(validInput)).toEqual({
|
||||
queueName: queueNames.filePreview,
|
||||
jobName: "generate-file-preview",
|
||||
payload: {
|
||||
kind: "preview.file",
|
||||
...validInput,
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("enqueue observer accessor", () => {
|
||||
afterEach(() => {
|
||||
setEnqueueObserver(null);
|
||||
});
|
||||
|
||||
test("getEnqueueObserver returns null by default", () => {
|
||||
setEnqueueObserver(null);
|
||||
expect(getEnqueueObserver()).toBeNull();
|
||||
});
|
||||
|
||||
test("getEnqueueObserver returns the registered observer", () => {
|
||||
const observer: EnqueueObserver = vi.fn();
|
||||
setEnqueueObserver(observer);
|
||||
expect(getEnqueueObserver()).toBe(observer);
|
||||
});
|
||||
});
|
||||
@@ -4,9 +4,6 @@ import {
|
||||
JOB_PRIORITY_HIGH,
|
||||
JOB_PRIORITY_LOW,
|
||||
JOB_PRIORITY_NORMAL,
|
||||
buildAiBrowserTaskJob,
|
||||
buildAiChatCompletionJob,
|
||||
buildAiEmbeddingJob,
|
||||
buildBrowserSessionCleanupJob,
|
||||
buildBrowserSessionRunJob,
|
||||
buildBrowserUsageFlushJob,
|
||||
@@ -232,75 +229,6 @@ describe("queue producers", () => {
|
||||
expect(descriptor.jobName).not.toBe("run-export");
|
||||
});
|
||||
|
||||
test("builds AI chat completion descriptors", () => {
|
||||
expect(
|
||||
buildAiChatCompletionJob({
|
||||
tenantId: "tenant_1",
|
||||
userId: "user_1",
|
||||
conversationId: "conversation_1",
|
||||
model: "model-chat",
|
||||
messages: [{ role: "user", content: "Summarize this." }],
|
||||
metadata: { requestId: "req_ai_chat_1" },
|
||||
})
|
||||
).toEqual({
|
||||
queueName: queueNames.aiChatbot,
|
||||
jobName: "chat-completion",
|
||||
payload: {
|
||||
kind: "ai.chat-completion",
|
||||
tenantId: "tenant_1",
|
||||
userId: "user_1",
|
||||
conversationId: "conversation_1",
|
||||
model: "model-chat",
|
||||
messages: [{ role: "user", content: "Summarize this." }],
|
||||
metadata: { requestId: "req_ai_chat_1" },
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
test("builds AI embedding descriptors", () => {
|
||||
expect(
|
||||
buildAiEmbeddingJob({
|
||||
tenantId: "tenant_2",
|
||||
userId: "user_2",
|
||||
model: "model-embedding",
|
||||
messages: [{ role: "user", content: "Index this document." }],
|
||||
metadata: { documentId: "doc_1" },
|
||||
})
|
||||
).toEqual({
|
||||
queueName: queueNames.aiChatbot,
|
||||
jobName: "create-embedding",
|
||||
payload: {
|
||||
kind: "ai.embedding",
|
||||
tenantId: "tenant_2",
|
||||
userId: "user_2",
|
||||
model: "model-embedding",
|
||||
messages: [{ role: "user", content: "Index this document." }],
|
||||
metadata: { documentId: "doc_1" },
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
test("builds AI browser task descriptors on the AI queue", () => {
|
||||
expect(
|
||||
buildAiBrowserTaskJob({
|
||||
tenantId: "tenant_3",
|
||||
userId: "user_3",
|
||||
task: { action: "research", url: "https://example.test" },
|
||||
metadata: { requestId: "req_ai_browser_1" },
|
||||
})
|
||||
).toEqual({
|
||||
queueName: queueNames.aiChatbot,
|
||||
jobName: "browser-task",
|
||||
payload: {
|
||||
kind: "ai.browser-task",
|
||||
tenantId: "tenant_3",
|
||||
userId: "user_3",
|
||||
task: { action: "research", url: "https://example.test" },
|
||||
metadata: { requestId: "req_ai_browser_1" },
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
test("builds browser automation session run descriptors on the browser automation queue", () => {
|
||||
expect(
|
||||
buildBrowserSessionRunJob({
|
||||
@@ -729,26 +657,6 @@ describe("queue producers", () => {
|
||||
);
|
||||
});
|
||||
|
||||
test("keeps AI browser task descriptors separate from browser automation session jobs", () => {
|
||||
const aiBrowserTask = buildAiBrowserTaskJob({
|
||||
tenantId: "tenant_shared",
|
||||
task: { action: "research" },
|
||||
});
|
||||
const browserSessionRun = buildBrowserSessionRunJob({
|
||||
tenantId: "tenant_shared",
|
||||
sessionId: "session_shared",
|
||||
policyId: "policy_shared",
|
||||
task: { action: "research" },
|
||||
});
|
||||
|
||||
expect(aiBrowserTask.queueName).toBe(queueNames.aiChatbot);
|
||||
expect(aiBrowserTask.jobName).toBe("browser-task");
|
||||
expect(aiBrowserTask.payload.kind).toBe("ai.browser-task");
|
||||
expect(browserSessionRun.queueName).toBe(queueNames.browserAutomation);
|
||||
expect(browserSessionRun.jobName).toBe("browser-session-run");
|
||||
expect(browserSessionRun.payload.kind).toBe("browser.session-run");
|
||||
});
|
||||
|
||||
test("preserves requestId metadata on every producer descriptor", () => {
|
||||
const descriptors = [
|
||||
buildTransactionalMailJob({
|
||||
@@ -771,21 +679,6 @@ describe("queue producers", () => {
|
||||
metadata: { requestId: "req_notification_1" },
|
||||
}),
|
||||
buildCleanupJob({ task: "expired-sessions", metadata: { requestId: "req_cleanup_1" } }),
|
||||
buildAiChatCompletionJob({
|
||||
tenantId: "tenant_1",
|
||||
messages: [{ role: "user", content: "Hello" }],
|
||||
metadata: { requestId: "req_ai_chat_1" },
|
||||
}),
|
||||
buildAiEmbeddingJob({
|
||||
tenantId: "tenant_1",
|
||||
messages: [{ role: "user", content: "Embed this" }],
|
||||
metadata: { requestId: "req_ai_embedding_1" },
|
||||
}),
|
||||
buildAiBrowserTaskJob({
|
||||
tenantId: "tenant_1",
|
||||
task: { action: "research" },
|
||||
metadata: { requestId: "req_ai_browser_1" },
|
||||
}),
|
||||
buildBrowserSessionRunJob({
|
||||
tenantId: "tenant_1",
|
||||
sessionId: "session_1",
|
||||
@@ -828,9 +721,6 @@ describe("queue producers", () => {
|
||||
}),
|
||||
buildNotificationJob({ tenantId: "tenant_1", userId: "user_1", channel: "in-app", messageKey: "message.ready" }),
|
||||
buildCleanupJob({ task: "expired-sessions" }),
|
||||
buildAiChatCompletionJob({ tenantId: "tenant_1", messages: [{ role: "user", content: "Hello" }] }),
|
||||
buildAiEmbeddingJob({ tenantId: "tenant_1", messages: [{ role: "user", content: "Embed this" }] }),
|
||||
buildAiBrowserTaskJob({ tenantId: "tenant_1", task: { action: "research" } }),
|
||||
buildBrowserSessionRunJob({ tenantId: "tenant_1", sessionId: "session_1", task: { action: "capture" } }),
|
||||
buildBrowserSessionCleanupJob({ tenantId: "tenant_1", sessionId: "session_1" }),
|
||||
buildBrowserUsageFlushJob({ tenantId: "tenant_1", sessionId: "session_1" }),
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import type { JobsOptions, TypedQueue } from "./types.js";
|
||||
import { queueNames, type QueueName } from "./names.js";
|
||||
import type {
|
||||
AiJob,
|
||||
BrowserAutomationJob,
|
||||
CleanupJob,
|
||||
ExportJob,
|
||||
@@ -20,16 +19,12 @@ import type {
|
||||
} from "./payloads.js";
|
||||
import { JOB_PRIORITY_CRITICAL, priorityFromOverride, priorityLabel, resolveJobPriorityForTenant, type PriorityOverride } from "./priority.js";
|
||||
|
||||
type AiJobForKind<Kind extends AiJob["kind"]> = AiJob & { kind: Kind };
|
||||
type BrowserAutomationJobForKind<Kind extends BrowserAutomationJob["kind"]> = BrowserAutomationJob & { kind: Kind };
|
||||
|
||||
export type TransactionalMailJobInput = Omit<TransactionalMailJob, "kind">;
|
||||
export type WebhookDeliveryJobInput = Omit<WebhookDeliveryJob, "kind">;
|
||||
export type NotificationJobInput = Omit<NotificationJob, "kind">;
|
||||
export type CleanupJobInput = Omit<CleanupJob, "kind">;
|
||||
export type AiChatCompletionJobInput = Omit<AiJobForKind<"ai.chat-completion">, "kind">;
|
||||
export type AiEmbeddingJobInput = Omit<AiJobForKind<"ai.embedding">, "kind">;
|
||||
export type AiBrowserTaskJobInput = Omit<AiJobForKind<"ai.browser-task">, "kind">;
|
||||
export type BrowserSessionRunJobInput = Omit<BrowserAutomationJobForKind<"browser.session-run">, "kind">;
|
||||
export type BrowserSessionCleanupJobInput = Omit<BrowserAutomationJobForKind<"browser.session-cleanup">, "kind">;
|
||||
export type BrowserUsageFlushJobInput = Omit<BrowserAutomationJobForKind<"usage.flush">, "kind">;
|
||||
@@ -96,21 +91,6 @@ export type CleanupJobDescriptor = QueueJobDescriptor<typeof queueNames.maintena
|
||||
payload: CleanupJob;
|
||||
};
|
||||
|
||||
export type AiChatCompletionJobDescriptor = QueueJobDescriptor<typeof queueNames.aiChatbot> & {
|
||||
jobName: "chat-completion";
|
||||
payload: AiJobForKind<"ai.chat-completion">;
|
||||
};
|
||||
|
||||
export type AiEmbeddingJobDescriptor = QueueJobDescriptor<typeof queueNames.aiChatbot> & {
|
||||
jobName: "create-embedding";
|
||||
payload: AiJobForKind<"ai.embedding">;
|
||||
};
|
||||
|
||||
export type AiBrowserTaskJobDescriptor = QueueJobDescriptor<typeof queueNames.aiChatbot> & {
|
||||
jobName: "browser-task";
|
||||
payload: AiJobForKind<"ai.browser-task">;
|
||||
};
|
||||
|
||||
export type BrowserSessionRunJobDescriptor = QueueJobDescriptor<typeof queueNames.browserAutomation> & {
|
||||
jobName: "browser-session-run";
|
||||
payload: BrowserAutomationJobForKind<"browser.session-run">;
|
||||
@@ -152,14 +132,12 @@ export type PluginInvokeJobDescriptor = QueueJobDescriptor<typeof queueNames.plu
|
||||
};
|
||||
|
||||
export type EmailProducerJobDescriptor = TransactionalMailJobDescriptor | WebhookDeliveryJobDescriptor;
|
||||
export type AiProducerJobDescriptor = AiChatCompletionJobDescriptor | AiEmbeddingJobDescriptor | AiBrowserTaskJobDescriptor;
|
||||
export type BrowserAutomationProducerJobDescriptor = BrowserSessionRunJobDescriptor | BrowserSessionCleanupJobDescriptor | BrowserUsageFlushJobDescriptor;
|
||||
|
||||
export type ProducerJobDescriptor =
|
||||
| EmailProducerJobDescriptor
|
||||
| NotificationJobDescriptor
|
||||
| CleanupJobDescriptor
|
||||
| AiProducerJobDescriptor
|
||||
| BrowserAutomationProducerJobDescriptor
|
||||
| FileScanJobDescriptor
|
||||
| FilePreviewJobDescriptor
|
||||
@@ -185,11 +163,6 @@ const retryJobOptionsByQueueAndJobName: {
|
||||
// next 24h tick recover the run.
|
||||
"dsgvo-grace-period-scan": { attempts: 2, backoff: { type: "fixed", delay: 30_000 } },
|
||||
},
|
||||
[queueNames.aiChatbot]: {
|
||||
"chat-completion": { attempts: 3, backoff: { type: "exponential", delay: 2_000 } },
|
||||
"create-embedding": { attempts: 3, backoff: { type: "exponential", delay: 2_000 } },
|
||||
"browser-task": { attempts: 2, backoff: { type: "exponential", delay: 5_000 } },
|
||||
},
|
||||
[queueNames.browserAutomation]: {
|
||||
"browser-session-run": { attempts: 2, backoff: { type: "exponential", delay: 5_000 } },
|
||||
"browser-session-cleanup": { attempts: 3, backoff: { type: "fixed", delay: 10_000 } },
|
||||
@@ -279,39 +252,6 @@ export function buildCleanupJob(input: CleanupJobInput): CleanupJobDescriptor {
|
||||
};
|
||||
}
|
||||
|
||||
export function buildAiChatCompletionJob(input: AiChatCompletionJobInput): AiChatCompletionJobDescriptor {
|
||||
return {
|
||||
queueName: queueNames.aiChatbot,
|
||||
jobName: "chat-completion",
|
||||
payload: {
|
||||
kind: "ai.chat-completion",
|
||||
...input,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function buildAiEmbeddingJob(input: AiEmbeddingJobInput): AiEmbeddingJobDescriptor {
|
||||
return {
|
||||
queueName: queueNames.aiChatbot,
|
||||
jobName: "create-embedding",
|
||||
payload: {
|
||||
kind: "ai.embedding",
|
||||
...input,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function buildAiBrowserTaskJob(input: AiBrowserTaskJobInput): AiBrowserTaskJobDescriptor {
|
||||
return {
|
||||
queueName: queueNames.aiChatbot,
|
||||
jobName: "browser-task",
|
||||
payload: {
|
||||
kind: "ai.browser-task",
|
||||
...input,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function buildBrowserSessionRunJob(input: BrowserSessionRunJobInput): BrowserSessionRunJobDescriptor {
|
||||
return {
|
||||
queueName: queueNames.browserAutomation,
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
import { describe, expect, test, vi } from "vitest";
|
||||
import { queueNames } from "./names.js";
|
||||
|
||||
// Track every BullMQ primitive constructed through the factory wrappers so we
|
||||
// can assert the thin pass-through layer forwards arguments verbatim. Declared
|
||||
// via vi.hoisted so it is initialised before the hoisted vi.mock factory runs.
|
||||
const { constructions } = vi.hoisted(() => ({
|
||||
constructions: [] as { cls: string; args: unknown[] }[],
|
||||
}));
|
||||
|
||||
vi.mock("bullmq", () => {
|
||||
class Queue {
|
||||
constructor(...args: unknown[]) {
|
||||
constructions.push({ cls: "Queue", args });
|
||||
}
|
||||
}
|
||||
class QueueEvents {
|
||||
constructor(...args: unknown[]) {
|
||||
constructions.push({ cls: "QueueEvents", args });
|
||||
}
|
||||
}
|
||||
class Worker {
|
||||
constructor(...args: unknown[]) {
|
||||
constructions.push({ cls: "Worker", args });
|
||||
}
|
||||
}
|
||||
return { Queue, QueueEvents, Worker };
|
||||
});
|
||||
|
||||
// Imported after the hoisted mock so the ESM factory pulls the mocked bullmq.
|
||||
import { createQueue, createQueueEvents, createWorker } from "./factory.js";
|
||||
|
||||
describe("ESM runtime factory (factory.ts -> runtime.ts)", () => {
|
||||
test("createQueue merges defaultJobOptions into the BullMQ Queue options", () => {
|
||||
const options = { connection: { host: "localhost", port: 6379 } } as never;
|
||||
const queue = createQueue(queueNames.email, options);
|
||||
|
||||
expect(queue).toBeDefined();
|
||||
const last = constructions.at(-1);
|
||||
expect(last?.cls).toBe("Queue");
|
||||
expect(last?.args[0]).toBe(queueNames.email);
|
||||
expect(last?.args[1]).toMatchObject({
|
||||
defaultJobOptions: { attempts: 3 },
|
||||
connection: { host: "localhost", port: 6379 },
|
||||
});
|
||||
});
|
||||
|
||||
test("createQueueEvents forwards the queue name (options optional)", () => {
|
||||
const queueEvents = createQueueEvents(queueNames.notifications);
|
||||
|
||||
expect(queueEvents).toBeDefined();
|
||||
const last = constructions.at(-1);
|
||||
expect(last?.cls).toBe("QueueEvents");
|
||||
expect(last?.args[0]).toBe(queueNames.notifications);
|
||||
});
|
||||
|
||||
test("createWorker forwards the queue name, processor and options", () => {
|
||||
const processor = async () => "done";
|
||||
const worker = createWorker(queueNames.fileScan, processor as never, { connection: {} } as never);
|
||||
|
||||
expect(worker).toBeDefined();
|
||||
const last = constructions.at(-1);
|
||||
expect(last?.cls).toBe("Worker");
|
||||
expect(last?.args[0]).toBe(queueNames.fileScan);
|
||||
expect(last?.args[1]).toBe(processor);
|
||||
});
|
||||
});
|
||||
|
||||
describe("CJS runtime entry (runtime.cjs)", () => {
|
||||
// The CJS entry is loaded natively (Node `require`), so vi.mock("bullmq")
|
||||
// does not intercept it. We assert the module-level export contract without
|
||||
// invoking the factories — calling them would construct a real BullMQ
|
||||
// primitive and open a Redis connection the test cannot satisfy.
|
||||
test("exports the same factory surface and the retry defaults", async () => {
|
||||
const mod = (await import("./runtime.cjs")) as Record<string, unknown>;
|
||||
const cjs = ((mod.default as Record<string, unknown> | undefined) ?? mod) as {
|
||||
createQueue: unknown;
|
||||
createQueueEvents: unknown;
|
||||
createWorker: unknown;
|
||||
defaultJobOptions: {
|
||||
attempts: number;
|
||||
removeOnComplete: number;
|
||||
removeOnFail: number;
|
||||
backoff: { delay: number; type: string };
|
||||
};
|
||||
};
|
||||
|
||||
expect(typeof cjs.createQueue).toBe("function");
|
||||
expect(typeof cjs.createQueueEvents).toBe("function");
|
||||
expect(typeof cjs.createWorker).toBe("function");
|
||||
expect(cjs.defaultJobOptions).toMatchObject({ attempts: 3, removeOnComplete: 100, removeOnFail: 250 });
|
||||
expect(cjs.defaultJobOptions.backoff).toMatchObject({ delay: 1000, type: "exponential" });
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user