From bb46f48d3b0e0965f6551a5a1a57d6db5973fd46 Mon Sep 17 00:00:00 2001 From: Tatsuto YAMAMOTO Date: Sat, 9 Sep 2023 23:34:53 +0900 Subject: [PATCH] =?UTF-8?q?feat(test):=20=E3=82=B5=E3=83=BC=E3=83=90?= =?UTF-8?q?=E3=83=BC=E3=81=AE=E3=83=90=E3=83=AA=E3=83=87=E3=83=BC=E3=82=B7?= =?UTF-8?q?=E3=83=A7=E3=83=B3=E3=83=86=E3=82=B9=E3=83=88=E3=81=AE=E5=AE=9F?= =?UTF-8?q?=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/domain/server.test.ts | 116 ++++++++++++++++++++++++++++++++++++++ src/domain/server.ts | 47 +++++++++++++++ 2 files changed, 163 insertions(+) create mode 100644 src/domain/server.test.ts diff --git a/src/domain/server.test.ts b/src/domain/server.test.ts new file mode 100644 index 0000000..65274ce --- /dev/null +++ b/src/domain/server.test.ts @@ -0,0 +1,116 @@ +import { describe, expect, it } from "vitest"; +import { Server, ServerArgs } from "./server.js"; +import { Snowflake } from "../helpers/id_generator.js"; + +describe("Server", () => { + const defaultServerArgs: ServerArgs = { + id: "10101294855225344" as Snowflake, + host: "social.example.jp", + softwareName: "Mastodon", + softwareVersion: "4.0.0", + name: "example social", + description: "this is example social", + maintainer: "example maintainer", + maintainerEmail: "johndoe@example.jp", + iconURL: "https://social.example.jp/icon.png", + faviconURL: "https://social.example.jp/favicon.png", + }; + + it("128文字以上のホスト名は設定できない", () => { + expect(() => { + new Server({ + ...defaultServerArgs, + host: "a".repeat(128 + 1), + }); + }).toThrowError(new Error("failed to create server: host is too long")); + }); + + it("4文字以下のホスト名は設定できない", () => { + expect(() => { + new Server({ + ...defaultServerArgs, + host: "a.b", + }); + }).toThrowError(new Error("failed to create server: host is too short")); + }); + + it("ホスト名を設定できる", () => { + expect(() => { + new Server({ + ...defaultServerArgs, + }); + }).toBeTruthy(); + }); + + it("128文字以上のソフトウェア名は設定できない", () => { + expect(() => { + new Server({ + ...defaultServerArgs, + softwareName: "a".repeat(128 + 1), + }); + }).toThrowError( + new Error("failed to create server: softwareName is too long"), + ); + }); + + it("ソフトウェア名を設定できる", () => { + expect(() => { + new Server({ + ...defaultServerArgs, + }); + }).toBeTruthy(); + }); + + it("128文字以上のソフトウェアバージョンは設定できない", () => { + expect(() => { + new Server({ + ...defaultServerArgs, + softwareVersion: "a".repeat(128 + 1), + }); + }).toThrowError( + new Error("failed to create server: softwareVersion is too long"), + ); + }); + + it("128文字以上のサーバー名は設定できない", () => { + expect(() => { + new Server({ + ...defaultServerArgs, + name: "a".repeat(128 + 1), + }); + }).toThrowError(new Error("failed to create server: name is too long")); + }); + + it("3000文字以上のサーバー説明文は設定できない", () => { + expect(() => { + new Server({ + ...defaultServerArgs, + description: "a".repeat(3000 + 1), + }); + }).toThrowError( + new Error("failed to create server: description is too long"), + ); + }); + + it("256文字以上の管理者名は設定できない", () => { + expect(() => { + new Server({ + ...defaultServerArgs, + maintainer: "a".repeat(256 + 1), + }); + }).toThrowError( + new Error("failed to create server: maintainer is too long"), + ); + }); + + it("256文字以上の管理者メールアドレスは設定できない", () => { + expect(() => { + new Server({ + ...defaultServerArgs, + maintainerEmail: "a".repeat(256 + 1), + }); + }).toThrowError( + new Error("failed to create server: maintainerEmail is too long"), + ); + }); +}); diff --git a/src/domain/server.ts b/src/domain/server.ts index af57074..12a2c61 100644 --- a/src/domain/server.ts +++ b/src/domain/server.ts @@ -1,5 +1,18 @@ import { Snowflake } from "../helpers/id_generator.js"; +export interface ServerArgs { + id: Snowflake; + host: string; + softwareName: string; + softwareVersion: string; + name: string; + description: string; + maintainer: string; + maintainerEmail: string; + iconURL: string; + faviconURL: string; +} + export class Server { get softwareName(): string { return this._softwareName; @@ -105,6 +118,8 @@ export class Server { iconURL: string; faviconURL: string; }) { + this.validate(args); + this._id = args.id; this._host = args.host; this._softwareName = args.softwareName; @@ -116,4 +131,36 @@ export class Server { this._iconURL = args.iconURL; this._faviconURL = args.faviconURL; } + + private validate(args: ServerArgs) { + if ([...args.host].length > 128) { + throw new Error("failed to create server: host is too long"); + } else if ([...args.host].length < 4) { + throw new Error("failed to create server: host is too short"); + } + + if ([...args.softwareName].length > 128) { + throw new Error("failed to create server: softwareName is too long"); + } + + if ([...args.softwareVersion].length > 128) { + throw new Error("failed to create server: softwareVersion is too long"); + } + + if ([...args.name].length > 128) { + throw new Error("failed to create server: name is too long"); + } + + if ([...args.description].length > 3000) { + throw new Error("failed to create server: description is too long"); + } + + if ([...args.maintainer].length > 256) { + throw new Error("failed to create server: maintainer is too long"); + } + + if ([...args.maintainerEmail].length > 256) { + throw new Error("failed to create server: maintainerEmail is too long"); + } + } }