deno.land / std@0.224.0 / uuid / v1_test.ts
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.import { assert, assertEquals, assertThrows } from "../assert/mod.ts";import { generate, validate } from "./v1.ts";import { uuidToBytes } from "./_common.ts";
Deno.test("validate() checks if a string is a valid v1 UUID", () => { const u = generate(); const t = "63655efa-7ee6-11ea-bc55-0242ac130003"; const n = "63655efa-7ee6-11eg-bc55-0242ac130003";
assert(validate(u as string), `generated ${u} should be valid`); assert(validate(t), `${t} should be valid`); assert(!validate(n), `${n} should not be valid`);});
Deno.test("generate() generates a non-empty string", () => { const u1 = generate(); const u2 = generate({ msecs: new Date("2011-11-01").getTime(), nsecs: 10000, });
assertEquals(typeof u1, "string", "returns a string"); assert(u1 !== "", "return string is not empty"); assertEquals(typeof u2, "string", "returns a string"); assert(u2 !== "", "return string is not empty");});
Deno.test("generate() generates UUIDs in version 1 format", () => { for (let i = 0; i < 10000; i++) { const u = generate() as string; assert(validate(u), `${u} is not a valid uuid v1`); }});
Deno.test("generate() can generate a static v1 UUID", () => { const v1options = { node: [0x01, 0x23, 0x45, 0x67, 0x89, 0xab], clockseq: 0x1234, msecs: new Date("2011-11-01").getTime(), nsecs: 5678, }; const u = generate(v1options); assertEquals(u, "710b962e-041c-11e1-9234-0123456789ab");});
Deno.test("generate() can fill the UUID into a buffer", () => { const buf: number[] = []; const v1options = { node: [0x01, 0x23, 0x45, 0x67, 0x89, 0xab], clockseq: 0x1234, msecs: new Date("2011-11-01").getTime(), nsecs: 5678, }; const uuid = generate(v1options, buf, 0); const expect = uuidToBytes("710b962e-041c-11e1-9234-0123456789ab");
assertEquals(buf, expect); assertEquals(buf, uuid);});
Deno.test("generate() throws when node is passed with less than 6 numbers", () => { assertThrows( () => { generate({ node: [0x01, 0x23, 0x45, 0x67, 0x89] }); }, Error, "Cannot create UUID. The node option must be an array of 6 bytes", );});
Deno.test("generate() throws when node is passed with more than 6 numbers", () => { assertThrows( () => { generate({ node: [0x01, 0x23, 0x45, 0x67, 0x89, 0x89, 0x89] }); }, Error, "Cannot create UUID. The node option must be an array of 6 bytes", );});
Deno.test("generate() throws when create more than 10M uuids/sec", () => { assertThrows( () => { generate({ nsecs: 10001 }); }, Error, "Can't create more than 10M uuids/sec", );});
Version Info