feat: modules and resources domains, drop removed domains, regenerated models
This commit is contained in:
+66
-145
@@ -1,12 +1,9 @@
|
||||
import { vi } from 'vitest';
|
||||
import type { PendingEffect } from '../../src/generated/scenarios.js';
|
||||
import type { RemoteConfig } from '../../src/generated/remote-config.js';
|
||||
import type { Store } from '../../src/generated/stores.js';
|
||||
import type { StorageItem } from '../../src/generated/storage.js';
|
||||
import type {
|
||||
ClaimQuestResponse,
|
||||
ListQuestsResponse,
|
||||
} from '../../src/generated/quests.js';
|
||||
import type { ResourceRow } from '../../src/generated/resources.js';
|
||||
import type { CallModuleFunctionClientResponse } from '../../src/generated/modules.js';
|
||||
import type { CatalogCurrency } from '../../src/generated/catalog.js';
|
||||
import type { ProjectStorageItem } from '../../src/generated/project-storage.js';
|
||||
|
||||
export interface RecordedRequest {
|
||||
method: string;
|
||||
@@ -17,28 +14,17 @@ export interface RecordedRequest {
|
||||
}
|
||||
|
||||
export interface FakeGatewayState {
|
||||
scenarios: Map<string, PendingEffect[]>;
|
||||
callbacks: Map<string, PendingEffect | null>;
|
||||
callbackErrors: Map<string, { status: number; code?: string }>;
|
||||
pendingEffects: PendingEffect[];
|
||||
counterCompleted: boolean;
|
||||
counterEffect?: PendingEffect;
|
||||
remoteConfigs: Record<string, RemoteConfig>;
|
||||
stores: Store[];
|
||||
quests: ListQuestsResponse;
|
||||
questClaims: Map<string, ClaimQuestResponse>;
|
||||
storage: Map<string, StorageItem>;
|
||||
projectStorage: Map<string, ProjectStorageItem>;
|
||||
currencies: CatalogCurrency[];
|
||||
resources: Map<string, Map<string, ResourceRow>>;
|
||||
moduleCalls: CallModuleFunctionClientResponse;
|
||||
}
|
||||
|
||||
export interface FakeGateway {
|
||||
state: FakeGatewayState;
|
||||
recorded: RecordedRequest[];
|
||||
purchases: Array<{ storeSlug: string; offerSlug: string; idempotencyKey: string; authToken: string | null }>;
|
||||
questClaims: Array<{ questSlug: string; authToken: string | null }>;
|
||||
install(): void;
|
||||
onEvent(event: string, ...effects: PendingEffect[]): void;
|
||||
onCallback(nodeId: string, handle: string, next: PendingEffect | null): void;
|
||||
onCallbackError(nodeId: string, handle: string, status?: number, code?: string): void;
|
||||
}
|
||||
|
||||
function json(body: unknown, status = 200): Response {
|
||||
@@ -50,52 +36,16 @@ function json(body: unknown, status = 200): Response {
|
||||
|
||||
const noContent = (): Response => new Response(null, { status: 204 });
|
||||
|
||||
function replaceRun(pending: PendingEffect[], next: PendingEffect[]): PendingEffect[] {
|
||||
const runIds = new Set(next.map((effect) => effect.runId));
|
||||
return [...pending.filter((effect) => !runIds.has(effect.runId)), ...next];
|
||||
}
|
||||
|
||||
export function createFakeGateway(seed?: Partial<FakeGatewayState>): FakeGateway {
|
||||
const state: FakeGatewayState = {
|
||||
scenarios: seed?.scenarios ?? new Map(),
|
||||
callbacks: seed?.callbacks ?? new Map(),
|
||||
callbackErrors: seed?.callbackErrors ?? new Map(),
|
||||
pendingEffects: seed?.pendingEffects ?? [],
|
||||
counterCompleted: seed?.counterCompleted ?? false,
|
||||
counterEffect: seed?.counterEffect,
|
||||
remoteConfigs: seed?.remoteConfigs ?? {},
|
||||
stores: seed?.stores ?? [],
|
||||
quests: seed?.quests ?? { quests: [] },
|
||||
questClaims: seed?.questClaims ?? new Map(),
|
||||
storage: seed?.storage ?? new Map(),
|
||||
projectStorage: seed?.projectStorage ?? new Map(),
|
||||
currencies: seed?.currencies ?? [],
|
||||
resources: seed?.resources ?? new Map(),
|
||||
moduleCalls: seed?.moduleCalls ?? { result: {} },
|
||||
};
|
||||
|
||||
const recorded: RecordedRequest[] = [];
|
||||
const purchases: FakeGateway['purchases'] = [];
|
||||
const questClaims: FakeGateway['questClaims'] = [];
|
||||
|
||||
function advanceExpiredWaits(): PendingEffect[] {
|
||||
const now = Date.now();
|
||||
const next: PendingEffect[] = [];
|
||||
for (const effect of state.pendingEffects) {
|
||||
if (
|
||||
effect.type === 'wait' &&
|
||||
effect.waitDeadline &&
|
||||
Date.parse(effect.waitDeadline) <= now
|
||||
) {
|
||||
const continued = state.callbacks.get(`${effect.nodeId}:onComplete`);
|
||||
if (continued === undefined) {
|
||||
next.push(effect);
|
||||
} else if (continued !== null) {
|
||||
next.push(continued);
|
||||
}
|
||||
} else {
|
||||
next.push(effect);
|
||||
}
|
||||
}
|
||||
state.pendingEffects = next;
|
||||
return next;
|
||||
}
|
||||
|
||||
async function handle(req: RecordedRequest): Promise<Response> {
|
||||
const { method, path, query, body } = req;
|
||||
@@ -117,85 +67,54 @@ export function createFakeGateway(seed?: Partial<FakeGatewayState>): FakeGateway
|
||||
}
|
||||
|
||||
if (method === 'GET' && path === '/sdk/v1/catalog') {
|
||||
return json({ items: [] });
|
||||
return json({ currencies: state.currencies });
|
||||
}
|
||||
|
||||
if (method === 'POST' && path === '/sdk/v1/scenarios/trigger') {
|
||||
const event = (body as { event?: string }).event ?? '';
|
||||
const effects = state.scenarios.get(event) ?? [];
|
||||
state.pendingEffects = replaceRun(state.pendingEffects, effects);
|
||||
return json({ effects });
|
||||
const moduleMatch = path.match(/^\/sdk\/v1\/modules\/([^/]+)\/([^/]+)$/);
|
||||
if (method === 'POST' && moduleMatch) {
|
||||
return json(state.moduleCalls);
|
||||
}
|
||||
if (method === 'GET' && path === '/sdk/v1/scenarios/pending') {
|
||||
return json({ effects: advanceExpiredWaits() });
|
||||
}
|
||||
if (method === 'POST' && path === '/sdk/v1/scenarios/callback') {
|
||||
const b = body as { nodeId?: string; handle?: string; runId?: string };
|
||||
const key = `${b.nodeId}:${b.handle}`;
|
||||
const err = state.callbackErrors.get(key);
|
||||
if (err) {
|
||||
return json({ code: err.code ?? 'callback failed', error: 'callback failed' }, err.status);
|
||||
|
||||
const resourceItemMatch = path.match(/^\/sdk\/v1\/resources\/([^/]+)\/([^/]+)$/);
|
||||
const resourceListMatch = path.match(/^\/sdk\/v1\/resources\/([^/]+)$/);
|
||||
if (resourceItemMatch) {
|
||||
const slug = decodeURIComponent(resourceItemMatch[1]);
|
||||
const id = decodeURIComponent(resourceItemMatch[2]);
|
||||
const rows = state.resources.get(slug) ?? new Map();
|
||||
if (method === 'GET') {
|
||||
const row = rows.get(id);
|
||||
return row ? json(row) : json({ error: 'not found' }, 404);
|
||||
}
|
||||
const next = state.callbacks.get(key);
|
||||
state.pendingEffects = state.pendingEffects.filter(
|
||||
(effect) => !(effect.nodeId === b.nodeId && effect.runId === (b.runId ?? effect.runId)),
|
||||
);
|
||||
if (next) {
|
||||
state.pendingEffects = replaceRun(state.pendingEffects, [next]);
|
||||
return json({ effect: next });
|
||||
if (method === 'PATCH') {
|
||||
const existing = rows.get(id);
|
||||
if (!existing) return json({ error: 'not found' }, 404);
|
||||
const next = { ...existing, ...(body as ResourceRow), id };
|
||||
rows.set(id, next);
|
||||
state.resources.set(slug, rows);
|
||||
return json(next);
|
||||
}
|
||||
return json({});
|
||||
}
|
||||
if (method === 'POST' && path === '/sdk/v1/scenarios/counter') {
|
||||
if (state.counterCompleted && state.counterEffect) {
|
||||
state.pendingEffects = replaceRun(state.pendingEffects, [state.counterEffect]);
|
||||
} else if (state.counterCompleted) {
|
||||
const b = body as { nodeId?: string; runId?: string };
|
||||
state.pendingEffects = state.pendingEffects.filter(
|
||||
(effect) => !(effect.nodeId === b.nodeId && effect.runId === (b.runId ?? effect.runId)),
|
||||
);
|
||||
if (method === 'DELETE') {
|
||||
rows.delete(id);
|
||||
state.resources.set(slug, rows);
|
||||
return noContent();
|
||||
}
|
||||
return json({ completed: state.counterCompleted, effect: state.counterEffect });
|
||||
}
|
||||
|
||||
if (method === 'GET' && path === '/sdk/v1/remote-configs') {
|
||||
return json({ configs: state.remoteConfigs });
|
||||
}
|
||||
if (method === 'GET' && path.startsWith('/sdk/v1/remote-configs/')) {
|
||||
const key = decodeURIComponent(path.slice('/sdk/v1/remote-configs/'.length));
|
||||
const cfg = state.remoteConfigs[key];
|
||||
return cfg ? json(cfg) : json({ error: 'not found' }, 404);
|
||||
}
|
||||
|
||||
if (method === 'GET' && path === '/sdk/v1/stores') {
|
||||
return json({ stores: state.stores, total: state.stores.length });
|
||||
}
|
||||
const purchaseMatch = path.match(/^\/sdk\/v1\/stores\/([^/]+)\/offers\/([^/]+)\/purchase$/);
|
||||
if (method === 'POST' && purchaseMatch) {
|
||||
const b = body as { idempotencyKey?: string };
|
||||
purchases.push({
|
||||
storeSlug: decodeURIComponent(purchaseMatch[1]),
|
||||
offerSlug: decodeURIComponent(purchaseMatch[2]),
|
||||
idempotencyKey: b.idempotencyKey ?? '',
|
||||
authToken: req.authToken,
|
||||
});
|
||||
return json({ success: true, purchaseId: `purchase-${purchases.length}` });
|
||||
}
|
||||
const storeMatch = path.match(/^\/sdk\/v1\/stores\/([^/]+)$/);
|
||||
if (method === 'GET' && storeMatch) {
|
||||
const slug = decodeURIComponent(storeMatch[1]);
|
||||
const store = state.stores.find((s) => s.slug === slug);
|
||||
return store ? json(store) : json({ error: 'not found' }, 404);
|
||||
}
|
||||
|
||||
if (method === 'POST' && path === '/sdk/v1/quests/list') {
|
||||
return json(state.quests);
|
||||
}
|
||||
if (method === 'POST' && path === '/sdk/v1/quests/claim') {
|
||||
const b = body as { questSlug?: string };
|
||||
const questSlug = b.questSlug ?? '';
|
||||
questClaims.push({ questSlug, authToken: req.authToken });
|
||||
return json(state.questClaims.get(questSlug) ?? { success: false, error: 'not found' });
|
||||
if (resourceListMatch) {
|
||||
const slug = decodeURIComponent(resourceListMatch[1]);
|
||||
const rows = state.resources.get(slug) ?? new Map();
|
||||
if (method === 'GET') {
|
||||
return json({ items: [...rows.values()], total: rows.size });
|
||||
}
|
||||
if (method === 'POST') {
|
||||
const created: ResourceRow = {
|
||||
id: (body as ResourceRow).id ?? `row-${rows.size + 1}`,
|
||||
data: (body as ResourceRow).data,
|
||||
version: 1,
|
||||
};
|
||||
rows.set(created.id!, created);
|
||||
state.resources.set(slug, rows);
|
||||
return json(created);
|
||||
}
|
||||
}
|
||||
|
||||
if (path === '/sdk/v1/storage') {
|
||||
@@ -222,6 +141,19 @@ export function createFakeGateway(seed?: Partial<FakeGatewayState>): FakeGateway
|
||||
}
|
||||
}
|
||||
|
||||
if (path === '/sdk/v1/project-storage') {
|
||||
if (method === 'GET') {
|
||||
return json({ items: [...state.projectStorage.values()], nextCursor: '' });
|
||||
}
|
||||
if (method === 'PUT') {
|
||||
const items = (body as { items?: ProjectStorageItem[] }).items ?? [];
|
||||
for (const item of items) {
|
||||
if (item.id) state.projectStorage.set(item.id, item);
|
||||
}
|
||||
return noContent();
|
||||
}
|
||||
}
|
||||
|
||||
return json({ error: `unhandled route: ${method} ${path}` }, 500);
|
||||
}
|
||||
|
||||
@@ -248,17 +180,6 @@ export function createFakeGateway(seed?: Partial<FakeGatewayState>): FakeGateway
|
||||
return {
|
||||
state,
|
||||
recorded,
|
||||
purchases,
|
||||
questClaims,
|
||||
install,
|
||||
onEvent(event, ...effects) {
|
||||
state.scenarios.set(event, effects);
|
||||
},
|
||||
onCallback(nodeId, handle, next) {
|
||||
state.callbacks.set(`${nodeId}:${handle}`, next);
|
||||
},
|
||||
onCallbackError(nodeId, handle, status = 500, code) {
|
||||
state.callbackErrors.set(`${nodeId}:${handle}`, { status, code });
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user