Files
openidea/src/features/app/__tests__/newProjectTemplate.test.ts
T
l0ng-ai 605d5f4985 feat(new-project): project generators for mainstream languages and frameworks (#2689)
File › New Project offers 38 generators grouped by language (Java, Spring Boot, Quarkus, Micronaut, Kotlin, Ktor, Scala, Python/Django/FastAPI/Flask, Node/Vite/Next.js/Nuxt/Angular/SvelteKit/Astro/NestJS/Express, Go, Rust, C/C++, Zig, .NET, PHP/Laravel/Symfony, Ruby/Rails, Dart/Flutter, Swift, Elixir/Phoenix, Haskell, static website, empty). Each is a declaration in generators/<id>.toml run by the new ide-scaffold crate: built-in templates, the framework's official CLI run non-interactively, or a project service zip (Spring Initializr, code.quarkus.io, Micronaut Launch) whose metadata becomes form fields. Projects are built in a staging directory and moved into place only on success. The dialog renders each form from its schema, flags missing toolchains with install hints and streams progress; created projects open trusted. New Project is also on the welcome screen and the title bar project menu.

Closes #709
2026-11-11 17:22:57 +08:00

50 lines
2.0 KiB
TypeScript

import { describe, expect, it } from "vitest";
import type { Field } from "@/platform/ipc";
import { effectiveValues, fieldError, isVisible, render } from "../newProjectTemplate";
const text = (id: string, defaultValue: string, pattern: string | null = null): Field => ({
id,
label: id,
kind: { type: "text", pattern, placeholder: "" },
default: defaultValue,
visible: {},
hint: "",
});
describe("new project field templates", () => {
it("renders fields and filters like the backend", () => {
const v = { group: "com.example", name: "My Cool-App" };
expect(render("{{group}}.{{name|ident}}", v)).toBe("com.example.mycoolapp");
expect(render("src/{{group|path}}/{{ name | pascal }}.java", v)).toBe("src/com/example/MyCoolApp.java");
expect(render("{{name|kebab}} {{name|snake}}", v)).toBe("my-cool-app my_cool_app");
expect(render("{{missing}}x", v)).toBe("x");
expect(render("{{name|ident}}", { name: "1app" })).toBe("_1app");
expect(render("{{name|kebab}}", { name: "myApp2" })).toBe("my-app2");
expect(render("{{ns|json}}", { ns: "Acme\\Shop" })).toBe("Acme\\\\Shop");
});
it("fills defaults in field order and keeps edited values", () => {
const fields = [
text("group", "com.example"),
text("artifact", "{{name|kebab}}"),
text("package", "{{group}}.{{artifact|ident}}"),
];
expect(effectiveValues(fields, " My App ", {})).toEqual({
name: "My App",
group: "com.example",
artifact: "my-app",
package: "com.example.myapp",
});
expect(effectiveValues(fields, "x", { group: "org.acme" }).package).toBe("org.acme.x");
});
it("shows fields by their conditions and checks patterns", () => {
const f = { ...text("cxx", "20"), visible: { lang: ["cpp"] } };
expect(isVisible(f, { lang: "cpp" })).toBe(true);
expect(isVisible(f, { lang: "c" })).toBe(false);
const g = text("group", "", "^[a-z]+(\\.[a-z]+)*$");
expect(fieldError(g, "com.example")).toBeNull();
expect(fieldError(g, "com..example")).not.toBeNull();
});
});