Skip to content

Free & Open-Source Invoice Generator - No Sign-Up, No Ads, Instant PDF Export, 100% In-Browser, and Fully Customizable Templates.

6.1 KB
Raw Blame
import { describe, it, expect } from "vitest";

import { telegramUpdateSchema } from "./telegram-schema";

describe("Telegram Schema Validation", () => {
  describe("telegramUpdateSchema", () => {
    it("should validate a valid Telegram update", () => {
      const validUpdate = {
        update_id: 123_456_789,
        message: {
          message_id: 1,
          from: {
            id: 987_654_321,
            is_bot: false,
            first_name: "John",
          },
          chat: {
            id: 987_654_321,
            type: "private",
          },
          date: 1_234_567_890,
          text: "/generate",
        },
      } as const;

      // `parse` throws on an invalid update, so the test still fails loudly
      // without an `if (result.success)` guard around the assertions.
      const parsed = telegramUpdateSchema.parse(validUpdate);

      expect(parsed.update_id).toBe(123_456_789);
      expect(parsed.message?.text).toBe("/generate");
    });

    it("should validate message without entities", () => {
      const updateWithoutEntities = {
        update_id: 123_456_789,
        message: {
          message_id: 1,
          from: {
            id: 987_654_321,
            is_bot: false,
            first_name: "John",
          },
          chat: {
            id: 987_654_321,
            type: "private",
          },
          date: 1_234_567_890,
          text: "/generate",
        },
      } as const;

      const parsed = telegramUpdateSchema.parse(updateWithoutEntities);

      expect(parsed.message?.entities).toBeUndefined();
    });

    it("should reject update with missing update_id", () => {
      const invalidUpdate = {
        message: {
          message_id: 1,
          from: {
            id: 987_654_321,
            is_bot: false,
            first_name: "John",
          },
          chat: {
            id: 987_654_321,
            type: "private",
          },
          date: 1_234_567_890,
          text: "/generate",
        },
      } as const;

      const result = telegramUpdateSchema.safeParse(invalidUpdate);

      expect(result.success).toBe(false);
    });

    it("should reject update with missing message", () => {
      const updateWithoutMessage = {
        update_id: 123_456_789,
      } as const;

      const result = telegramUpdateSchema.safeParse(updateWithoutMessage);

      expect(result.success).toBe(false);
    });

    it("should reject update with invalid from object", () => {
      const invalidUpdate = {
        update_id: 123_456_789,
        message: {
          message_id: 1,
          from: {
            id: "not-a-number",
            is_bot: false,
            first_name: "John",
          },
          chat: {
            id: 987_654_321,
            type: "private",
          },
          date: 1_234_567_890,
          text: "/generate",
        },
      } as const;

      const result = telegramUpdateSchema.safeParse(invalidUpdate);
      expect(result.success).toBe(false);
    });

    it("should reject update with extra unknown fields", () => {
      const updateWithExtra = {
        update_id: 123_456_789,
        message: {
          message_id: 1,
          from: {
            id: 987_654_321,
            is_bot: false,
            first_name: "John",
          },
          chat: {
            id: 987_654_321,
            type: "private",
          },
          date: 1_234_567_890,
          text: "/generate",
        },
        extra_field: "should fail",
      } as const;

      const result = telegramUpdateSchema.safeParse(updateWithExtra);
      expect(result.success).toBe(false);
    });

    it("should validate update with entities", () => {
      const updateWithEntities = {
        update_id: 123_456_789,
        message: {
          message_id: 1,
          from: {
            id: 987_654_321,
            is_bot: false,
            first_name: "John",
          },
          chat: {
            id: 987_654_321,
            type: "private",
          },
          date: 1_234_567_890,
          text: "/generate",
          entities: [
            {
              offset: 0,
              length: 9,
              type: "bot_command",
            },
          ],
        },
      } as const;

      const parsed = telegramUpdateSchema.parse(updateWithEntities);

      expect(parsed.message?.entities).toHaveLength(1);
      expect(parsed.message?.entities?.[0].type).toBe("bot_command");
    });

    it("should reject message with text other than '/generate'", () => {
      const updateWithInvalidCommand = {
        update_id: 123_456_789,
        message: {
          message_id: 1,
          from: {
            id: 987_654_321,
            is_bot: false,
            first_name: "John",
          },
          chat: {
            id: 987_654_321,
            type: "private",
          },
          date: 1_234_567_890,
          text: "/start",
        },
      } as const;

      const result = telegramUpdateSchema.safeParse(updateWithInvalidCommand);
      expect(result.success).toBe(false);
    });

    it("should reject message with empty text", () => {
      const updateWithEmptyText = {
        update_id: 123_456_789,
        message: {
          message_id: 1,
          from: {
            id: 987_654_321,
            is_bot: false,
            first_name: "John",
          },
          chat: {
            id: 987_654_321,
            type: "private",
          },
          date: 1_234_567_890,
          text: "",
        },
      } as const;

      const result = telegramUpdateSchema.safeParse(updateWithEmptyText);
      expect(result.success).toBe(false);
    });

    it("should accept message with exactly '/generate' text", () => {
      const updateWithGenerateCommand = {
        update_id: 123_456_789,
        message: {
          message_id: 1,
          from: {
            id: 987_654_321,
            is_bot: false,
            first_name: "John",
          },
          chat: {
            id: 987_654_321,
            type: "private",
          },
          date: 1_234_567_890,
          text: "/generate",
        },
      } as const;

      const parsed = telegramUpdateSchema.parse(updateWithGenerateCommand);

      expect(parsed.message?.text).toBe("/generate");
    });
  });
});