Skip to content

Commit

Permalink
fix: flaky tests, add InMemoryTimelineRepository tests
Browse files Browse the repository at this point in the history
  • Loading branch information
laminne committed Jul 16, 2024
1 parent 45415e7 commit 7fe5466
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 13 deletions.
99 changes: 99 additions & 0 deletions pkg/timeline/adaptor/repository/dummy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { Option, Result } from '@mikuroxina/mini-fn';
import { afterEach, describe, expect, it } from 'vitest';

import { type AccountID } from '../../../accounts/model/account.js';
import { Note, type NoteID } from '../../../notes/model/note.js';
import { InMemoryTimelineRepository } from './dummy.js';

describe('InMemoryTimelineRepository', () => {
const dummyPublicNote = Note.new({
id: '1' as NoteID,
authorID: '100' as AccountID,
content: 'Hello world',
contentsWarningComment: '',
createdAt: new Date('2023-09-10T00:00:00Z'),
originalNoteID: Option.none(),
attachmentFileID: [],
sendTo: Option.none(),
visibility: 'PUBLIC',
});
const dummyHomeNote = Note.new({
id: '2' as NoteID,
authorID: '100' as AccountID,
content: 'Hello world to Home',
contentsWarningComment: '',
createdAt: new Date('2023-09-11T00:00:00Z'),
originalNoteID: Option.none(),
attachmentFileID: [],
sendTo: Option.none(),
visibility: 'HOME',
});
const dummyFollowersNote = Note.new({
id: '3' as NoteID,
authorID: '100' as AccountID,
content: 'Hello world to followers',
contentsWarningComment: '',
createdAt: new Date('2023-09-12T00:00:00Z'),
originalNoteID: Option.none(),
attachmentFileID: [],
sendTo: Option.none(),
visibility: 'FOLLOWERS',
});
const dummyDirectNote = Note.new({
id: '4' as NoteID,
authorID: '100' as AccountID,
content: 'Hello world to direct',
contentsWarningComment: '',
createdAt: new Date('2023-09-13T00:00:00Z'),
originalNoteID: Option.none(),
attachmentFileID: [],
sendTo: Option.some('101' as AccountID),
visibility: 'DIRECT',
});

const repository = new InMemoryTimelineRepository([
dummyPublicNote,
dummyHomeNote,
dummyFollowersNote,
dummyDirectNote,
]);
afterEach(() => {
repository.reset([
dummyPublicNote,
dummyHomeNote,
dummyFollowersNote,
dummyDirectNote,
]);
});

it('Account Timeline only returns PUBLIC/HOME/FOLLOWERS notes', async () => {
const actual = await repository.getAccountTimeline('100' as AccountID, {
id: '100' as AccountID,
hasAttachment: true,
noNsfw: false,
});
expect(Result.isOk(actual)).toBe(true);
expect(Result.unwrap(actual).length).toBe(3);
expect(
Result.unwrap(actual)
.map((v) => v.getVisibility())
.includes('DIRECT'),
).toBe(false);
});
it('Home Timeline only returns PUBLIC/HOME/FOLLOWERS notes', async () => {
const actual = await repository.getHomeTimeline(
['1' as NoteID, '2' as NoteID, '3' as NoteID, '4' as NoteID],
{
hasAttachment: true,
noNsfw: false,
},
);
expect(Result.isOk(actual)).toBe(true);
expect(Result.unwrap(actual).length).toBe(3);
expect(
Result.unwrap(actual)
.map((v) => v.getVisibility())
.includes('DIRECT'),
).toBe(false);
});
});
30 changes: 21 additions & 9 deletions pkg/timeline/adaptor/repository/dummy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {
} from '../../model/repository.js';

export class InMemoryTimelineRepository implements TimelineRepository {
private readonly data: Map<NoteID, Note>;
private data: Map<NoteID, Note>;

constructor(data: readonly Note[] = []) {
this.data = new Map(data.map((v) => [v.getID(), v]));
Expand All @@ -23,15 +23,20 @@ export class InMemoryTimelineRepository implements TimelineRepository {
(note) => note[1].getAuthorID() === accountId,
);

// NOTE: filter DIRECT notes
const filtered = accountNotes.filter(
(note) => note[1].getVisibility() !== 'DIRECT',
);

// ToDo: filter hasAttachment, noNSFW
accountNotes.sort(
filtered.sort(
(a, b) => b[1].getCreatedAt().getTime() - a[1].getCreatedAt().getTime(),
);
const beforeIndex = filter.beforeId
? accountNotes.findIndex((note) => note[1].getID() === filter.beforeId)
: accountNotes.length - 1;
? filtered.findIndex((note) => note[1].getID() === filter.beforeId)

Check warning on line 36 in pkg/timeline/adaptor/repository/dummy.ts

View check run for this annotation

Codecov / codecov/patch

pkg/timeline/adaptor/repository/dummy.ts#L36

Added line #L36 was not covered by tests
: filtered.length;

return Result.ok(accountNotes.slice(0, beforeIndex).map((note) => note[1]));
return Result.ok(filtered.slice(0, beforeIndex).map((note) => note[1]));
}

async getHomeTimeline(
Expand All @@ -47,14 +52,21 @@ export class InMemoryTimelineRepository implements TimelineRepository {
notes.push(n);
}

// NOTE: filter DIRECT notes
const filtered = notes.filter((note) => note.getVisibility() !== 'DIRECT');
// ToDo: filter hasAttachment, noNSFW
notes.sort(
filtered.sort(
(a, b) => b.getCreatedAt().getTime() - a.getCreatedAt().getTime(),
);
const beforeIndex = filter.beforeId
? notes.findIndex((note) => note.getID() === filter.beforeId)
: notes.length;
? filtered.findIndex((note) => note.getID() === filter.beforeId)

Check warning on line 62 in pkg/timeline/adaptor/repository/dummy.ts

View check run for this annotation

Codecov / codecov/patch

pkg/timeline/adaptor/repository/dummy.ts#L62

Added line #L62 was not covered by tests
: filtered.length;

return Result.ok(notes.slice(0, beforeIndex));
return Result.ok(filtered.slice(0, beforeIndex));
}

reset(data: readonly Note[] = []) {
this.data.clear();
this.data = new Map(data.map((v) => [v.getID(), v]));
}
}
8 changes: 4 additions & 4 deletions pkg/timeline/service/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('AccountTimelineService', () => {
authorID: '100' as AccountID,
content: 'Hello world',
contentsWarningComment: '',
createdAt: new Date(),
createdAt: new Date('2023-09-10T00:00:00Z'),
originalNoteID: Option.none(),
attachmentFileID: [],
sendTo: Option.none(),
Expand All @@ -31,7 +31,7 @@ describe('AccountTimelineService', () => {
authorID: '100' as AccountID,
content: 'Hello world to Home',
contentsWarningComment: '',
createdAt: new Date(),
createdAt: new Date('2023-09-11T00:00:00Z'),
originalNoteID: Option.none(),
attachmentFileID: [],
sendTo: Option.none(),
Expand All @@ -42,7 +42,7 @@ describe('AccountTimelineService', () => {
authorID: '100' as AccountID,
content: 'Hello world to followers',
contentsWarningComment: '',
createdAt: new Date(),
createdAt: new Date('2023-09-12T00:00:00Z'),
originalNoteID: Option.none(),
attachmentFileID: [],
sendTo: Option.none(),
Expand All @@ -53,7 +53,7 @@ describe('AccountTimelineService', () => {
authorID: '100' as AccountID,
content: 'Hello world to direct',
contentsWarningComment: '',
createdAt: new Date(),
createdAt: new Date('2023-09-13T00:00:00Z'),
originalNoteID: Option.none(),
attachmentFileID: [],
sendTo: Option.some('101' as AccountID),
Expand Down

0 comments on commit 7fe5466

Please sign in to comment.