summaryrefslogtreecommitdiff
path: root/std/uuid/v5_test.ts
blob: d24da36b577afb05a6166f646f5c5baed42d7c3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { assert, assertEquals } from "../testing/asserts.ts";
import { generate, validate } from "./v5.ts";

const NAMESPACE = "1b671a64-40d5-491e-99b0-da01ff1f3341";

Deno.test({
  name: "[UUID] test_uuid_v5",
  fn(): void {
    const u = generate({ value: "", namespace: NAMESPACE });
    assertEquals(typeof u, "string", "returns a string");
    assert(u !== "", "return string is not empty");
  },
});

Deno.test({
  name: "[UUID] test_uuid_v5_format",
  fn(): void {
    for (let i = 0; i < 10000; i++) {
      const u = generate({ value: String(i), namespace: NAMESPACE }) as string;
      assert(validate(u), `${u} is not a valid uuid v5`);
    }
  },
});

Deno.test({
  name: "[UUID] test_uuid_v5_option",
  fn(): void {
    const v5Options = {
      value: "Hello, World",
      namespace: NAMESPACE,
    };
    const u = generate(v5Options);
    assertEquals(u, "4b4f2adc-5b27-57b5-8e3a-c4c4bcf94f05");
  },
});

Deno.test({
  name: "[UUID] test_uuid_v5_buf_offset",
  fn(): void {
    const buf = [
      75,
      79,
      42,
      220,
      91,
      39,
      87,
      181,
      142,
      58,
      196,
      196,
      188,
      249,
      79,
      5,
    ];
    const origin = JSON.parse(JSON.stringify(buf));
    generate({ value: "Hello, World", namespace: NAMESPACE }, buf);
    assertEquals(origin, buf);

    generate({ value: "Hello, World", namespace: NAMESPACE }, buf, 3);
    assertEquals(origin.slice(0, 3), buf.slice(0, 3));
    assertEquals(origin, buf.slice(3));
  },
});

Deno.test({
  name: "[UUID] is_valid_uuid_v5",
  fn(): void {
    const u = generate({
      value: "Hello, World",
      namespace: "1b671a64-40d5-491e-99b0-da01ff1f3341",
    }) as string;
    const t = "4b4f2adc-5b27-57b5-8e3a-c4c4bcf94f05";
    const n = "4b4f2adc-5b27-17b5-8e3a-c4c4bcf94f05";

    assert(validate(u), `generated ${u} should be valid`);
    assert(validate(t), `${t} should be valid`);
    assert(!validate(n), `${n} should not be valid`);
  },
});