mirror of
https://github.com/vrtmrz/obsidian-livesync.git
synced 2026-08-30 15:27:06 +00:00
fix(cli): replace the headless log handler
This commit is contained in:
@@ -0,0 +1,55 @@
|
|||||||
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
import { mkdtemp, rm } from "node:fs/promises";
|
||||||
|
import { tmpdir } from "node:os";
|
||||||
|
import { join } from "node:path";
|
||||||
|
|
||||||
|
const mocks = vi.hoisted(() => ({
|
||||||
|
setLogHandler: vi.fn(),
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock("./services/NodeServiceHub", () => ({
|
||||||
|
NodeServiceContext: class {},
|
||||||
|
NodeServiceHub: class {
|
||||||
|
API = {
|
||||||
|
addLog: {
|
||||||
|
setHandler: mocks.setLogHandler,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
|
||||||
|
import { main } from "./main";
|
||||||
|
|
||||||
|
function createStandardIoMock() {
|
||||||
|
return {
|
||||||
|
readStdin: vi.fn(async () => ""),
|
||||||
|
prompt: vi.fn(async () => ""),
|
||||||
|
writeStdout: vi.fn(),
|
||||||
|
writeStderr: vi.fn(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("CLI log handler", () => {
|
||||||
|
const originalArgv = process.argv.slice();
|
||||||
|
let databasePath: string;
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
databasePath = await mkdtemp(join(tmpdir(), "livesync-cli-log-handler-"));
|
||||||
|
mocks.setLogHandler.mockReset();
|
||||||
|
mocks.setLogHandler.mockImplementation(() => {
|
||||||
|
throw new Error("__LOG_HANDLER_CONFIGURED__");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(async () => {
|
||||||
|
process.argv = originalArgv.slice();
|
||||||
|
await rm(databasePath, { recursive: true, force: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("replaces the default Headless API log handler", async () => {
|
||||||
|
process.argv = ["node", "livesync-cli", databasePath, "remote-ls"];
|
||||||
|
|
||||||
|
await expect(main(createStandardIoMock())).rejects.toThrow("__LOG_HANDLER_CONFIGURED__");
|
||||||
|
expect(mocks.setLogHandler).toHaveBeenCalledWith(expect.any(Function), true);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -399,7 +399,7 @@ export async function main(
|
|||||||
if (!options.verbose) return;
|
if (!options.verbose) return;
|
||||||
}
|
}
|
||||||
writeStderrLine(standardIo, prefix, message);
|
writeStderrLine(standardIo, prefix, message);
|
||||||
});
|
}, true);
|
||||||
// Prevent replication result from being processed automatically in non-daemon commands.
|
// Prevent replication result from being processed automatically in non-daemon commands.
|
||||||
// In daemon mode the default handler must run so changes are applied to the filesystem.
|
// In daemon mode the default handler must run so changes are applied to the filesystem.
|
||||||
if (options.command !== "daemon") {
|
if (options.command !== "daemon") {
|
||||||
|
|||||||
Reference in New Issue
Block a user