Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

projects.test.ts

153 lines4.7 KB

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