51 lines
1.5 KiB
JavaScript
51 lines
1.5 KiB
JavaScript
import { UUID } from '../index.mjs';
|
|
import assert from 'node:assert';
|
|
|
|
describe('UUID', () => {
|
|
describe('#constructor()', () => {
|
|
it('should create UUID from string', () => {
|
|
const uuid_str = '123e4567-e89b-12d3-a456-426614171440';
|
|
const uuid = new UUID(uuid_str);
|
|
assert.strictEqual(uuid.toString(), uuid_str);
|
|
});
|
|
|
|
it('should create UUID from ArrayBuffer', () => {
|
|
const buffer = new Uint8Array([
|
|
18, 62, 69, 103, 232, 155, 18, 211,
|
|
164, 86, 66, 102, 20, 23, 20, 64
|
|
]).buffer;
|
|
const uuid = new UUID(buffer);
|
|
assert.strictEqual(uuid.toString(), '123e4567-e89b-12d3-a456-426614171440');
|
|
});
|
|
|
|
it('should throw error for invalid input type', () => {
|
|
assert.throws(() => {
|
|
new UUID({}); // passing invalid input
|
|
}, /^Error: Invalid input type/);
|
|
});
|
|
});
|
|
|
|
describe('#v4()', () => {
|
|
it('should generate a version 4 UUID', () => {
|
|
const uuid = UUID.v4();
|
|
assert.strictEqual(uuid.toString().length, 36);
|
|
});
|
|
});
|
|
|
|
describe('#v5()', () => {
|
|
it('should generate a version 5 UUID', () => {
|
|
const namespace = '123e4567-e89b-12d3-a456-426614171440'; // Sample namespace
|
|
const name = 'test';
|
|
const uuid = UUID.v5(name, namespace);
|
|
assert.strictEqual(uuid.toString().length, 36);
|
|
});
|
|
});
|
|
|
|
describe('#toString()', () => {
|
|
it('should convert UUID to string', () => {
|
|
const uuid_str = '123e4567-e89b-12d3-a456-426614174000';
|
|
const uuid = new UUID(uuid_str);
|
|
assert.strictEqual(uuid.toString(), uuid_str);
|
|
});
|
|
});
|
|
}); |