2025-05-09 10:45:32 +00:00
|
|
|
import { UniqueID } from "../unique-id";
|
2025-01-30 10:45:31 +00:00
|
|
|
|
2025-02-04 18:25:10 +00:00
|
|
|
// Mock UUID generation to ensure predictable tests
|
|
|
|
|
jest.mock("uuid", () => ({ v4: () => "123e4567-e89b-12d3-a456-426614174000" }));
|
|
|
|
|
|
|
|
|
|
describe("UniqueID", () => {
|
|
|
|
|
test("should create a UniqueID with a valid UUID", () => {
|
|
|
|
|
const id = "123e4567-e89b-12d3-a456-426614174000";
|
|
|
|
|
const result = UniqueID.create(id);
|
2025-01-30 10:45:31 +00:00
|
|
|
|
2025-02-03 13:12:36 +00:00
|
|
|
expect(result.isSuccess).toBe(true);
|
2025-02-24 19:00:28 +00:00
|
|
|
expect(result.data.toString()).toBe(id);
|
2025-01-30 10:45:31 +00:00
|
|
|
});
|
|
|
|
|
|
2025-02-25 15:25:30 +00:00
|
|
|
test("should generate a UniqueID with a valid UUID", () => {
|
|
|
|
|
const result = UniqueID.generate();
|
|
|
|
|
|
|
|
|
|
expect(result.isSuccess).toBe(true);
|
|
|
|
|
expect(result.data.toString()).toBeTruthy();
|
|
|
|
|
});
|
|
|
|
|
|
2025-02-04 18:25:10 +00:00
|
|
|
test("should fail to create UniqueID with an invalid UUID", () => {
|
2025-01-30 10:45:31 +00:00
|
|
|
const result = UniqueID.create("invalid-uuid");
|
|
|
|
|
|
2025-02-04 18:25:10 +00:00
|
|
|
expect(result.isFailure).toBe(true);
|
|
|
|
|
});
|
|
|
|
|
|
2025-02-24 19:00:28 +00:00
|
|
|
test("should fail when id is undefined and generateOnEmpty is false", () => {
|
2025-02-04 18:25:10 +00:00
|
|
|
const result = UniqueID.create(undefined, false);
|
|
|
|
|
|
2025-02-24 19:00:28 +00:00
|
|
|
expect(result.isFailure).toBe(true);
|
2025-02-04 18:25:10 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("should generate a new UUID when id is undefined and generateOnEmpty is true", () => {
|
|
|
|
|
const result = UniqueID.create(undefined, true);
|
|
|
|
|
|
|
|
|
|
expect(result.isSuccess).toBe(true);
|
2025-02-24 19:00:28 +00:00
|
|
|
expect(result.data?.toString()).toBeTruthy();
|
2025-02-04 18:25:10 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("should fail when id is null", () => {
|
|
|
|
|
const result = UniqueID.create(null as any);
|
|
|
|
|
|
|
|
|
|
expect(result.isFailure).toBe(true);
|
2025-01-30 10:45:31 +00:00
|
|
|
});
|
|
|
|
|
|
2025-02-04 18:25:10 +00:00
|
|
|
test("should create a UniqueID when id is an empty string and generateOnEmpty is true", () => {
|
|
|
|
|
const result = UniqueID.create(" ", true);
|
2025-01-30 10:45:31 +00:00
|
|
|
|
2025-02-03 13:12:36 +00:00
|
|
|
expect(result.isSuccess).toBe(true);
|
2025-02-24 19:00:28 +00:00
|
|
|
expect(result.data?.toString()).toBeTruthy();
|
2025-01-30 10:45:31 +00:00
|
|
|
});
|
|
|
|
|
|
2025-02-24 19:00:28 +00:00
|
|
|
test("should fail when id is an empty string and generateOnEmpty is false", () => {
|
2025-02-04 18:25:10 +00:00
|
|
|
const result = UniqueID.create(" ", false);
|
2025-01-30 10:45:31 +00:00
|
|
|
|
2025-02-24 19:00:28 +00:00
|
|
|
expect(result.isFailure).toBe(true);
|
2025-01-30 10:45:31 +00:00
|
|
|
});
|
|
|
|
|
});
|