summaryrefslogtreecommitdiff
path: root/Homework/cs5260/consumer/t/S3WidgetKeyGenerator.spec.ts
diff options
context:
space:
mode:
Diffstat (limited to 'Homework/cs5260/consumer/t/S3WidgetKeyGenerator.spec.ts')
-rw-r--r--Homework/cs5260/consumer/t/S3WidgetKeyGenerator.spec.ts48
1 files changed, 48 insertions, 0 deletions
diff --git a/Homework/cs5260/consumer/t/S3WidgetKeyGenerator.spec.ts b/Homework/cs5260/consumer/t/S3WidgetKeyGenerator.spec.ts
new file mode 100644
index 0000000..642c7a4
--- /dev/null
+++ b/Homework/cs5260/consumer/t/S3WidgetKeyGenerator.spec.ts
@@ -0,0 +1,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"
+ );
+});