Skip to content
Closed
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
100 changes: 100 additions & 0 deletions npm-packages/convex/src/cli/codegen_templates/component_api.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { test, expect } from "vitest";
import type {
AnalyzedFunction,
AnalyzedModule,
} from "../lib/deployApi/modules.js";
import type { CanonicalizedModulePath } from "../lib/deployApi/paths.js";

const mockCtx = {
crash: async (error: any) => {
throw new Error(error.printedMessage);
},
} as any;

function createMockFunction(name: string): AnalyzedFunction {
return {
name,
udfType: "Query",
visibility: { kind: "public" as const },
args: "{}",
returns: "any",
} as AnalyzedFunction;
}

function createMockModule(functions: AnalyzedFunction[]): AnalyzedModule {
return {
functions,
} as AnalyzedModule;
}

import { buildApiTree } from "./component_api.js";

test("should deduplicate single-function files matching filename", async () => {
const functions: Record<CanonicalizedModulePath, AnalyzedModule> = {
// Single-function files matching filename (should deduplicate)
"blog/post/getComments.ts": createMockModule([
createMockFunction("getComments"),
]),
"admin/users/permissions/checkAccess.ts": createMockModule([
createMockFunction("checkAccess"),
]),
// Multi-function file (should NOT deduplicate)
"blog/post/mutations.ts": createMockModule([
createMockFunction("createPost"),
createMockFunction("updatePost"),
]),
} as Record<CanonicalizedModulePath, AnalyzedModule>;

const tree = (await buildApiTree(mockCtx, functions, {
kind: "public",
})) as any;

// Verify deduplication: single-function files with matching names
expect(tree.blog?.branch?.post?.branch?.getComments?.leaf?.name).toBe(
"getComments",
);
expect(
tree.admin?.branch?.users?.branch?.permissions?.branch?.checkAccess?.leaf
?.name,
).toBe("checkAccess");

// Verify no deduplication: multi-function files keep filename
expect(
tree.blog?.branch?.post?.branch?.mutations?.branch?.createPost?.leaf?.name,
).toBe("createPost");
expect(
tree.blog?.branch?.post?.branch?.mutations?.branch?.updatePost?.leaf?.name,
).toBe("updatePost");
});

test("should maintain backward compatibility with existing patterns", async () => {
const functions: Record<CanonicalizedModulePath, AnalyzedModule> = {
// Multi-function files - existing pattern should work unchanged
"api/users.ts": createMockModule([
createMockFunction("getUserById"),
createMockFunction("listUsers"),
]),
"blog/queries.ts": createMockModule([
createMockFunction("getPosts"),
createMockFunction("getPostById"),
]),
} as Record<CanonicalizedModulePath, AnalyzedModule>;

const tree = (await buildApiTree(mockCtx, functions, {
kind: "public",
})) as any;

// All should keep their filenames
expect(tree.api?.branch?.users?.branch?.getUserById?.leaf?.name).toBe(
"getUserById",
);
expect(tree.api?.branch?.users?.branch?.listUsers?.leaf?.name).toBe(
"listUsers",
);
expect(tree.blog?.branch?.queries?.branch?.getPosts?.leaf?.name).toBe(
"getPosts",
);
expect(tree.blog?.branch?.queries?.branch?.getPostById?.leaf?.name).toBe(
"getPostById",
);
});
24 changes: 22 additions & 2 deletions npm-packages/convex/src/cli/codegen_templates/component_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,22 @@ interface ApiTree {
| { type: "leaf"; leaf: AnalyzedFunction };
}

async function buildApiTree(
export async function buildApiTree(
ctx: Context,
functions: Record<CanonicalizedModulePath, AnalyzedModule>,
visibility: Visibility,
): Promise<ApiTree> {
const root: ApiTree = {};

const shouldDeduplicateFilename = (
functionName: string,
pathComponents: string[],
functionCountInFile: number,
): boolean => {
const lastPathComponent = pathComponents[pathComponents.length - 1];
return functionName === lastPathComponent && functionCountInFile === 1;
};

for (const [modulePath, module] of Object.entries(functions)) {
const p = importPath(modulePath);
if (p.startsWith("_deps/")) {
Expand All @@ -259,7 +269,17 @@ async function buildApiTree(
continue;
}
let current = root;
for (const pathComponent of p.split("/")) {
const pathComponents = p.split("/");
const shouldSkipLastComponent = shouldDeduplicateFilename(
f.name,
pathComponents,
module.functions.length,
);
const componentsToUse = shouldSkipLastComponent
? pathComponents.slice(0, -1) // use the penultimate component
: pathComponents;

for (const pathComponent of componentsToUse) {
let next = current[pathComponent];
if (!next) {
next = { type: "branch", branch: {} };
Expand Down