import { describe, test, expect } from "bun:test";
import { SessionController } from "../../../src/controllers/SessionController";
import { buildMockSession } from "../../setup";

describe("SessionController", () => {
  // -------------------------------------------------------------------
  // Constructor
  // -------------------------------------------------------------------
  describe("constructor", () => {
    test("sets all public properties from session data", () => {
      const data = buildMockSession();
      const ctrl = new SessionController(data);
      expect(ctrl.id).toBe("session-1");
      expect(ctrl.sessionKey).toBe("sk-abc123");
      expect(ctrl.userId).toBe("user-1");
      expect(ctrl.expires).toBeInstanceOf(Date);
      expect(ctrl.refreshedAt).toBeNull();
      expect(ctrl.invalidatedAt).toBeNull();
      expect(ctrl.terminated).toBe(false);
    });

    test("sets custom values from overrides", () => {
      const refreshDate = new Date("2025-06-01");
      const ctrl = new SessionController(
        buildMockSession({ refreshedAt: refreshDate }),
      );
      expect(ctrl.refreshedAt).toEqual(refreshDate);
    });
  });

  // -------------------------------------------------------------------
  // invalidate
  // -------------------------------------------------------------------
  describe("invalidate()", () => {
    test("throws when session is already invalidated", async () => {
      const ctrl = new SessionController(
        buildMockSession({ invalidatedAt: new Date() }),
      );
      await expect(ctrl.invalidate()).rejects.toThrow(
        "Session has already been invalidated",
      );
    });
  });

  // -------------------------------------------------------------------
  // generateTokens
  // -------------------------------------------------------------------
  describe("generateTokens()", () => {
    test("throws when tokens have already been generated", async () => {
      const ctrl = new SessionController(
        buildMockSession({ refreshTokenGenerated: true }),
      );
      await expect(ctrl.generateTokens()).rejects.toThrow(
        "Tokens have alredy been generated",
      );
    });
  });
});
