diff --git a/core/Account/User.test.ts b/core/Account/User.test.ts new file mode 100644 index 0000000..03aa257 --- /dev/null +++ b/core/Account/User.test.ts @@ -0,0 +1,32 @@ +import { IAccountRepository } from "infrastructure"; +import { Avatar, Race } from "./Avatar"; +import { Player, User } from "./User"; + +describe("User's functionality", () => { + test("password hashing", () => { + const cases = [ + { raw: "password", hash: "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8" }, + { raw: "cello", hash: "9bbf02efd82322aadc5d06c9bcf35bb4b0e3302ca158dc800407be1a4fea67e2" }, + ]; + + cases.forEach((c) => { + expect(User.hashPassword(c.raw)).toBe(c.hash.toLowerCase()); + }); + }); +}); + +describe("Player's functionality", () => { + test("player level", () => { + [ + { exp: 0, lv: 0 }, + { exp: 1250, lv: 5 }, + { exp: 5000, lv: 10 }, + ].forEach(async (c) => { + const repoMock = jest.fn(() => new Avatar(Race.Egg, c.exp)); + const p = await Player.New({ getAvatar: repoMock } as unknown as IAccountRepository, "", ""); + const lv = p.level; + expect(repoMock).toBeCalledTimes(1); + expect(lv).toBe(c.lv); + }); + }); +}); diff --git a/core/Account/User.ts b/core/Account/User.ts index 2ef620a..f961fc6 100644 --- a/core/Account/User.ts +++ b/core/Account/User.ts @@ -60,21 +60,22 @@ export class LoginOptions { } export class Player extends User { - private _avatar?: Avatar; - - public get avatar(): Avatar { - return (this._avatar = this._avatar ?? this.repo.getAvatar(this.accountId)); - } - - public set avatar(value: Avatar) { - this._avatar = value; - } + public avatar!: Avatar; public get level(): number { return this.avatar.level; } + /** + * プレイヤーをインスタンス化する場合は`New`を使ってください。 + */ constructor(repo: IAccountRepository, accountId: string, email: string) { super(repo, accountId, email); } + + public static async New(repo: IAccountRepository, accountId: string, email: string): Promise { + const p = new Player(repo, accountId, email); + p.avatar = await p.repo.getAvatar(p.accountId); + return p; + } }