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

NewProject.test.ts

140 lines5.1 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Layer from "effect/Layer";
6import * as Path from "effect/Path";
7​
8import * as ServerConfig from "../config.ts";
9import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
10import * as VcsProcess from "../vcs/VcsProcess.ts";
11import { createNewProjectFolder } from "./NewProject.ts";
12​
13const TestLayer = GitVcsDriver.layer.pipe(
14 Layer.provide(ServerConfig.layerTest(process.cwd(), { prefix: "t3-new-project-" })),
15 Layer.provideMerge(VcsProcess.layer),
16 Layer.provideMerge(NodeServices.layer),
17);
18​
19const GIT_ENV_KEYS = [
20 "GIT_CONFIG_GLOBAL",
21 "GIT_CONFIG_NOSYSTEM",
22 "GIT_CONFIG_COUNT",
23 "GIT_CONFIG_KEY_0",
24 "GIT_CONFIG_VALUE_0",
25 "GIT_AUTHOR_NAME",
26 "GIT_AUTHOR_EMAIL",
27 "GIT_COMMITTER_NAME",
28 "GIT_COMMITTER_EMAIL",
29 "EMAIL",
30] as const;
31​
32// Git reads the developer's own config (signing, default branch, identity)
33// unless the test replaces it, so each test runs against an empty one.
34const withGitEnv = <A, E, R>(
35 env: Partial<Record<(typeof GIT_ENV_KEYS)[number], string>>,
36 effect: Effect.Effect<A, E, R>,
37) =>
38 Effect.gen(function* () {
39 const fileSystem = yield* FileSystem.FileSystem;
40 const emptyConfig = yield* fileSystem.makeTempFileScoped({ prefix: "t3-gitconfig-" });
41 return yield* Effect.acquireUseRelease(
42 Effect.sync(() => {
43 const saved = GIT_ENV_KEYS.map((key) => [key, process.env[key]] as const);
44 for (const key of GIT_ENV_KEYS) delete process.env[key];
45 Object.assign(process.env, {
46 GIT_CONFIG_GLOBAL: emptyConfig,
47 GIT_CONFIG_NOSYSTEM: "1",
48 ...env,
49 });
50 return saved;
51 }),
52 () => effect,
53 (saved) =>
54 Effect.sync(() => {
55 for (const [key, value] of saved) {
56 if (value === undefined) delete process.env[key];
57 else process.env[key] = value;
58 }
59 }),
60 );
61 });
62​
63const gitOutput = (cwd: string, args: ReadonlyArray<string>) =>
64 Effect.gen(function* () {
65 const git = yield* GitVcsDriver.GitVcsDriver;
66 const result = yield* git.execute({ operation: "NewProject.test", cwd, args });
67 return result.stdout.trim();
68 });
69​
70it.layer(TestLayer)("createNewProjectFolder", (it) => {
71 it.effect("makes the folder, starter files, and first commit, and suffixes a taken name", () =>
72 Effect.scoped(
73 withGitEnv(
74 {
75 GIT_AUTHOR_NAME: "Test",
76 GIT_AUTHOR_EMAIL: "test@test.com",
77 GIT_COMMITTER_NAME: "Test",
78 GIT_COMMITTER_EMAIL: "test@test.com",
79 },
80 Effect.gen(function* () {
81 const fileSystem = yield* FileSystem.FileSystem;
82 const path = yield* Path.Path;
83 const root = path.join(
84 yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-projects-" }),
85 "projects",
86 );
87​
88 const first = yield* createNewProjectFolder({ root, name: "Pinball Stats" });
89 const second = yield* createNewProjectFolder({ root, name: "pinball stats" });
90​
91 assert.equal(first.workspaceRoot, path.join(root, "pinball-stats"));
92 assert.equal(second.workspaceRoot, path.join(root, "pinball-stats-2"));
93 assert.isUndefined(first.commitError);
94 const readme = yield* fileSystem.readFileString(
95 path.join(first.workspaceRoot, "README.md"),
96 );
97 assert.include(readme, "# Pinball Stats");
98 assert.include(readme, `src="assets/icon.svg"`);
99 const icon = yield* fileSystem.readFileString(
100 path.join(first.workspaceRoot, "assets", "icon.svg"),
101 );
102 assert.include(icon, ">PS</text>");
103 assert.equal(
104 yield* gitOutput(first.workspaceRoot, ["log", "--format=%s"]),
105 "Initial commit",
106 );
107 assert.equal(
108 yield* gitOutput(first.workspaceRoot, ["rev-parse", "--abbrev-ref", "HEAD"]),
109 "main",
110 );
111 assert.equal(yield* gitOutput(first.workspaceRoot, ["status", "--porcelain"]), "");
112 }),
113 ),
114 ),
115 );
116​
117 it.effect("keeps the folder and reports why when Git cannot commit", () =>
118 Effect.scoped(
119 // No identity anywhere, and Git may not guess one from the host name.
120 withGitEnv(
121 {
122 GIT_CONFIG_COUNT: "1",
123 GIT_CONFIG_KEY_0: "user.useConfigOnly",
124 GIT_CONFIG_VALUE_0: "true",
125 },
126 Effect.gen(function* () {
127 const fileSystem = yield* FileSystem.FileSystem;
128 const path = yield* Path.Path;
129 const root = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-projects-" });
130​
131 const result = yield* createNewProjectFolder({ root, name: "No Identity" });
132​
133 assert.include(result.commitError ?? "", "no name or email");
134 assert.isTrue(yield* fileSystem.exists(path.join(result.workspaceRoot, "README.md")));
135 assert.isTrue(yield* fileSystem.exists(path.join(result.workspaceRoot, ".git")));
136 }),
137 ),
138 ),
139 );
140});