mirror of
https://github.com/hanzoai/dev-desktop.git
synced 2026-11-23 05:52:02 +00:00
122 lines
4.7 KiB
TypeScript
122 lines
4.7 KiB
TypeScript
import { assert, it, describe } from "@effect/vitest";
|
|
import * as NodeServices from "@effect/platform-node/NodeServices";
|
|
import * as Effect from "effect/Effect";
|
|
import * as FileSystem from "effect/FileSystem";
|
|
import * as Layer from "effect/Layer";
|
|
import * as Logger from "effect/Logger";
|
|
import * as Path from "effect/Path";
|
|
|
|
import * as VcsProjectConfig from "./VcsProjectConfig.ts";
|
|
|
|
const layerTest = VcsProjectConfig.layer.pipe(
|
|
Layer.provide(NodeServices.layer),
|
|
Layer.provideMerge(NodeServices.layer),
|
|
);
|
|
|
|
describe("VcsProjectConfig", () => {
|
|
it.layer(layerTest)("discovers .hanzo/vcs.json from nested workspaces", (it) => {
|
|
it.effect("returns the configured kind", () =>
|
|
Effect.gen(function* () {
|
|
const fileSystem = yield* FileSystem.FileSystem;
|
|
const path = yield* Path.Path;
|
|
const root = yield* fileSystem.makeTempDirectoryScoped({
|
|
prefix: "hanzo-vcs-config-test-",
|
|
});
|
|
const configDir = path.join(root, ".hanzodev");
|
|
const nested = path.join(root, "packages", "app");
|
|
yield* fileSystem.makeDirectory(configDir, { recursive: true });
|
|
yield* fileSystem.makeDirectory(nested, { recursive: true });
|
|
yield* fileSystem.writeFileString(
|
|
path.join(configDir, "vcs.json"),
|
|
JSON.stringify({ vcs: { kind: "jj" } }),
|
|
);
|
|
|
|
const config = yield* VcsProjectConfig.VcsProjectConfig;
|
|
const kind = yield* config.resolveKind({ cwd: nested });
|
|
|
|
assert.equal(kind, "jj");
|
|
}),
|
|
);
|
|
});
|
|
|
|
it.layer(layerTest)("continues to parent configs after a candidate inspect failure", (it) => {
|
|
it.effect("logs the failed candidate and returns the parent config", () => {
|
|
const messages: unknown[] = [];
|
|
const logger = Logger.make<unknown, void>(({ message }) => {
|
|
messages.push(message);
|
|
});
|
|
|
|
return Effect.gen(function* () {
|
|
const fileSystem = yield* FileSystem.FileSystem;
|
|
const path = yield* Path.Path;
|
|
const root = yield* fileSystem.makeTempDirectoryScoped({
|
|
prefix: "hanzo-vcs-config-test-",
|
|
});
|
|
const configDir = path.join(root, ".hanzodev");
|
|
const cwd = path.join(root, "invalid\0child");
|
|
yield* fileSystem.makeDirectory(configDir, { recursive: true });
|
|
yield* fileSystem.writeFileString(
|
|
path.join(configDir, "vcs.json"),
|
|
JSON.stringify({ vcs: { kind: "jj" } }),
|
|
);
|
|
|
|
const config = yield* VcsProjectConfig.VcsProjectConfig;
|
|
const kind = yield* config.resolveKind({ cwd });
|
|
|
|
assert.equal(kind, "jj");
|
|
const failedCandidate = path.join(cwd, ".hanzodev", "vcs.json");
|
|
const [error] = messages[0] as ReadonlyArray<unknown>;
|
|
assert.instanceOf(error, VcsProjectConfig.VcsProjectConfigError);
|
|
assert.equal(
|
|
error.message,
|
|
"Failed to inspect VCS project config at " + failedCandidate + ".",
|
|
);
|
|
assert.deepInclude(error, {
|
|
operation: "inspect",
|
|
cwd,
|
|
configPath: failedCandidate,
|
|
_tag: "VcsProjectConfigError",
|
|
});
|
|
}).pipe(Effect.provide(Logger.layer([logger], { mergeWithExisting: false })));
|
|
});
|
|
});
|
|
|
|
it.layer(layerTest)("falls back to auto when config JSON is malformed", (it) => {
|
|
it.effect("returns auto and logs the failed operation and path", () => {
|
|
const messages: unknown[] = [];
|
|
const logger = Logger.make<unknown, void>(({ message }) => {
|
|
messages.push(message);
|
|
});
|
|
|
|
return Effect.gen(function* () {
|
|
const fileSystem = yield* FileSystem.FileSystem;
|
|
const path = yield* Path.Path;
|
|
const root = yield* fileSystem.makeTempDirectoryScoped({
|
|
prefix: "hanzo-vcs-config-test-",
|
|
});
|
|
const configDir = path.join(root, ".hanzodev");
|
|
yield* fileSystem.makeDirectory(configDir, { recursive: true });
|
|
yield* fileSystem.writeFileString(path.join(configDir, "vcs.json"), "{not json");
|
|
|
|
const config = yield* VcsProjectConfig.VcsProjectConfig;
|
|
const kind = yield* config.resolveKind({ cwd: root });
|
|
|
|
assert.equal(kind, "auto");
|
|
const [error] = messages[0] as ReadonlyArray<unknown>;
|
|
assert.instanceOf(error, VcsProjectConfig.VcsProjectConfigError);
|
|
assert.equal(
|
|
error.message,
|
|
"Failed to decode VCS project config at " + path.join(configDir, "vcs.json") + ".",
|
|
);
|
|
assert.deepInclude(error.cause, { _tag: "SchemaError" });
|
|
assert.deepInclude(error, {
|
|
operation: "decode",
|
|
cwd: root,
|
|
configPath: path.join(configDir, "vcs.json"),
|
|
_tag: "VcsProjectConfigError",
|
|
});
|
|
}).pipe(Effect.provide(Logger.layer([logger], { mergeWithExisting: false })));
|
|
});
|
|
});
|
|
});
|