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 {