Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import { EnvironmentId, ProjectId, CommandId, SourceControlDiscoveryResult } from "@v12/contracts";
3import * as Option from "effect/Option";
4
5import {
6 buildAddProjectRemoteSourceReadiness,
7 buildProjectCreateCommand,
8 findExistingAddProject,
9 getAddProjectInitialQuery,
10 resolveAddProjectPath,
11 sortAddProjectProviderSources,
12} from "./projects.ts";
13import type { EnvironmentProject } from "../state/models.ts";
14
15describe("add project shared logic", () => {
16 it("resolves initial browse paths from settings", () => {
17 expect(getAddProjectInitialQuery("")).toBe("~/");
18 expect(getAddProjectInitialQuery("/work")).toBe("/work/");
19 expect(getAddProjectInitialQuery("C:\\work")).toBe("C:\\work\\");
20 });
21
22 it("rejects unsupported windows paths on non-windows environments", () => {
23 expect(
24 resolveAddProjectPath({
25 rawPath: "C:\\repo",
26 platform: "MacIntel",
27 currentProjectCwd: null,
28 }),
29 ).toEqual({
30 ok: false,
31 error: "Windows-style paths are only supported on Windows environments.",
32 });
33 });
34
35 it("resolves relative paths from the active project cwd", () => {
36 expect(
37 resolveAddProjectPath({
38 rawPath: "../next",
39 platform: "Linux",
40 currentProjectCwd: "/work/current",
41 }),
42 ).toEqual({ ok: true, path: "/work/next" });
43 });
44
45 it("marks authenticated source control providers as ready", () => {
46 const discovery: SourceControlDiscoveryResult = {
47 versionControlSystems: [],
48 sourceControlProviders: [
49 {
50 kind: "github",
51 label: "GitHub",
52 status: "available",
53 installHint: "Install gh",
54 version: Option.some("1.0.0"),
55 detail: Option.none(),
56 auth: {
57 status: "authenticated",
58 account: Option.some("octo"),
59 host: Option.some("github.com"),
60 detail: Option.none(),
61 },
62 },
63 {
64 kind: "gitlab",
65 label: "GitLab",
66 status: "available",
67 installHint: "Install glab",
68 version: Option.some("1.0.0"),
69 detail: Option.none(),
70 auth: {
71 status: "unauthenticated",
72 account: Option.none(),
73 host: Option.none(),
74 detail: Option.some("Run glab auth login"),
75 },
76 },
77 ],
78 };
79
80 const readiness = buildAddProjectRemoteSourceReadiness(discovery);
81 expect(readiness.url.ready).toBe(true);
82 expect(readiness.github.ready).toBe(true);
83 expect(readiness.gitlab).toEqual({ ready: false, hint: "Run glab auth login" });
84 expect(sortAddProjectProviderSources(readiness)[0]).toBe("github");
85 });
86
87 it("finds existing projects by normalized path in the target environment", () => {
88 const env = EnvironmentId.make("env");
89 const other = EnvironmentId.make("other");
90 const projects: EnvironmentProject[] = [
91 {
92 environmentId: other,
93 id: ProjectId.make("same-path-other-env"),
94 title: "Other",
95 workspaceRoot: "/repo",
96 createdAt: "2026-01-01T00:00:00.000Z",
97 updatedAt: "2026-01-01T00:00:00.000Z",
98 repositoryIdentity: null,
99 defaultModelSelection: null,
100 scripts: [],
101 },
102 {
103 environmentId: env,
104 id: ProjectId.make("project"),
105 title: "Repo",
106 workspaceRoot: "/repo/",
107 createdAt: "2026-01-01T00:00:00.000Z",
108 updatedAt: "2026-01-01T00:00:00.000Z",
109 repositoryIdentity: null,
110 defaultModelSelection: null,
111 scripts: [],
112 },
113 ];
114
115 expect(findExistingAddProject({ projects, environmentId: env, path: "/repo" })?.id).toBe(
116 "project",
117 );
118 });
119
120 it("builds the existing project.create command shape", () => {
121 expect(
122 buildProjectCreateCommand({
123 commandId: CommandId.make("command"),
124 projectId: ProjectId.make("project"),
125 workspaceRoot: "/work/repo",
126 createdAt: "2026-01-01T00:00:00.000Z",
127 }),
128 ).toMatchObject({
129 type: "project.create",
130 commandId: "command",
131 projectId: "project",
132 title: "repo",
133 workspaceRoot: "/work/repo",
134 createWorkspaceRootIfMissing: true,
135 defaultModelSelection: {
136 instanceId: "codex",
137 model: "gpt-5.4",
138 },
139 });
140 });
141});