Initial Commit
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
import type { FetchLike } from "@suaveplan/payment-providers";
|
||||
import { AdyenProvider } from "../src/adyen/adyen.js";
|
||||
import type { AdyenConfig } from "../src/types/adyen.js";
|
||||
|
||||
/** Single recorded call on the test `FetchLike`. */
|
||||
export interface RecordedCall {
|
||||
readonly url: string;
|
||||
readonly init?: RequestInit;
|
||||
}
|
||||
|
||||
/** Test harness bundling a provider with helpers for inspecting requests. */
|
||||
export interface Harness {
|
||||
readonly provider: AdyenProvider;
|
||||
readonly calls: RecordedCall[];
|
||||
readonly bodyOf: (index: number) => Record<string, unknown>;
|
||||
readonly headersOf: (index: number) => Record<string, string>;
|
||||
}
|
||||
|
||||
/** Convenience: build a `FetchLike` that responds with the given JSON payload. */
|
||||
export function makeFetch(payload: unknown, status = 200): FetchLike {
|
||||
return async () =>
|
||||
new Response(JSON.stringify(payload), {
|
||||
status,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
}
|
||||
|
||||
function recording(payload: unknown): {
|
||||
calls: RecordedCall[];
|
||||
fetch: FetchLike;
|
||||
} {
|
||||
const calls: RecordedCall[] = [];
|
||||
const fetch: FetchLike = async (url, init) => {
|
||||
calls.push({ url, init });
|
||||
return new Response(JSON.stringify(payload), {
|
||||
status: 200,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
};
|
||||
return { calls, fetch };
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a harness around an `AdyenProvider` whose `fetch` records every
|
||||
* request and replies with the supplied JSON payload. Optional `extra`
|
||||
* config overrides merge over the defaults (`apiKey`, `merchantAccount`,
|
||||
* `baseUrl`).
|
||||
*/
|
||||
export function setupRecording(
|
||||
payload: unknown,
|
||||
extra: Partial<AdyenConfig> = {}
|
||||
): Harness {
|
||||
const { calls, fetch } = recording(payload);
|
||||
const provider = new AdyenProvider({
|
||||
apiKey: "adyen_key_123",
|
||||
merchantAccount: "TestMerchant",
|
||||
baseUrl: "https://adyen.mock/v71",
|
||||
fetch,
|
||||
...extra,
|
||||
});
|
||||
const bodyOf = (index: number): Record<string, unknown> => {
|
||||
const raw = calls[index]?.init?.body;
|
||||
if (typeof raw !== "string") {
|
||||
throw new Error("expected string body");
|
||||
}
|
||||
return JSON.parse(raw) as Record<string, unknown>;
|
||||
};
|
||||
const headersOf = (index: number): Record<string, string> => {
|
||||
const raw = calls[index]?.init?.headers;
|
||||
if (raw === undefined) {
|
||||
throw new Error("expected headers");
|
||||
}
|
||||
return raw as Record<string, string>;
|
||||
};
|
||||
return { provider, calls, bodyOf, headersOf };
|
||||
}
|
||||
Reference in New Issue
Block a user