Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions scripts/generate-sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ test("imports the generator without writing the tracked SDK", async () => {

test("rewrites the current binary return shape without changing JSON operations", async () => {
const { generateSdkSource } = await loadGeneratorModule();
const createSchema = openapiSpec.paths["/v1/project-registration/channels"]
.post.requestBody.content["application/json"].schema;
const bindSchema = openapiSpec.paths["/v1/project-registration/channels/bind-existing"]
.post.requestBody.content["application/json"].schema;
expect(createSchema.required).toEqual([]);
expect(bindSchema.required).toEqual(["operation_intent", "bind_existing"]);
const raw = generateSdkFromOpenApi(openapiSpec as any, {
className: "ConversationsClient",
apiKeyHeader: "x-api-key",
Expand All @@ -117,6 +123,7 @@ test("rewrites the current binary return shape without changing JSON operations"
const generated = generateSdkSource().code;
const generatedDownload = methodSource(generated, "downloadMessageAttachment");
const generatedGetMessage = methodSource(generated, "getMessage");
const generatedRegisterProjectChannel = methodSource(generated, "registerProjectChannel");

expect(generatedDownload).toContain(
'query: { "encoding": "base64" }, init?: RequestInit): Promise<{ "name": string; "mime_type": string; "size": number; "content_base64": string }>',
Expand All @@ -131,6 +138,9 @@ test("rewrites the current binary return shape without changing JSON operations"
expect(generated).toContain(
'opts.responseType === "arrayBuffer" && !response.headers.get("content-type")?.toLowerCase().includes("application/json")',
);
expect(generatedRegisterProjectChannel).toContain(
'body: { "operation_intent"?: "create" } & Record<string, unknown>',
);
expect(generatedGetMessage).toBe(rawGetMessage);
});

Expand Down
1 change: 1 addition & 0 deletions src/cli/commands/project-registration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe("registerProjectRegistrationCommands", () => {
expect(names).toEqual(expect.arrayContaining([
"capability",
"create",
"bind-existing",
"lookup-receipt",
"compensate",
"verify-inverse",
Expand Down
35 changes: 30 additions & 5 deletions src/cli/commands/project-registration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { readFileSync } from "node:fs";
import type { Command } from "commander";
import { closeDb } from "../../lib/db.js";
import type {
ProjectChannelRegistrationOperationIntent,
ProjectChannelRegistrationLookupRequest,
ProjectChannelRegistrationRequest,
ProjectChannelRegistrationReadRequest,
} from "../../lib/project-channel-registration.js";
import { assertProjectChannelRegistrationOperationIntent } from "../../lib/project-channel-registration.js";
import { getStore } from "../../lib/store/index.js";
import { printJson } from "../../lib/stdout.js";
import { emitCliError } from "../cli-error.js";
Expand Down Expand Up @@ -54,7 +56,11 @@ function requestObject(path: string, opts: OutputOptions): Record<string, unknow
return parsed as Record<string, unknown>;
}

function registrationRequest(path: string, opts: OutputOptions): ProjectChannelRegistrationRequest {
function registrationRequest(
path: string,
opts: OutputOptions,
expectedIntent?: ProjectChannelRegistrationOperationIntent,
): ProjectChannelRegistrationRequest {
const parsed = requestObject(path, opts);
const targetDigest = typeof parsed.target_digest === "string"
? parsed.target_digest.trim()
Expand All @@ -63,10 +69,14 @@ function registrationRequest(path: string, opts: OutputOptions): ProjectChannelR
emitCliError("The request file must include target_digest.", opts);
}
const { target_digest: _targetDigest, target: _target, ...request } = parsed;
return {
const parsedRequest = {
...request,
target: remoteTarget(targetDigest),
} as unknown as ProjectChannelRegistrationRequest;
if (expectedIntent) {
assertProjectChannelRegistrationOperationIntent(parsedRequest, expectedIntent);
}
return parsedRequest;
}

function collectionBounds(opts: {
Expand Down Expand Up @@ -104,13 +114,28 @@ export function registerProjectRegistrationCommands(program: Command): void {

registration
.command("create")
.description("Conditionally register a project channel from one contract request JSON file")
.description("Conditionally create an absent project channel from one contract request JSON file")
.requiredOption("--request <path>", "Contract request JSON file")
.option("-j, --json", "Output as JSON")
.action(async (opts) => {
try {
printJson(await getStore().registerProjectChannel(
registrationRequest(opts.request, opts),
registrationRequest(opts.request, opts, "create"),
));
} finally {
closeDb();
}
});

registration
.command("bind-existing")
.description("Conditionally bind one existing channel to a Projects workspace without recreating it")
.requiredOption("--request <path>", "Bind-existing contract request JSON file")
.option("-j, --json", "Output as JSON")
.action(async (opts) => {
try {
printJson(await getStore().registerProjectChannel(
registrationRequest(opts.request, opts, "bind_existing"),
));
} finally {
closeDb();
Expand Down Expand Up @@ -149,7 +174,7 @@ export function registerProjectRegistrationCommands(program: Command): void {

registration
.command("verify-inverse")
.description("Verify accepted inverse receipt and target absence")
.description("Verify accepted inverse receipt and target absence or restored ownership")
.requiredOption("--request <path>", "Inverse contract request JSON file")
.option("-j, --json", "Output as JSON")
.action(async (opts) => {
Expand Down
Loading
Loading