Skip to content

Commit fa7280b

Browse files
fix(server): quote typographic quotes in the Windows doctor command
PowerShell also ends a double-quoted string at typographic quotes, so a base dir containing one broke the copied command. Also cover the published storage status, including the dev-path guard. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 2de6552 commit fa7280b

3 files changed

Lines changed: 83 additions & 8 deletions

File tree

‎apps/server/src/cli/invocation.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,16 @@ it.layer(NodeServices.layer)("host CLI commands", (it) => {
208208
expect((yield* hostCommand("win32", {})).command).toBe(
209209
't3 doctor "--base-dir" "C:\\Users\\Jane Doe\\.t3"',
210210
);
211+
// Typographic quotes also delimit PowerShell strings; doubling keeps them.
212+
expect(
213+
(yield* resolveHostCliCommand("doctor", ["--base-dir", "C:\\T3\\\u201CArchive\u201D"]).pipe(
214+
Effect.provideService(HostProcess.Platform, "win32"),
215+
Effect.provideService(HostProcess.Environment, { PATH: "" }),
216+
Effect.provideService(HostProcess.ExecutablePath, "C:\\t3.exe"),
217+
Effect.provideService(HostProcess.Arguments, ["C:\\t3.exe", "C:\\bin.mjs"]),
218+
Effect.provideService(HostProcess.IsExecutable, false),
219+
)).command,
220+
).toBe('t3 doctor "--base-dir" "C:\\T3\\\u201C\u201CArchive\u201D\u201D"');
211221
// POSIX hosts keep single quotes for the same input.
212222
expect((yield* hostCommand("linux", { T3CODE_CLI_PATH: shim })).command).toBe(
213223
`${shim} doctor --base-dir 'C:\\Users\\Jane Doe\\.t3'`,

‎apps/server/src/cli/invocation.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,14 @@ const shellWord = (value: string) =>
150150
/**
151151
* Quotes `value` for PowerShell, the default Windows terminal. Always quoted,
152152
* so a path reads the same whether or not it holds a space. Inside double
153-
* quotes `"` doubles, and `$` and the backtick are escaped so nothing expands.
153+
* quotes `"` and its typographic forms (which also end the string) double,
154+
* and `$` and the backtick are escaped so nothing expands.
154155
*/
155156
const powershellWord = (value: string) =>
156-
`"${value.replaceAll("`", "``").replaceAll("$", "`$").replaceAll('"', '""')}"`;
157+
`"${value
158+
.replaceAll("`", "``")
159+
.replaceAll("$", "`$")
160+
.replace(/["\u201C\u201D\u201E]/g, "$&$&")}"`;
157161

158162
/** Quotes a word for the host's shell: POSIX, or PowerShell on Windows. */
159163
const hostShellWord = (platform: NodeJS.Platform, value: string) =>

‎apps/server/src/persistence/DatabaseStorage.test.ts‎

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import * as NodeServices from "@effect/platform-node/NodeServices";
22
import { assert, it } from "@effect/vitest";
33
import * as Effect from "effect/Effect";
4+
import * as FileSystem from "effect/FileSystem";
45
import * as Layer from "effect/Layer";
6+
import * as Option from "effect/Option";
7+
import * as Path from "effect/Path";
8+
import * as Stream from "effect/Stream";
9+
import * as TestClock from "effect/testing/TestClock";
510
import * as SqlClient from "effect/sql/SqlClient";
611

712
import * as ServerConfig from "../config.ts";
@@ -19,6 +24,23 @@ const freePages = Effect.map(
1924
(rows) => rows[0]?.freelist_count ?? 0,
2025
);
2126

27+
const storageLayer = <E>(
28+
config: Layer.Layer<ServerConfig.ServerConfig, E, FileSystem.FileSystem | Path.Path>,
29+
) =>
30+
DatabaseStorage.layer.pipe(
31+
Layer.provide(ProjectionMaintenance.layer),
32+
Layer.provide(Layer.merge(EventStore.layer, ProjectionStore.layer)),
33+
Layer.provideMerge(Sqlite.layerMemory),
34+
Layer.provideMerge(config),
35+
Layer.provide(NodeServices.layer),
36+
);
37+
38+
const testConfig = ServerConfig.layerTest(process.cwd(), { prefix: "database-storage-" });
39+
40+
const currentStatus = Effect.flatMap(DatabaseStorage.DatabaseStorage, (storage) =>
41+
storage.status.pipe(Stream.take(1), Stream.runHead),
42+
);
43+
2244
it.effect("a new database returns freed pages online without losing data", () =>
2345
Effect.gen(function* () {
2446
const sql = yield* SqlClient.SqlClient;
@@ -33,14 +55,53 @@ it.effect("a new database returns freed pages online without losing data", () =>
3355
yield* storage.reclaim;
3456
assert.equal(yield* freePages, 0);
3557
assert.deepEqual(yield* sql`SELECT value FROM retained`, [{ value: "keep" }]);
58+
}).pipe(Effect.provide(storageLayer(testConfig))),
59+
);
60+
61+
it.effect("publishes file accounting and the doctor command after each pass", () =>
62+
Effect.gen(function* () {
63+
const sql = yield* SqlClient.SqlClient;
64+
const config = yield* ServerConfig.ServerConfig;
65+
yield* sql`CREATE TABLE discarded (value BLOB)`;
66+
yield* sql`INSERT INTO discarded VALUES (zeroblob(1048576))`;
67+
yield* TestClock.adjust("1 minute");
68+
const before = (yield* currentStatus).pipe(Option.getOrThrow);
69+
assert.isNotNull(before);
70+
assert.isAbove(before!.databaseBytes, 1048576);
71+
assert.isTrue(before!.incrementalVacuumEnabled);
72+
assert.include(before!.compactCommand, "doctor --base-dir");
73+
assert.include(before!.compactCommand, config.baseDir);
74+
75+
// The next reclaim pass frees the dropped table and republishes.
76+
yield* sql`DROP TABLE discarded`;
77+
yield* TestClock.adjust("1 minute");
78+
const storage = yield* DatabaseStorage.DatabaseStorage;
79+
const after = yield* storage.status.pipe(
80+
Stream.filter((status) => status !== null && status.databaseBytes < before!.databaseBytes),
81+
Stream.runHead,
82+
Effect.map(Option.getOrThrow),
83+
);
84+
assert.isAtMost(after!.databaseBytes, before!.databaseBytes - 1048576);
85+
assert.equal(after!.reclaimableBytes, 0);
86+
}).pipe(Effect.provide(storageLayer(testConfig))),
87+
);
88+
89+
it.effect("publishes nothing when doctor would repair a different database", () =>
90+
Effect.gen(function* () {
91+
yield* TestClock.adjust("1 minute");
92+
assert.deepEqual(yield* currentStatus, Option.some(null));
3693
}).pipe(
3794
Effect.provide(
38-
DatabaseStorage.layer.pipe(
39-
Layer.provide(ProjectionMaintenance.layer),
40-
Layer.provide(Layer.merge(EventStore.layer, ProjectionStore.layer)),
41-
Layer.provideMerge(Sqlite.layerMemory),
42-
Layer.provide(ServerConfig.layerTest(process.cwd(), { prefix: "database-storage-" })),
43-
Layer.provide(NodeServices.layer),
95+
storageLayer(
96+
// A dev server without an explicit home keeps state in <baseDir>/dev,
97+
// while `doctor --base-dir` targets <baseDir>/userdata.
98+
Layer.effect(
99+
ServerConfig.ServerConfig,
100+
Effect.gen(function* () {
101+
const config = yield* ServerConfig.ServerConfig;
102+
return { ...config, dbPath: `${config.baseDir}/dev/statev2.sqlite` };
103+
}),
104+
).pipe(Layer.provide(testConfig)),
44105
),
45106
),
46107
),

0 commit comments

Comments
 (0)