Files
agentic-dev/packages/auth/src/di/container.test.ts

52 lines
2.0 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { authContainer } from "./container";
import { AUTH_SYMBOLS } from "./symbols";
import { AuthModule } from "./module";
import { MockUsersRepository } from "@/infrastructure/repositories/mock-users.repository";
import { MockAuthenticationService } from "@/infrastructure/services/mock-authentication.service";
import type { IUsersRepository } from "@/application/repositories/users-repository.interface";
import type { IAuthenticationService } from "@/application/services/authentication-service.interface";
describe("authContainer", () => {
beforeEach(() => {
authContainer.unbindAll();
authContainer.load(AuthModule);
});
afterEach(() => {
authContainer.unbindAll();
});
it("resolves IUsersRepository to MockUsersRepository by default", () => {
const repo = authContainer.get<IUsersRepository>(
AUTH_SYMBOLS.IUsersRepository,
);
expect(repo).toBeInstanceOf(MockUsersRepository);
});
it("resolves IAuthenticationService to MockAuthenticationService by default", () => {
const service = authContainer.get<IAuthenticationService>(
AUTH_SYMBOLS.IAuthenticationService,
);
expect(service).toBeInstanceOf(MockAuthenticationService);
});
it("authentication service receives users repository via constructor injection", async () => {
const service = authContainer.get<IAuthenticationService>(
AUTH_SYMBOLS.IAuthenticationService,
);
// The service should be able to validate against the seeded users
const { session, cookie } = await service.createSession({
id: "1",
username: "alice",
passwordHash: "hashed_password_alice",
});
expect(session.userId).toBe("1");
expect(cookie.value).toBe(session.id);
// After session creation, validateSession should resolve user via the repo
const validated = await service.validateSession(session.id);
expect(validated.user.username).toBe("alice");
});
});