From 17ba73b11306aa21c2c1ed8824f224acb8ca6709 Mon Sep 17 00:00:00 2001 From: Tatsuto YAMAMOTO Date: Sun, 10 Sep 2023 00:19:40 +0900 Subject: [PATCH] =?UTF-8?q?feat(test):=20Post=E3=81=AE=E3=83=90=E3=83=AA?= =?UTF-8?q?=E3=83=87=E3=83=BC=E3=82=B7=E3=83=A7=E3=83=B3=E3=83=86=E3=82=B9?= =?UTF-8?q?=E3=83=88=E3=81=AE=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/domain/post.test.ts | 38 ++++++++++++++++++++++++++++++++++++++ src/domain/post.ts | 19 +++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 src/domain/post.test.ts diff --git a/src/domain/post.test.ts b/src/domain/post.test.ts new file mode 100644 index 0000000..9431915 --- /dev/null +++ b/src/domain/post.test.ts @@ -0,0 +1,38 @@ +import { describe, expect, it } from "vitest"; +import { Post, PostArgs } from "./post.js"; +import { Snowflake } from "../helpers/id_generator.js"; +import { Media } from "./media.js"; + +describe("Post", () => { + const defaultPostArgs: PostArgs = { + id: "10101450391945216" as Snowflake, + authorID: "10101451896913920" as Snowflake, + text: "Hello, world!", + visibility: 0, + createdAt: new Date(), + attachments: [], + reactions: [], + }; + + it("添付ファイルは16個以上添付できない", () => { + expect(() => { + new Post({ + ...defaultPostArgs, + attachments: new Array(17).fill({} as Media), + }); + }).toThrowError( + new Error( + "failed to create post: The number of attachments must be less than 16.", + ), + ); + }); + + it("ファイルを添付できる", () => { + expect(() => { + new Post({ + ...defaultPostArgs, + attachments: new Array(10).fill({} as Media), + }); + }).toBeTruthy(); + }); +}); diff --git a/src/domain/post.ts b/src/domain/post.ts index 46bf32f..c949194 100644 --- a/src/domain/post.ts +++ b/src/domain/post.ts @@ -2,6 +2,16 @@ import { Snowflake } from "../helpers/id_generator.js"; import { User } from "./user.js"; import { Media } from "./media.js"; +export interface PostArgs { + id: Snowflake; + authorID: Snowflake; + text: string; + visibility: number; + createdAt: Date; + attachments: Array; + reactions: Array; +} + export class Post { get visibility(): number { return this._visibility; @@ -54,6 +64,7 @@ export class Post { attachments: Array; reactions: Array; }) { + this.validate(args); this._id = args.id; this._authorID = args.authorID; @@ -70,6 +81,14 @@ export class Post { } return text; } + + private validate(args: PostArgs) { + if (args.attachments.length > 16) { + throw new Error( + "failed to create post: The number of attachments must be less than 16.", + ); + } + } } export class PostReactionEvent {