AkurAI Build
Menu

Public repository / 0216cf0f6008aae88a648a82e9b3a462a6688302

AkurAI-Mail / src/lib/webmail/session.test.ts

Latest change 0d7f5f902b20084326479f36d4ccf2b55ee890c0 - OLI-317: scheduled sends read the API's payload field (legacy message kept) by Ólafur Búi Ólafsson

import { describe, expect, test } from "bun:test";
import { scheduledSendView, webmailSession } from "./session";

describe("webmailSession", () => {
  test("OIDC session: mailbox is the signed-in user, not an admin", () => {
    expect(webmailSession({ authenticated: true, user: "me@x.is", method: "oidc" })).toEqual({ signedIn: true, email: "me@x.is", admin: false });
  });

  test("explicit admin/email fields win", () => {
    expect(webmailSession({ authenticated: true, admin: true, email: "a@x.is", user: "root" })).toEqual({ signedIn: true, email: "a@x.is", admin: true });
  });

  test("password admin login is an admin", () => {
    expect(webmailSession({ authenticated: true, user: "admin", method: "password", mailbox: "" }).admin).toBe(true);
  });

  test("no session", () => {
    expect(webmailSession({ authenticated: false, mailbox: "", method: "none" })).toEqual({ signedIn: false, email: "", admin: false });
  });
});

describe("scheduledSendView", () => {

  test("reads the API's payload field", () => {
    const view = scheduledSendView({ id: "s1", status: "pending", send_at: 1700000000, payload: { subject: "Hi", to: "a@x.is" } });
    expect(view).toEqual({ id: "s1", status: "pending", sendAt: 1700000000, subject: "Hi", to: "a@x.is" });
  });

  test("still reads the legacy message field", () => {
    const view = scheduledSendView({ id: "s2", status: "pending", send_at: 1, message: { subject: "Old", to: "b@x.is" } });
    expect(view.subject).toBe("Old");
    expect(view.to).toBe("b@x.is");
  });

  test("tolerates a missing or malformed body", () => {
    expect(scheduledSendView({ id: "s3", status: "sent", send_at: 5 })).toEqual({ id: "s3", status: "sent", sendAt: 5, subject: "", to: "" });
    expect(scheduledSendView({ id: "s4", status: "pending", send_at: "7", payload: "nonsense" }).sendAt).toBe(7);
    expect(scheduledSendView({ id: "s5", status: "pending", send_at: 9, payload: { to: ["c@x.is", "d@x.is"] } }).to).toBe("c@x.is, d@x.is");
  });
});