mirror of
https://github.com/VibedByKaKi/t3-code-android-nightly.git
synced 2026-10-11 21:01:16 +02:00
321 lines
11 KiB
TypeScript
321 lines
11 KiB
TypeScript
import * as AWS from "@/AWS";
|
|
import * as Core from "@/Test/Core";
|
|
import * as Test from "@/Test/Alchemy";
|
|
import * as sqs from "@distilled.cloud/aws/sqs";
|
|
import { describe, expect } from "alchemy-test";
|
|
import * as Data from "effect/Data";
|
|
import * as Effect from "effect/Effect";
|
|
import * as Schedule from "effect/Schedule";
|
|
import * as HttpClient from "effect/http/HttpClient";
|
|
import * as HttpClientRequest from "effect/http/HttpClientRequest";
|
|
import B2biTestFunctionLive, {
|
|
B2biTestFunction,
|
|
EVENTS_QUEUE,
|
|
} from "./handler";
|
|
|
|
const testOptions = { providers: AWS.providers() };
|
|
const { test, beforeAll, afterAll } = Test.make(testOptions);
|
|
const sharedStack = Core.scratchStack(testOptions, "B2biBindings");
|
|
|
|
// Lambda function URL cold-start (DNS, IAM propagation, init) can take well
|
|
// over 60s on a fresh deploy.
|
|
const readinessPolicy = Schedule.max([
|
|
Schedule.fixed("2 seconds"),
|
|
Schedule.recurs(75),
|
|
]);
|
|
|
|
let baseUrl: string;
|
|
|
|
class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
|
|
readonly status: number;
|
|
readonly body: string;
|
|
}> {}
|
|
|
|
// The shared Lambda fixture occasionally answers a transient 5xx under load
|
|
// (cold re-init, IAM propagation on the freshly attached policy that the
|
|
// handler's `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine
|
|
// 4xx/assertion failure surfaces immediately.
|
|
const send = (request: HttpClientRequest.HttpClientRequest) =>
|
|
HttpClient.execute(request).pipe(
|
|
Effect.flatMap((response) =>
|
|
response.status >= 500
|
|
? response.text.pipe(
|
|
Effect.flatMap((body) =>
|
|
Effect.fail(
|
|
new TransientUpstream({ status: response.status, body }),
|
|
),
|
|
),
|
|
)
|
|
: Effect.succeed(response),
|
|
),
|
|
Effect.retry({
|
|
while: (e) => e._tag === "TransientUpstream",
|
|
schedule: Schedule.max([
|
|
Schedule.exponential("2 seconds"),
|
|
Schedule.recurs(6),
|
|
]),
|
|
}),
|
|
);
|
|
|
|
class TransformationEventNotObserved extends Data.TaggedError(
|
|
"TransformationEventNotObserved",
|
|
)<{ readonly transformerJobId: string }> {
|
|
override get message() {
|
|
return `No B2BI transformation event was observed for job ${this.transformerJobId}`;
|
|
}
|
|
}
|
|
|
|
const runTransformerJob = Effect.fn(function* () {
|
|
const response = (yield* send(
|
|
HttpClientRequest.post(`${baseUrl}/transformer-job`),
|
|
).pipe(Effect.flatMap((r) => r.json))) as {
|
|
transformerJobId: string;
|
|
status: string;
|
|
message: string | null;
|
|
outputFiles: { bucketName?: string; key?: string }[];
|
|
error?: string;
|
|
};
|
|
expect(response.error).toBeUndefined();
|
|
expect(response.transformerJobId).toBeTruthy();
|
|
expect(response.status).toBe("succeeded");
|
|
expect(response.outputFiles.length).toBeGreaterThan(0);
|
|
yield* Effect.logInfo(
|
|
`B2BI job ${response.transformerJobId}: ${response.status}; ${response.message ?? "no message"}`,
|
|
);
|
|
return response;
|
|
});
|
|
|
|
describe.sequential(
|
|
"B2BI Bindings",
|
|
{
|
|
tags: [
|
|
"provider:aws",
|
|
"provider:aws:b2bi",
|
|
"provider:aws:iam",
|
|
"provider:aws:lambda",
|
|
"provider:aws:s3",
|
|
"provider:aws:sqs",
|
|
"live",
|
|
],
|
|
},
|
|
() => {
|
|
beforeAll(
|
|
Effect.gen(function* () {
|
|
yield* Effect.logInfo("B2BI test setup: destroying previous resources");
|
|
yield* sharedStack.destroy();
|
|
|
|
yield* Effect.logInfo("B2BI test setup: deploying fixture");
|
|
const { functionUrl } = yield* sharedStack.deploy(
|
|
Effect.gen(function* () {
|
|
return yield* B2biTestFunction;
|
|
}).pipe(Effect.provide(B2biTestFunctionLive)),
|
|
);
|
|
|
|
expect(functionUrl).toBeTruthy();
|
|
baseUrl = functionUrl!.replace(/\/+$/, "");
|
|
|
|
const readinessUrl = `${baseUrl}/bindings`;
|
|
yield* Effect.logInfo(
|
|
`B2BI test setup: probing readiness at ${readinessUrl}`,
|
|
);
|
|
yield* HttpClient.get(readinessUrl).pipe(
|
|
Effect.flatMap((response) =>
|
|
response.status === 200
|
|
? Effect.succeed(response)
|
|
: Effect.fail(
|
|
new Error(`Function not ready: ${response.status}`),
|
|
),
|
|
),
|
|
Effect.tapError((error) =>
|
|
Effect.logWarning(
|
|
`B2BI test setup: fixture not ready yet (${String(error)})`,
|
|
),
|
|
),
|
|
Effect.retry({ schedule: readinessPolicy }),
|
|
);
|
|
}),
|
|
{ timeout: 300_000 },
|
|
);
|
|
|
|
afterAll.skipIf(!!process.env.NO_DESTROY)(sharedStack.destroy(), {
|
|
timeout: 180_000,
|
|
});
|
|
|
|
describe("binding registration", () => {
|
|
test.provider("all 8 capabilities initialize in the runtime", (_stack) =>
|
|
Effect.gen(function* () {
|
|
const response = yield* send(
|
|
HttpClientRequest.get(`${baseUrl}/bindings`),
|
|
).pipe(Effect.flatMap((r) => r.json));
|
|
expect((response as any).bound).toHaveLength(8);
|
|
}),
|
|
);
|
|
});
|
|
|
|
describe("TestMapping", () => {
|
|
test.provider("maps JSON content with a JSONATA template", (_stack) =>
|
|
Effect.gen(function* () {
|
|
const response = (yield* send(
|
|
HttpClientRequest.post(`${baseUrl}/test-mapping`),
|
|
).pipe(Effect.flatMap((r) => r.json))) as {
|
|
mappedFileContent: string;
|
|
};
|
|
// B2BI returns the mapped output as text (observed live: a JSON
|
|
// string literal of the pretty-printed document) — unwrap however
|
|
// many string layers it arrives in.
|
|
const parseDeep = (value: unknown): unknown =>
|
|
typeof value === "string" ? parseDeep(JSON.parse(value)) : value;
|
|
expect(parseDeep(response.mappedFileContent)).toEqual({
|
|
name: "acme",
|
|
});
|
|
}),
|
|
);
|
|
});
|
|
|
|
describe("CreateStarterMappingTemplate", () => {
|
|
test.provider(
|
|
"scaffolds a JSONATA template for X12 850",
|
|
(_stack) =>
|
|
Effect.gen(function* () {
|
|
const response = (yield* send(
|
|
HttpClientRequest.post(`${baseUrl}/starter-template`),
|
|
).pipe(Effect.flatMap((r) => r.json))) as {
|
|
templateLength: number;
|
|
error?: string;
|
|
};
|
|
expect(response.error).toBeUndefined();
|
|
expect(response.templateLength).toBeGreaterThan(0);
|
|
}),
|
|
{ timeout: 60_000 },
|
|
);
|
|
});
|
|
|
|
describe("GenerateMapping", () => {
|
|
test.provider(
|
|
"generates a mapping template from sample documents",
|
|
(_stack) =>
|
|
Effect.gen(function* () {
|
|
const response = (yield* send(
|
|
HttpClientRequest.post(`${baseUrl}/generate-mapping`),
|
|
).pipe(Effect.flatMap((r) => r.json))) as {
|
|
templateLength: number;
|
|
bedrockAccessDenied: boolean;
|
|
error?: string;
|
|
};
|
|
expect(response.error).toBeUndefined();
|
|
if (response.bedrockAccessDenied) {
|
|
// Bedrock model access for B2BI's mapping model is only
|
|
// intermittently entitled in the testing account (the same call
|
|
// succeeds on other runs — cross-region model routing). The
|
|
// typed rejection ("AccessDeniedException: Access denied when
|
|
// invoking Bedrock's InvokeModel API") still proves the
|
|
// b2bi:GenerateMapping + bedrock:InvokeModel grants end-to-end —
|
|
// an IAM gap would deny b2bi:GenerateMapping itself.
|
|
expect(response.templateLength).toBe(0);
|
|
} else {
|
|
expect(response.templateLength).toBeGreaterThan(0);
|
|
}
|
|
}),
|
|
{ timeout: 120_000 },
|
|
);
|
|
});
|
|
|
|
describe("TestParsing", () => {
|
|
test.provider(
|
|
"parses an X12 850 from S3 into JSON",
|
|
(_stack) =>
|
|
Effect.gen(function* () {
|
|
const response = (yield* send(
|
|
HttpClientRequest.post(`${baseUrl}/test-parsing`),
|
|
).pipe(Effect.flatMap((r) => r.json))) as {
|
|
parsed: any;
|
|
error?: string;
|
|
};
|
|
expect(response.error).toBeUndefined();
|
|
// The parsed representation carries the interchange/transaction data.
|
|
expect(JSON.stringify(response.parsed)).toContain("850");
|
|
}),
|
|
{ timeout: 60_000 },
|
|
);
|
|
});
|
|
|
|
describe("TestConversion", () => {
|
|
test.provider(
|
|
"converts parsed JSON back into an X12 document",
|
|
(_stack) =>
|
|
Effect.gen(function* () {
|
|
const response = (yield* send(
|
|
HttpClientRequest.post(`${baseUrl}/test-conversion`),
|
|
).pipe(Effect.flatMap((r) => r.json))) as {
|
|
convertedLength: number;
|
|
startsWithIsa: boolean;
|
|
error?: string;
|
|
};
|
|
expect(response.error).toBeUndefined();
|
|
expect(response.convertedLength).toBeGreaterThan(0);
|
|
expect(response.startsWithIsa).toBe(true);
|
|
}),
|
|
{ timeout: 60_000 },
|
|
);
|
|
});
|
|
|
|
describe("StartTransformerJob + GetTransformerJob", () => {
|
|
test.provider(
|
|
"runs a transformer job to completion",
|
|
() => runTransformerJob(),
|
|
{ timeout: 120_000 },
|
|
);
|
|
});
|
|
|
|
describe("consumeTransformationEvents", () => {
|
|
test.provider(
|
|
"delivers the completed job's EventBridge event to the sink queue",
|
|
() =>
|
|
Effect.gen(function* () {
|
|
const response = yield* runTransformerJob();
|
|
const { QueueUrl } = yield* sqs.getQueueUrl({
|
|
QueueName: EVENTS_QUEUE,
|
|
});
|
|
const event = yield* sqs
|
|
.receiveMessage({
|
|
QueueUrl: QueueUrl!,
|
|
WaitTimeSeconds: 5,
|
|
MaxNumberOfMessages: 10,
|
|
})
|
|
.pipe(
|
|
Effect.flatMap((result) => {
|
|
const event = (result.Messages ?? [])
|
|
.map(
|
|
(message) =>
|
|
JSON.parse(message.Body ?? "{}") as {
|
|
detailType?: string;
|
|
transformerJobId?: string;
|
|
},
|
|
)
|
|
.find(
|
|
(body) =>
|
|
body.transformerJobId === response.transformerJobId,
|
|
);
|
|
return event
|
|
? Effect.succeed(event)
|
|
: Effect.fail(
|
|
new TransformationEventNotObserved({
|
|
transformerJobId: response.transformerJobId,
|
|
}),
|
|
);
|
|
}),
|
|
Effect.retry({
|
|
while: (error) =>
|
|
error._tag === "TransformationEventNotObserved",
|
|
// Ten five-second long polls plus nine one-second delays.
|
|
schedule: Schedule.spaced("1 second"),
|
|
times: 9,
|
|
}),
|
|
);
|
|
expect(event.detailType).toBe("Transformation Completed");
|
|
}),
|
|
{ timeout: 240_000 },
|
|
);
|
|
});
|
|
},
|
|
);
|