Skip to content

Commit

Permalink
feat: added test for save-tosend-message
Browse files Browse the repository at this point in the history
  • Loading branch information
henriquecweiss committed Nov 21, 2024
1 parent 76cdb3a commit f5b3ed6
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 9 deletions.
29 changes: 29 additions & 0 deletions packages/jobs/schemas/tosend-messages.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,32 @@ export const querySchema = z
data.toTimestamp >= data.fromTimestamp,
{ message: "toTimestamp must be greater than or equal to fromTimestamp" },
);

const metaValueSchema = z.union([
z.string(),
z.number(),
z.boolean(),
z.null(),
z.array(z.union([z.string(), z.number(), z.boolean(), z.null()])),
z.record(
z.string(),
z.union([z.string(), z.number(), z.boolean(), z.null()]),
),
]);

const messageSchema = z.object({
userId: z.string().min(1),
text: z.string().min(1),
timestamp: z.number(),
meta: z.record(z.string(), metaValueSchema).optional(),
});

export type MetaValue =
| string
| number
| boolean
| null
| Array<string | number | boolean | null>
| Record<string, string | number | boolean | null>;

export { messageSchema };
89 changes: 89 additions & 0 deletions packages/jobs/tests/save-tosend-message.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { describe, expect, it } from "vitest";
import { messageSchema } from "../schemas/tosend-messages.schema";

interface MessageParams {
userId: string;
text: string;
timestamp: number;
meta?: Record<string, unknown>;
}

// Test factories
function createValidMessage(overrides?: Partial<MessageParams>): MessageParams {
return {
userId: "user123",
text: "Hello, this is a test message",
timestamp: Date.now(),
...overrides,
};
}

describe("Save Tosend Message Schema Validation", () => {
describe("valid messages", () => {
it("should parse a complete valid message", () => {
const validMessage = createValidMessage();
const result = messageSchema.parse(validMessage);
expect(result).toEqual(validMessage);
});

it("should accept messages with meta data", () => {
const validMessage = createValidMessage({
meta: {
priority: "high",
category: "notification",
tags: ["important", "urgent"],
},
});
const result = messageSchema.parse(validMessage);
expect(result).toEqual(validMessage);
});

it("should accept valid timestamps", () => {
const validMessage = createValidMessage({
timestamp: 1234567890,
});
const result = messageSchema.parse(validMessage);
expect(result).toEqual(validMessage);
});
});

describe("invalid messages", () => {
it("should throw an error for missing required fields", () => {
const invalidMessage = {
userId: "user123",
// Missing other required fields
};

expect(() => messageSchema.parse(invalidMessage)).toThrow();
});

it("should throw an error for empty text", () => {
const invalidMessage = createValidMessage({
text: "",
});
expect(() => messageSchema.parse(invalidMessage)).toThrow();
});

it("should throw an error for invalid timestamp type", () => {
const invalidMessage = createValidMessage({
// @ts-expect-error Testing invalid timestamp type
timestamp: "not a number",
});
expect(() => messageSchema.parse(invalidMessage)).toThrow();
});

it("should throw an error for invalid userId type", () => {
const invalidMessage = createValidMessage({
// @ts-expect-error Testing invalid userId type
userId: 123,
});
expect(() => messageSchema.parse(invalidMessage)).toThrow();
});

it("should throw an error for missing userId", () => {
const invalidMessage = createValidMessage();
const { userId, ...invalidMessageWithoutUserId } = invalidMessage;
expect(() => messageSchema.parse(invalidMessageWithoutUserId)).toThrow();
});
});
});
16 changes: 7 additions & 9 deletions packages/jobs/trigger/save-tosend-message.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import { logger, task } from "@trigger.dev/sdk/v3";
import { z } from "zod";
import type { z } from "zod";
import { supabase } from "../lib/supabase";

const messageSchema = z.object({
userId: z.string(),
text: z.string(),
timestamp: z.number(),
meta: z.record(z.any()),
});
import type { messageSchema } from "../schemas/tosend-messages.schema";

export const saveTosendMessageTask = task({
id: "save-tosend-message",
Expand All @@ -31,7 +25,11 @@ export const saveTosendMessageTask = task({
return { success: true };
} catch (error) {
logger.error("Error saving tosend message", { error, payload });
return { success: false, error: (error as Error).message };
return {
success: false,
error:
error instanceof Error ? error.message : "Unknown error occurred",
};
}
},
});

0 comments on commit f5b3ed6

Please sign in to comment.