Skip to content

Commit

Permalink
fixup tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dlants committed Dec 27, 2024
1 parent 241f8f1 commit b896b5c
Show file tree
Hide file tree
Showing 9 changed files with 597 additions and 424 deletions.
498 changes: 498 additions & 0 deletions bun/src/chat/__snapshots__/chat.spec.ts.snap

Large diffs are not rendered by default.

440 changes: 40 additions & 400 deletions bun/src/chat/chat.spec.ts

Large diffs are not rendered by default.

12 changes: 8 additions & 4 deletions bun/src/tea/tea.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,14 @@ export function createApp<Model, Msg, SubscriptionType extends string>({
});

for (const vimKey of BINDING_KEYS) {
await context.nvim.call("nvim_exec_lua", [
`require('magenta').listenToBufKey(${mount.buffer.id}, "${vimKey}")`,
[],
]);
try {
await context.nvim.call("nvim_exec_lua", [
`require('magenta').listenToBufKey(${mount.buffer.id}, "${vimKey}")`,
[],
]);
} catch (e) {
throw new Error(`failed to nvim_exec_lua: ${JSON.stringify(e)}`);
}
}

return {
Expand Down
3 changes: 1 addition & 2 deletions bun/src/tea/util.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/no-floating-promises */
import type { Nvim } from "bunvim";
import { NeovimTestHelper } from "../../test/preamble.ts";
import * as assert from "node:assert";
Expand All @@ -11,7 +10,7 @@ import {
import { pos } from "./view.ts";
import { NvimBuffer, type Line } from "../nvim/buffer.ts";

describe.only("tea/util.spec.ts", () => {
describe("tea/util.spec.ts", () => {
let helper: NeovimTestHelper;
let nvim: Nvim;
let buffer: NvimBuffer;
Expand Down
4 changes: 2 additions & 2 deletions bun/src/tea/view.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as assert from "assert";
import { describe, it, beforeAll, beforeEach, afterEach } from "bun:test";
import { NvimBuffer, type Line } from "../nvim/buffer.ts";

describe.only("Neovim Plugin Tests", async () => {
describe("Neovim Plugin Tests", async () => {
let helper: NeovimTestHelper;

beforeAll(() => {
Expand All @@ -19,7 +19,7 @@ describe.only("Neovim Plugin Tests", async () => {
helper.stopNvim();
});

it.only("basic rendering & update", async () => {
it("basic rendering & update", async () => {
const buffer = await NvimBuffer.create(false, true);
await buffer.setLines({ start: 0, end: 0, lines: [""] as Line[] });

Expand Down
28 changes: 17 additions & 11 deletions bun/src/tools/listBuffers.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
/* eslint-disable @typescript-eslint/no-floating-promises */
import { NeovimTestHelper } from "../../test/preamble.ts";
import * as ListBuffers from "./listBuffers.ts";
import * as assert from "assert";
import { type ToolRequestId } from "./toolManager.ts";
import { createApp } from "../tea/tea.ts";
import { describe, it, beforeAll, beforeEach, afterEach } from "bun:test";
import {
describe,
it,
beforeAll,
beforeEach,
afterEach,
expect,
} from "bun:test";
import { pos } from "../tea/view.ts";
import { NvimBuffer } from "../nvim/buffer.ts";

Expand Down Expand Up @@ -48,11 +53,13 @@ describe("tea/listBuffers.spec.ts", () => {

await mountedApp.waitForRender();

assert.equal(
(await buffer.getLines({ start: 0, end: -1 })).join("\n"),
`⚙️ Grabbing buffers...`,
"initial render of list buffers tool is as expected",
);
const content = (await buffer.getLines({ start: 0, end: -1 })).join("\n");

expect(
content,
// "initial render of list buffers tool is as expected",
).toBe(`⚙️ Grabbing buffers...`);

app.dispatch({
type: "finish",
result: {
Expand All @@ -63,10 +70,9 @@ describe("tea/listBuffers.spec.ts", () => {
});

await mountedApp.waitForRender();
assert.equal(
expect(
(await buffer.getLines({ start: 0, end: -1 })).join("\n"),
`✅ Finished getting buffers.`,
"initialRender is as expected",
);
).toBe(`✅ Finished getting buffers.`);
});
});
29 changes: 24 additions & 5 deletions bun/test/preamble.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { type MountedVDOM } from "../src/tea/view.ts";
import { assertUnreachable } from "../src/utils/assertUnreachable.ts";
import { setContext } from "../src/context.ts";
import { Lsp } from "../src/lsp.ts";
import path from "path";

export const MAGENTA_SOCK = "/tmp/magenta-test.sock";

Expand All @@ -25,19 +26,25 @@ export class NeovimTestHelper {

this.nvimProcess = spawn(
"nvim",
["--headless", "-n", "--clean", "--listen", MAGENTA_SOCK],
[
"--headless",
"-n",
"--clean",
"--listen",
MAGENTA_SOCK,
"-u",
"minimal-init.lua",
],
{
stdio: ["pipe", "pipe", "pipe"],
// root dir relative to this file
cwd: path.resolve(path.dirname(__filename), "../../"),
},
);

if (!this.nvimProcess.pid) {
throw new Error("Failed to start nvim process");
}

this.nvimProcess.stdout!.pipe(process.stdout);
this.nvimProcess.stderr!.pipe(process.stderr);

this.nvimProcess.on("error", (err) => {
reject(err);
});
Expand All @@ -58,6 +65,13 @@ export class NeovimTestHelper {
logging: { level: "debug" },
});

await this.nvimClient.call("nvim_exec_lua", [
`\
require('magenta').bridge(${this.nvimClient.channelId})
`,
[],
]);

setContext({
nvim: this.nvimClient,
lsp: new Lsp(this.nvimClient),
Expand Down Expand Up @@ -113,3 +127,8 @@ export function extractMountTree(mounted: MountedVDOM): unknown {
assertUnreachable(mounted);
}
}

process.on("uncaughtException", (err) => {
console.error(err);
process.exit(1);
});
5 changes: 5 additions & 0 deletions lua/magenta/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ M.setup = function()
vim.api.nvim_set_keymap("n", "<leader>m", ":Magenta toggle<CR>", {silent = true, noremap = true})
end

M.testSetup = function()
-- do not start. The test runner will start the process for us.
vim.api.nvim_set_keymap("n", "<leader>m", ":Magenta toggle<CR>", {silent = true, noremap = true})
end

M.start = function(silent)
if not silent then
vim.notify("magenta: init", vim.log.levels.INFO)
Expand Down
2 changes: 2 additions & 0 deletions minimal-init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vim.opt.runtimepath:append(".")
require("magenta")

0 comments on commit b896b5c

Please sign in to comment.