Skip to content

Commit

Permalink
Merge branch 'main' into replace-eslint-and-prettier-to-biome
Browse files Browse the repository at this point in the history
  • Loading branch information
m1sk9 committed Jul 20, 2024
2 parents 1d29f95 + 0851aec commit 1e6f5be
Show file tree
Hide file tree
Showing 4 changed files with 862 additions and 210 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
},
"dependencies": {
"@hono/node-server": "^1.7.0",
"@hono/swagger-ui": "^0.2.1",
"@hono/swagger-ui": "^0.4.0",
"@hono/zod-openapi": "^0.14.0",
"@mikuroxina/mini-fn": "^6.3.1",
"@prisma/client": "5.16.2",
"@prisma/client": "5.17.0",
"@scalar/hono-api-reference": "^0.5.0",
"argon2": "^0.40.0",
"blurhash": "^2.0.5",
Expand Down
85 changes: 85 additions & 0 deletions pkg/timeline/model/list.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { describe, expect, it } from 'vitest';

import type { AccountID } from '../../accounts/model/account.js';
import { type CreateListArgs, List, type ListID } from './list.js';

describe('List', () => {
const args: CreateListArgs = {
id: '1' as ListID,
title: 'My List',
publicity: 'PUBLIC',
ownerId: '2' as AccountID,
memberIds: ['3' as AccountID],
createdAt: new Date(),
} as const;

it('should create a new list', () => {
const list = List.new(args);

expect(list.getId()).toBe(args.id);
expect(list.getTitle()).toBe(args.title);
expect(list.isPublic()).toBe(true);
expect(list.getOwnerId()).toBe(args.ownerId);
expect(list.getMemberIds()).toEqual(args.memberIds);
expect(list.getCreatedAt()).toBe(args.createdAt);
});

it('should add a member to the list', () => {
const list = List.new(args);
const memberId = '4' as AccountID;

list.addMember(memberId);

expect(list.getMemberIds()).toStrictEqual([
'3' as AccountID,
'4' as AccountID,
]);
});

it('should not add a member if already in the list', () => {
const args: CreateListArgs = {
id: '1' as ListID,
title: 'My List',
publicity: 'PUBLIC',
ownerId: '2' as AccountID,
memberIds: ['3' as AccountID],
createdAt: new Date(),
} as const;
const list3 = List.new(args);
const memberId = '3' as AccountID;

list3.addMember(memberId);

expect(list3.getMemberIds()).toStrictEqual(['3' as AccountID]);
});

it('should remove member from list', () => {
const args: CreateListArgs = {
id: '1' as ListID,
title: 'My List',
publicity: 'PUBLIC',
ownerId: '2' as AccountID,
memberIds: ['3' as AccountID],
createdAt: new Date(),
} as const;
const list3 = List.new(args);
const memberId = '3' as AccountID;

list3.removeMember(memberId);

expect(list3.getMemberIds()).toStrictEqual([]);
});

it('should no duplicate member when initialize', () => {
const args: CreateListArgs = {
id: '1' as ListID,
title: 'My List',
publicity: 'PUBLIC',
ownerId: '2' as AccountID,
memberIds: ['3' as AccountID, '3' as AccountID],
createdAt: new Date(),
} as const;
const list3 = List.new(args);
expect(list3.getMemberIds()).toStrictEqual(['3' as AccountID]);
});
});
67 changes: 67 additions & 0 deletions pkg/timeline/model/list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import type { AccountID } from '../../accounts/model/account.js';
import type { ID } from '../../id/type.js';

export type ListID = ID<List>;
export type CreateListArgs = Readonly<{
id: ListID;
title: string;
publicity: 'PUBLIC' | 'PRIVATE';
ownerId: AccountID;
memberIds: readonly AccountID[];
createdAt: Date;
}>;

export class List {
private readonly id: ListID;
private readonly title: string;
private readonly publicity: 'PUBLIC' | 'PRIVATE';
private readonly ownerId: AccountID;
private readonly memberIds: Set<AccountID>;
private readonly createdAt: Date;

private constructor(args: CreateListArgs) {
this.id = args.id;
this.title = args.title;
this.publicity = args.publicity;
this.ownerId = args.ownerId;
this.memberIds = new Set<AccountID>(args.memberIds);
this.createdAt = args.createdAt;
}

static new(args: CreateListArgs) {
return new List(args);
}

getId(): ListID {
return this.id;
}

getTitle(): string {
return this.title;
}

isPublic(): boolean {
return this.publicity === 'PUBLIC';
}

getOwnerId(): AccountID {
return this.ownerId;
}

getMemberIds(): readonly AccountID[] {
return [...this.memberIds];
}

getCreatedAt(): Date {
return this.createdAt;
}

addMember(memberId: AccountID): void {
// ToDo: member limit
this.memberIds.add(memberId);
}

removeMember(memberId: AccountID): void {
this.memberIds.delete(memberId);
}
}
Loading

0 comments on commit 1e6f5be

Please sign in to comment.