message-pipeline.test.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { describe, expect, test } from "bun:test";
  2. import {
  3. createUserMessage,
  4. createAssistantMessage,
  5. normalizeMessages,
  6. extractTag,
  7. } from "../../src/utils/messages";
  8. // ─── Message Structure ────────────────────────────────────────────────
  9. describe("Message pipeline: message structure", () => {
  10. test("createUserMessage returns a Message with type 'user'", () => {
  11. const msg = createUserMessage("hello");
  12. expect(msg.type).toBe("user");
  13. expect(msg.message.role).toBe("user");
  14. expect(msg.uuid).toBeTruthy();
  15. expect(msg.timestamp).toBeTruthy();
  16. });
  17. test("createAssistantMessage returns a Message with type 'assistant'", () => {
  18. const msg = createAssistantMessage("response");
  19. expect(msg.type).toBe("assistant");
  20. expect(msg.message.role).toBe("assistant");
  21. expect(msg.uuid).toBeTruthy();
  22. });
  23. test("user and assistant messages have different UUIDs", () => {
  24. const user = createUserMessage("hello");
  25. const assistant = createAssistantMessage("response");
  26. expect(user.uuid).not.toBe(assistant.uuid);
  27. });
  28. });
  29. // ─── Tag Extraction ───────────────────────────────────────────────────
  30. describe("Message pipeline: tag extraction", () => {
  31. test("extractTag returns null for non-matching tag", () => {
  32. expect(extractTag("no tags here", "think")).toBeNull();
  33. });
  34. test("extractTag returns null for empty string", () => {
  35. expect(extractTag("", "think")).toBeNull();
  36. });
  37. test("extractTag requires tagName parameter", () => {
  38. // Calling without tagName throws
  39. expect(() => (extractTag as any)("hello")).toThrow();
  40. });
  41. });
  42. // ─── Normalization ────────────────────────────────────────────────────
  43. describe("Message pipeline: normalization", () => {
  44. test("normalizeMessages returns an array", () => {
  45. const msg = createUserMessage("hello");
  46. const result = normalizeMessages([msg]);
  47. expect(Array.isArray(result)).toBe(true);
  48. });
  49. test("normalizeMessages preserves at least one message for simple input", () => {
  50. const msg = createUserMessage("hello");
  51. const result = normalizeMessages([msg]);
  52. expect(result.length).toBeGreaterThanOrEqual(1);
  53. });
  54. });