mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-04 01:44:13 +00:00
0c08a6dcb9
Register the public view route, align generated entities and test fixtures, and share the scoped reads and access switches. Keep gallery and notification writes in order, and submit the email subscription from the dialog footer.
122 lines
5.7 KiB
TypeScript
122 lines
5.7 KiB
TypeScript
/**
|
|
* `JourneyEntryCommentsRepository` (#1702): comments are read oldest first
|
|
* with their account's name, hidden ones only for the editors, and the share
|
|
* link only shows the ones under entries a reader can see. A deleted account
|
|
* leaves its comment as unknown; a deleted entry takes its comments along.
|
|
*/
|
|
import { JourneyEntries } from '../../../../src/db/entities/JourneyEntries.entity';
|
|
import { JourneyEntryComments } from '../../../../src/db/entities/JourneyEntryComments.entity';
|
|
import { Users } from '../../../../src/db/entities/Users.entity';
|
|
import type { JourneyEntryCommentsRepository } from '../../../../src/db/repositories/JourneyEntryComments.repository';
|
|
import { forEachInOrder } from '../../../../src/utils/for-each-in-order';
|
|
import { createSnapshotTestDb } from '../../../helpers/db-mock';
|
|
import { makeJourneyComment } from '../../../helpers/factories/journey-social';
|
|
import { makeJourney, makeJourneyEntry } from '../../../helpers/factories/journeys';
|
|
import { countRows, deleteRows, findRow } from '../../../helpers/factories/rows';
|
|
import { makeUser } from '../../../helpers/factories/users';
|
|
import { resetTestDb } from '../../../helpers/test-db';
|
|
import { createTestOrm, type TestOrm } from '../../../helpers/test-orm';
|
|
|
|
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';
|
|
|
|
const testDb = createSnapshotTestDb();
|
|
let t: TestOrm;
|
|
let comments: JourneyEntryCommentsRepository;
|
|
|
|
beforeAll(async () => {
|
|
t = await createTestOrm(testDb);
|
|
comments = t.repo(JourneyEntryComments);
|
|
});
|
|
beforeEach(() => {
|
|
resetTestDb(testDb);
|
|
t.clear();
|
|
});
|
|
afterAll(async () => {
|
|
await t.close();
|
|
testDb.close();
|
|
});
|
|
|
|
async function entryOf(overrides = {}) {
|
|
const { user } = await makeUser(t);
|
|
const journey = await makeJourney(t, user.id);
|
|
const entry = await makeJourneyEntry(t, journey.id, user.id, overrides);
|
|
return { user, journey, entry };
|
|
}
|
|
|
|
describe('JourneyEntryCommentsRepository', () => {
|
|
it('JCOMMENT-REPO-001: inserts a comment and reads it back with its author, oldest first', async () => {
|
|
const { user, journey, entry } = await entryOf();
|
|
const first = await comments.insertComment({
|
|
entry_id: entry.id,
|
|
journey_id: journey.id,
|
|
user_id: user.id,
|
|
guest_name: null,
|
|
body: 'First',
|
|
});
|
|
const second = await comments.insertComment({
|
|
entry_id: entry.id,
|
|
journey_id: journey.id,
|
|
user_id: null,
|
|
guest_name: 'Ana',
|
|
body: 'Second',
|
|
});
|
|
|
|
const rows = await comments.listForJourney(journey.id, false);
|
|
expect(rows.map((r) => r.id)).toEqual([first, second]);
|
|
expect(rows[0]).toMatchObject({ body: 'First', username: user.username, guest_name: null, hidden: 0 });
|
|
expect(rows[1]).toMatchObject({ body: 'Second', username: null, guest_name: 'Ana' });
|
|
expect(rows[0]?.created_at).toMatch(/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
|
|
expect(await comments.findWithAuthor(second)).toMatchObject({ id: second, guest_name: 'Ana', username: null });
|
|
expect(await comments.findWithAuthor(second + 1000)).toBeUndefined();
|
|
});
|
|
|
|
it('JCOMMENT-REPO-002: a hidden comment is read only when asked for, and shows again', async () => {
|
|
const { user, journey, entry } = await entryOf();
|
|
const comment = await makeJourneyComment(t, { id: entry.id, journey_id: journey.id }, user.id);
|
|
|
|
await comments.setHidden(comment.id, true);
|
|
expect(await comments.listForJourney(journey.id, false)).toEqual([]);
|
|
expect((await comments.listForJourney(journey.id, true)).map((r) => r.hidden)).toEqual([1]);
|
|
expect(await comments.findRef(comment.id)).toMatchObject({ id: comment.id, hidden: 1, user_id: user.id });
|
|
|
|
await comments.setHidden(comment.id, false);
|
|
expect((await comments.listForJourney(journey.id, false)).length).toBe(1);
|
|
});
|
|
|
|
it('JCOMMENT-REPO-003: the link shows no comment under a draft, a skeleton, a dismissed entry or hidden', async () => {
|
|
const { user, journey, entry } = await entryOf();
|
|
const draft = await makeJourneyEntry(t, journey.id, user.id, { is_draft: 1 });
|
|
const skeleton = await makeJourneyEntry(t, journey.id, user.id, { type: 'skeleton' });
|
|
const dismissed = await makeJourneyEntry(t, journey.id, user.id, { dismissed: 1 });
|
|
const shown = await makeJourneyComment(t, { id: entry.id, journey_id: journey.id }, null);
|
|
await makeJourneyComment(t, { id: entry.id, journey_id: journey.id }, user.id, { hidden: 1 });
|
|
await forEachInOrder([draft, skeleton, dismissed], async (e) => {
|
|
await makeJourneyComment(t, { id: e.id, journey_id: journey.id }, user.id);
|
|
});
|
|
|
|
expect((await comments.listPublicForJourney(journey.id)).map((r) => r.id)).toEqual([shown.id]);
|
|
});
|
|
|
|
it('JCOMMENT-REPO-004: a deleted account leaves its comment as unknown, a deleted entry takes its comments', async () => {
|
|
const { journey, entry } = await entryOf();
|
|
const { user: other } = await makeUser(t);
|
|
const comment = await makeJourneyComment(t, { id: entry.id, journey_id: journey.id }, other.id);
|
|
|
|
await deleteRows(t, Users, { id: other.id });
|
|
t.clear();
|
|
expect(await findRow(t, JourneyEntryComments, { id: comment.id })).toMatchObject({ user_id: null });
|
|
|
|
await deleteRows(t, JourneyEntries, { id: entry.id });
|
|
expect(await countRows(t, JourneyEntryComments, { id: comment.id })).toBe(0);
|
|
});
|
|
|
|
it('JCOMMENT-REPO-005: deletes one comment and leaves the rest', async () => {
|
|
const { user, journey, entry } = await entryOf();
|
|
const a = await makeJourneyComment(t, { id: entry.id, journey_id: journey.id }, user.id);
|
|
const b = await makeJourneyComment(t, { id: entry.id, journey_id: journey.id }, user.id);
|
|
await comments.deleteById(a.id);
|
|
expect((await comments.listForJourney(journey.id, true)).map((r) => r.id)).toEqual([b.id]);
|
|
expect(await comments.findRef(a.id)).toBeUndefined();
|
|
});
|
|
});
|