99 lines
4.1 KiB
TypeScript
99 lines
4.1 KiB
TypeScript
import { http, HttpResponse } from 'msw';
|
|
import type { MediaCleanupPreviewDto, MediaLibraryDto, MediaPhotoDto, MediaStorageDto, MediaTrashRequest, MediaTrashResponse } from '@/features/media/services/types';
|
|
|
|
const daysAgo = (days: number) => new Date(Date.now() - days * 86_400_000).toISOString();
|
|
|
|
export const seedPhotos = (): MediaPhotoDto[] => [
|
|
{
|
|
id: 'hero:11111111-1111-1111-1111-111111111111',
|
|
module: 'hero',
|
|
bytes: 2_400_000,
|
|
width: 3000,
|
|
height: 2000,
|
|
createdAt: daysAgo(1),
|
|
thumbnailUrl: '/media/hero/0123456789abcdef/wide-480.webp',
|
|
uses: [{ module: 'hero', kind: 'slide', position: 1, active: true }],
|
|
originalFileName: 'zomer.jpg',
|
|
unusedSince: null,
|
|
deletedAt: null,
|
|
},
|
|
{
|
|
id: 'hero:22222222-2222-2222-2222-222222222222',
|
|
module: 'hero',
|
|
bytes: 1_800_000,
|
|
width: 2400,
|
|
height: 1600,
|
|
createdAt: daysAgo(12),
|
|
thumbnailUrl: null,
|
|
uses: [],
|
|
originalFileName: null,
|
|
unusedSince: null,
|
|
deletedAt: null,
|
|
},
|
|
{
|
|
id: 'hero:33333333-3333-3333-3333-333333333333',
|
|
module: 'hero',
|
|
bytes: 900_000,
|
|
width: 1600,
|
|
height: 1067,
|
|
createdAt: daysAgo(30),
|
|
thumbnailUrl: null,
|
|
uses: [{ module: 'hero', kind: 'slide', position: 2, active: false }],
|
|
originalFileName: null,
|
|
unusedSince: null,
|
|
deletedAt: null,
|
|
},
|
|
];
|
|
|
|
export const seedStorage = (): MediaStorageDto => ({ usedBytes: 100_000_000, limitBytes: 2_147_483_648, level: 'ok', warningPercent: 80 });
|
|
|
|
/** Stateless mocks of trash and restore: a photo in use is refused without the second confirmation. */
|
|
export const mediaTrashHandlers = [
|
|
http.get('*/media-library/trash', () => HttpResponse.json({ photos: [], modules: ['hero'] } satisfies MediaLibraryDto)),
|
|
http.post('*/media-library/delete', async ({ request }) => {
|
|
const body = (await request.json()) as MediaTrashRequest;
|
|
const photos = seedPhotos();
|
|
return HttpResponse.json({
|
|
results: body.ids.map((id) => {
|
|
const photo = photos.find((p) => p.id === id);
|
|
if (!photo) return { id, status: 'notFound', uses: [] };
|
|
if (photo.uses.length > 0 && !body.confirmedInUse) return { id, status: 'inUse', uses: photo.uses };
|
|
return { id, status: 'trashed', uses: photo.uses };
|
|
}),
|
|
} satisfies MediaTrashResponse);
|
|
}),
|
|
http.post('*/media-library/restore', async ({ request }) => {
|
|
const body = (await request.json()) as { ids: string[] };
|
|
return HttpResponse.json({ results: body.ids.map((id) => ({ id, status: 'restored', uses: [] })) } satisfies MediaTrashResponse);
|
|
}),
|
|
];
|
|
|
|
export const mediaPurgeHandlers = [
|
|
http.post('*/media-library/purge', async ({ request }) => {
|
|
const body = (await request.json()) as { ids?: string[]; all?: boolean };
|
|
const ids = body.all ? ['hero:all'] : (body.ids ?? []);
|
|
return HttpResponse.json({ results: ids.map((id) => ({ id, status: 'purged', uses: [] })) } satisfies MediaTrashResponse);
|
|
}),
|
|
];
|
|
|
|
export const mediaHandlers = [
|
|
...mediaPurgeHandlers,
|
|
...mediaTrashHandlers,
|
|
http.get('*/media-library/photos', () => HttpResponse.json({ photos: seedPhotos(), modules: ['hero'] } satisfies MediaLibraryDto)),
|
|
http.get('*/media-library/storage', () => HttpResponse.json(seedStorage())),
|
|
http.get('*/media-library/cleanup-preview', ({ request }) => {
|
|
const url = new URL(request.url);
|
|
const days = Number(url.searchParams.get('days') ?? 7);
|
|
const onlyUnused = url.searchParams.get('onlyUnused') !== 'false';
|
|
const cutoff = Date.now() - days * 86_400_000;
|
|
const photos = seedPhotos().filter((p) => new Date(p.createdAt).getTime() < cutoff && (!onlyUnused || p.uses.length === 0));
|
|
return HttpResponse.json({
|
|
days,
|
|
onlyUnused,
|
|
count: photos.length,
|
|
bytes: photos.reduce((sum, p) => sum + p.bytes, 0),
|
|
photos,
|
|
} satisfies MediaCleanupPreviewDto);
|
|
}),
|
|
];
|