mirror of
https://github.com/hanzoai/dev-desktop.git
synced 2026-11-20 01:07:23 +00:00
94 lines
3.5 KiB
TypeScript
94 lines
3.5 KiB
TypeScript
import * as NodeServices from "@effect/platform-node/NodeServices";
|
|
import { assert, it } from "@effect/vitest";
|
|
import * as Effect from "effect/Effect";
|
|
import * as FileSystem from "effect/FileSystem";
|
|
import * as Path from "effect/Path";
|
|
import * as SqlClient from "effect/sql/SqlClient";
|
|
|
|
import * as NodeSqliteClient from "@hanzodev/shared/nodeSqliteClient";
|
|
import { runSqliteState } from "./hanzo-sqlite-state.ts";
|
|
import * as HostProcess from "@hanzodev/shared/HostProcess";
|
|
import { symlinksSupported } from "@hanzodev/shared/testing/symlinks";
|
|
|
|
const createFixtureDatabase = Effect.fn("createSqliteStateFixtureDatabase")(function* (
|
|
baseDir: string,
|
|
) {
|
|
const fs = yield* FileSystem.FileSystem;
|
|
const path = yield* Path.Path;
|
|
const stateDir = path.join(baseDir, "userdata");
|
|
const databasePath = path.join(stateDir, "statev2.sqlite");
|
|
yield* fs.makeDirectory(stateDir, { recursive: true });
|
|
yield* Effect.gen(function* () {
|
|
const sql = yield* SqlClient.SqlClient;
|
|
yield* sql`CREATE TABLE fixtures (id INTEGER PRIMARY KEY, label TEXT NOT NULL)`;
|
|
yield* sql`INSERT INTO fixtures (id, label) VALUES (1, 'existing')`;
|
|
}).pipe(Effect.provide(NodeSqliteClient.layer({ filename: databasePath })));
|
|
});
|
|
|
|
it.layer(NodeServices.layer)("hanzo-sqlite-state", (it) => {
|
|
it.effect("enables checkpoint_fullfsync on its direct connection", () =>
|
|
Effect.gen(function* () {
|
|
const fs = yield* FileSystem.FileSystem;
|
|
const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "hanzo-sqlite-state-fsync-" });
|
|
yield* createFixtureDatabase(baseDir);
|
|
|
|
const result = yield* runSqliteState({
|
|
operation: "query",
|
|
baseDir,
|
|
sql: "PRAGMA checkpoint_fullfsync",
|
|
});
|
|
|
|
assert.equal(result.operation, "query");
|
|
if (result.operation === "query") {
|
|
assert.deepStrictEqual(result.rows, [{ checkpoint_fullfsync: 1 }]);
|
|
}
|
|
}),
|
|
);
|
|
|
|
it.effect.skipIf(!symlinksSupported)(
|
|
"backs up isolated state before writes and refuses the shared home",
|
|
() =>
|
|
Effect.gen(function* () {
|
|
const fs = yield* FileSystem.FileSystem;
|
|
const path = yield* Path.Path;
|
|
const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "hanzo-sqlite-state-exec-" });
|
|
yield* createFixtureDatabase(baseDir);
|
|
|
|
const mutation = yield* runSqliteState({
|
|
operation: "exec",
|
|
baseDir,
|
|
sql: "INSERT INTO fixtures (id, label) VALUES (2, 'seeded')",
|
|
});
|
|
assert.equal(mutation.operation, "exec");
|
|
if (mutation.operation === "exec" && (yield* HostProcess.Platform) !== "win32") {
|
|
assert.equal((yield* fs.stat(mutation.backup)).mode & 0o777, 0o600);
|
|
}
|
|
|
|
const error = yield* runSqliteState(
|
|
{
|
|
operation: "exec",
|
|
baseDir,
|
|
sql: "DELETE FROM fixtures",
|
|
},
|
|
{ sharedHome: baseDir },
|
|
).pipe(Effect.flip);
|
|
assert.equal(error._tag, "SqliteStateSharedHomeMutationError");
|
|
|
|
const aliasParent = yield* fs.makeTempDirectoryScoped({
|
|
prefix: "hanzo-sqlite-state-alias-",
|
|
});
|
|
const aliasBaseDir = path.join(aliasParent, "shared-home-alias");
|
|
yield* fs.symlink(baseDir, aliasBaseDir);
|
|
const aliasError = yield* runSqliteState(
|
|
{
|
|
operation: "exec",
|
|
baseDir: aliasBaseDir,
|
|
sql: "DELETE FROM fixtures",
|
|
},
|
|
{ sharedHome: baseDir },
|
|
).pipe(Effect.flip);
|
|
assert.equal(aliasError._tag, "SqliteStateSharedHomeMutationError");
|
|
}),
|
|
);
|
|
});
|