test(geschichten): add failing tests for draft fetch in page loader
RED: loader does not yet call parent() or fetch DRAFT stories. Also extracts settled<T>() helper to $lib/shared/server/settled.ts and seeds makeData/callLoad factories with drafts/parent defaults. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
5
frontend/src/lib/shared/server/settled.ts
Normal file
5
frontend/src/lib/shared/server/settled.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
export function settled<T>(res: PromiseSettledResult<unknown> | undefined): T | null {
|
||||
if (res?.status !== 'fulfilled') return null;
|
||||
const v = res.value as { response: Response; data: unknown };
|
||||
return v.response.ok ? ((v.data as T) ?? null) : null;
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { redirect } from '@sveltejs/kit';
|
||||
import { createApiClient } from '$lib/shared/api.server';
|
||||
import type { components } from '$lib/generated/api';
|
||||
import { settled } from '$lib/shared/server/settled';
|
||||
|
||||
type StatsDTO = components['schemas']['StatsDTO'];
|
||||
type TranscriptionQueueItemDTO = components['schemas']['TranscriptionQueueItemDTO'];
|
||||
@@ -14,12 +15,6 @@ type DocumentListItem = components['schemas']['DocumentListItem'];
|
||||
type GeschichteSummary = components['schemas']['GeschichteSummary'];
|
||||
type TagTreeNodeDTO = components['schemas']['TagTreeNodeDTO'];
|
||||
|
||||
function settled<T>(res: PromiseSettledResult<unknown> | undefined): T | null {
|
||||
if (res?.status !== 'fulfilled') return null;
|
||||
const v = res.value as { response: Response; data: unknown };
|
||||
return v.response.ok ? ((v.data as T) ?? null) : null;
|
||||
}
|
||||
|
||||
export async function load({ fetch, parent }) {
|
||||
const { canWrite, canAnnotate, canBlogWrite } = await parent();
|
||||
// READ_ALL without WRITE_ALL or ANNOTATE_ALL — see ADR-007.
|
||||
|
||||
@@ -24,11 +24,18 @@ function makeUrl(params: Record<string, string | string[]> = {}) {
|
||||
return url;
|
||||
}
|
||||
|
||||
function callLoad(url: URL) {
|
||||
function callLoad(url: URL, parentData: { canBlogWrite?: boolean } = {}) {
|
||||
return load({
|
||||
url,
|
||||
request: new Request('http://localhost/geschichten'),
|
||||
fetch: vi.fn() as unknown as typeof fetch
|
||||
fetch: vi.fn() as unknown as typeof fetch,
|
||||
parent: vi.fn().mockResolvedValue({
|
||||
canBlogWrite: false,
|
||||
canWrite: false,
|
||||
canAnnotate: false,
|
||||
user: null,
|
||||
...parentData
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
@@ -191,3 +198,60 @@ describe('geschichten page load — documentFilter title resolution', () => {
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
// ─── draft fetch ──────────────────────────────────────────────────────────────
|
||||
|
||||
describe('geschichten page load — draft fetch', () => {
|
||||
it('makes a second API call for DRAFT stories when canBlogWrite is true', async () => {
|
||||
const mockGet = mockApi();
|
||||
|
||||
await callLoad(makeUrl(), { canBlogWrite: true });
|
||||
|
||||
const draftCall = mockGet.mock.calls.find(
|
||||
([, opts]: [string, { params?: { query?: { status?: string } } }]) =>
|
||||
opts?.params?.query?.status === 'DRAFT'
|
||||
);
|
||||
expect(draftCall).toBeDefined();
|
||||
});
|
||||
|
||||
it('does NOT make a DRAFT API call when canBlogWrite is false', async () => {
|
||||
const mockGet = mockApi();
|
||||
|
||||
await callLoad(makeUrl(), { canBlogWrite: false });
|
||||
|
||||
const draftCall = mockGet.mock.calls.find(
|
||||
([, opts]: [string, { params?: { query?: { status?: string } } }]) =>
|
||||
opts?.params?.query?.status === 'DRAFT'
|
||||
);
|
||||
expect(draftCall).toBeUndefined();
|
||||
});
|
||||
|
||||
it('returns empty drafts array when the DRAFT fetch rejects', async () => {
|
||||
const mockGet = vi
|
||||
.fn()
|
||||
.mockResolvedValueOnce({ response: { ok: true, status: 200 }, data: [] })
|
||||
.mockRejectedValueOnce(new Error('network error'));
|
||||
vi.mocked(createApiClient).mockReturnValue({ GET: mockGet } as ReturnType<
|
||||
typeof createApiClient
|
||||
>);
|
||||
|
||||
const result = await callLoad(makeUrl(), { canBlogWrite: true });
|
||||
|
||||
expect(result.drafts).toEqual([]);
|
||||
});
|
||||
|
||||
it('returns drafts in page data when canBlogWrite is true', async () => {
|
||||
const draft = { id: 'draft-1', title: 'My Draft', status: 'DRAFT' };
|
||||
const mockGet = vi
|
||||
.fn()
|
||||
.mockResolvedValueOnce({ response: { ok: true, status: 200 }, data: [] })
|
||||
.mockResolvedValueOnce({ response: { ok: true, status: 200 }, data: [draft] });
|
||||
vi.mocked(createApiClient).mockReturnValue({ GET: mockGet } as ReturnType<
|
||||
typeof createApiClient
|
||||
>);
|
||||
|
||||
const result = await callLoad(makeUrl(), { canBlogWrite: true });
|
||||
|
||||
expect(result.drafts).toEqual([draft]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -26,6 +26,7 @@ function person(id: string, displayName: string) {
|
||||
function makeData(overrides: Partial<PageData> = {}): PageData {
|
||||
return {
|
||||
geschichten: [],
|
||||
drafts: [],
|
||||
personFilters: [],
|
||||
documentFilter: null,
|
||||
canBlogWrite: false,
|
||||
|
||||
Reference in New Issue
Block a user