summaryrefslogtreecommitdiff
path: root/Homework/cs5260/consumer/t/S3WidgetKeyGenerator.spec.ts
blob: 642c7a4b6564b238753dad70fea6cb3b3321fa70 (plain) (blame)
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
import { expect, test } from "bun:test";
import { S3WidgetKeyGenerator } from "@/consumer/dao/keys";
import { Widget } from "@/shared/schema";

test("builds keys correctly", () => {
  const generator = new S3WidgetKeyGenerator()
    .withPrefix("test")
    .withWidgetId("test")
    .withOwner("simponic");

  expect(generator.build()).toEqual("test/simponic/test");
});

test("when owner given spaces then replaces with dashes and lowercases", () => {
  const generator = new S3WidgetKeyGenerator()
    .withPrefix("widgets")
    .withWidgetId("test")
    .withOwner("simPOnic test");

  expect(generator.build()).toEqual("widgets/simponic-test/test");
});

test("when not all specifications given then throws", () => {
  const generator = new S3WidgetKeyGenerator()
    .withPrefix("test")
    .withWidgetId("test");

  expect(() => generator.build()).toThrow();
});

test("when invalid keys given then throws", () => {
  const generator = new S3WidgetKeyGenerator()
    .withPrefix("test")
    .withOwner("simponic/simponic")
    .withWidgetId("test");

  expect(() => generator.build()).toThrow();
});

test("when keys correct then builds from widget", () => {
  const generator = new S3WidgetKeyGenerator().withPrefix("test-widgets");

  const testWidget = Widget.parse({ owner: "simponic", id: "test-id" });

  expect(generator.fromWidget(testWidget)).toEqual(
    "test-widgets/simponic/test-id"
  );
});