2026-05-06 17:21:40 +05:30
|
|
|
import type { APIRequestContext } from "@playwright/test";
|
|
|
|
|
import { authHeaders, BACKEND_URL } from "./auth";
|
|
|
|
|
|
|
|
|
|
export type DocumentRow = {
|
|
|
|
|
id: number;
|
|
|
|
|
title: string;
|
|
|
|
|
content: string;
|
|
|
|
|
document_type: string;
|
|
|
|
|
status: { state?: string } | string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type Paginated<T> = {
|
|
|
|
|
items?: T[];
|
|
|
|
|
total?: number;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export async function listDocuments(
|
|
|
|
|
request: APIRequestContext,
|
|
|
|
|
token: string,
|
|
|
|
|
searchSpaceId: number,
|
|
|
|
|
limit = 100
|
|
|
|
|
): Promise<DocumentRow[]> {
|
|
|
|
|
const response = await request.get(
|
|
|
|
|
`${BACKEND_URL}/api/v1/documents?search_space_id=${searchSpaceId}&limit=${limit}`,
|
|
|
|
|
{ headers: authHeaders(token) }
|
|
|
|
|
);
|
|
|
|
|
if (!response.ok()) {
|
2026-05-09 05:16:20 +05:30
|
|
|
throw new Error(`listDocuments failed (${response.status()}): ${await response.text()}`);
|
2026-05-06 17:21:40 +05:30
|
|
|
}
|
|
|
|
|
const body = (await response.json()) as Paginated<DocumentRow> | DocumentRow[];
|
|
|
|
|
return Array.isArray(body) ? body : (body.items ?? []);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function isDocumentReady(doc: DocumentRow): boolean {
|
2026-05-09 05:16:20 +05:30
|
|
|
const state = typeof doc.status === "string" ? doc.status : doc.status?.state;
|
2026-05-06 17:21:40 +05:30
|
|
|
return state === "ready" || state === "READY";
|
|
|
|
|
}
|
2026-05-06 17:58:58 +05:30
|
|
|
|
|
|
|
|
export type EditorContent = {
|
|
|
|
|
document_id: number;
|
|
|
|
|
title: string;
|
|
|
|
|
document_type: string;
|
|
|
|
|
source_markdown: string;
|
|
|
|
|
content_size_bytes: number;
|
|
|
|
|
chunk_count: number;
|
|
|
|
|
truncated: boolean;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Same endpoint the UI hits when a user opens a document in the dashboard.
|
|
|
|
|
export async function getEditorContent(
|
|
|
|
|
request: APIRequestContext,
|
|
|
|
|
token: string,
|
|
|
|
|
searchSpaceId: number,
|
|
|
|
|
documentId: number
|
|
|
|
|
): Promise<EditorContent> {
|
|
|
|
|
const response = await request.get(
|
|
|
|
|
`${BACKEND_URL}/api/v1/search-spaces/${searchSpaceId}/documents/${documentId}/editor-content`,
|
|
|
|
|
{ headers: authHeaders(token) }
|
|
|
|
|
);
|
|
|
|
|
if (!response.ok()) {
|
2026-05-09 05:16:20 +05:30
|
|
|
throw new Error(`getEditorContent failed (${response.status()}): ${await response.text()}`);
|
2026-05-06 17:58:58 +05:30
|
|
|
}
|
|
|
|
|
return (await response.json()) as EditorContent;
|
|
|
|
|
}
|