@smartsamurai/krapi-sdk
Version:
KRAPI TypeScript SDK - Easy-to-use client SDK for connecting to self-hosted KRAPI servers (like Appwrite SDK)
180 lines (170 loc) • 4.94 kB
text/typescript
/**
* Testing Adapter
*
* Unifies TestingHttpClient and TestingService behind a common interface.
*/
import { TestingHttpClient } from "../../http-clients/testing-http-client";
import { TestingService } from "../../testing-service";
import { Project } from "../../types";
import { createAdapterInitError } from "./error-handler";
type Mode = "client" | "server";
export class TestingAdapter {
private mode: Mode;
private httpClient: TestingHttpClient | undefined;
private service: TestingService | undefined;
constructor(mode: Mode, httpClient?: TestingHttpClient, service?: TestingService) {
this.mode = mode;
this.httpClient = httpClient;
this.service = service;
}
async createTestProject(options?: {
name?: string;
with_collections?: boolean;
with_documents?: boolean;
document_count?: number;
}): Promise<Project> {
if (this.mode === "client") {
if (!this.httpClient) {
throw createAdapterInitError("HTTP client", this.mode);
}
const response = await this.httpClient.createTestProject(options);
return (response.data as unknown as Project) || ({} as Project);
} else {
if (!this.service) {
throw createAdapterInitError("Testing service", this.mode);
}
// TestingService doesn't have createTestProject - use generateTestData instead
// This creates test data but doesn't return a project object
await this.service.generateTestData();
// Return a placeholder project - in real implementation, would need to fetch the created project
return {} as Project;
}
}
async cleanup(projectId?: string): Promise<{
success: boolean;
deleted: {
projects: number;
collections: number;
documents: number;
files: number;
users: number;
};
}> {
if (this.mode === "client") {
if (!this.httpClient) {
throw createAdapterInitError("HTTP client", this.mode);
}
const response = await this.httpClient.cleanup(projectId);
return response.data || {
success: false,
deleted: {
projects: 0,
collections: 0,
documents: 0,
files: 0,
users: 0,
},
};
} else {
if (!this.service) {
throw createAdapterInitError("Testing service", this.mode);
}
const result = await this.service.cleanupTestData(projectId);
return {
success: result.success,
deleted: {
projects: result.deleted,
collections: 0,
documents: 0,
files: 0,
users: 0,
},
};
}
}
async runTests(testSuite?: string): Promise<{
results: Array<{
suite: string;
tests: Array<{
name: string;
passed: boolean;
error?: string;
duration: number;
}>;
}>;
summary: {
total: number;
passed: number;
failed: number;
duration: number;
};
}> {
if (this.mode === "client") {
if (!this.httpClient) {
throw createAdapterInitError("HTTP client", this.mode);
}
const response = await this.httpClient.runTests(testSuite);
return response.data || {
results: [],
summary: {
total: 0,
passed: 0,
failed: 0,
duration: 0,
},
};
} else {
if (!this.service) {
throw createAdapterInitError("Testing service", this.mode);
}
// TestingService has runFullTestSuite instead of runTests
const result = await this.service.runFullTestSuite();
// Transform TestSuite format to expected format
return {
results: [{
suite: result.database.name,
tests: result.database.tests.map(t => ({
name: t.name,
passed: t.passed,
error: t.message,
duration: t.duration,
})),
}],
summary: {
total: result.summary.totalTests,
passed: result.summary.totalPassed,
failed: result.summary.totalFailed,
duration: result.summary.duration,
},
};
}
}
async seedData(
projectId: string,
seedType: string,
options?: Record<string, unknown>
): Promise<{
success: boolean;
created: Record<string, number>;
}> {
if (this.mode === "client") {
if (!this.httpClient) {
throw createAdapterInitError("HTTP client", this.mode);
}
const response = await this.httpClient.seedData(projectId, seedType, options);
return response.data || {
success: false,
created: {},
};
} else {
if (!this.service) {
throw createAdapterInitError("Testing service", this.mode);
}
const result = await this.service.generateTestData(projectId);
return {
success: result.success,
created: result.created,
};
}
}
}