local test_samurai = require("test-samurai") local core = require("test-samurai.core") local function mkbuf(path, ft, lines) local bufnr = vim.api.nvim_create_buf(false, true) vim.api.nvim_buf_set_name(bufnr, path) vim.bo[bufnr].filetype = ft if lines then vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines) end return bufnr end local function capture_jobstart() local calls = {} local orig = vim.fn.jobstart vim.fn.jobstart = function(cmd, opts) table.insert(calls, { cmd = cmd, opts = opts }) return 1 end return calls, orig end describe("TSamLast", function() before_each(function() test_samurai.setup() end) it("reruns last Go command", function() local calls, orig_jobstart = capture_jobstart() local bufnr = mkbuf("/tmp/project/foo_test.go", "go", { "package main", "import \"testing\"", "", "func TestFoo(t *testing.T) {", " t.Run(\"first\", func(t *testing.T) {", " -- inside first", " })", "}", }) vim.api.nvim_set_current_buf(bufnr) vim.api.nvim_win_set_cursor(0, { 6, 0 }) core.run_nearest() core.run_last() vim.fn.jobstart = orig_jobstart assert.equals(2, #calls) assert.are.same({ "go", "test", "-json", "./", "-run", "^TestFoo/first$" }, calls[1].cmd) assert.are.same(calls[1].cmd, calls[2].cmd) assert.equals(calls[1].opts.cwd, calls[2].opts.cwd) end) it("uses go parser for TSamLast output (no raw JSON)", function() local json_line = vim.json.encode({ Action = "fail", Test = "TestHandleGet/returns_200", }) local calls = {} local orig_jobstart = vim.fn.jobstart vim.fn.jobstart = function(cmd, opts) table.insert(calls, { cmd = cmd, opts = opts }) if opts and opts.on_stdout then opts.on_stdout(1, { json_line }, nil) end if opts and opts.on_exit then opts.on_exit(1, 1, nil) end return 1 end local bufnr = mkbuf("/tmp/project/foo_last_output_test.go", "go", { "package main", "import \"testing\"", "", "func TestHandleGet(t *testing.T) {", " t.Run(\"returns_200\", func(t *testing.T) {", " -- inside test", " })", "}", }) vim.api.nvim_set_current_buf(bufnr) core.run_all() core.run_last() local out_buf = vim.api.nvim_get_current_buf() local lines = vim.api.nvim_buf_get_lines(out_buf, 0, -1, false) vim.fn.jobstart = orig_jobstart assert.equals(2, #calls) local has_raw = false for _, line in ipairs(lines) do if line == json_line then has_raw = true break end end assert.is_false(has_raw) end) it("reruns last JS command", function() local calls, orig_jobstart = capture_jobstart() local bufnr = mkbuf("/tmp/project/foo_last.test.ts", "typescript", { 'describe("outer", function() {', ' it("inner 1", function() {', " -- inside 1", " })", "", ' it("inner 2", function() {', " -- inside 2", " })", "})", }) vim.api.nvim_set_current_buf(bufnr) vim.api.nvim_win_set_cursor(0, { 7, 0 }) core.run_nearest() core.run_last() vim.fn.jobstart = orig_jobstart assert.equals(2, #calls) assert.are.same( { "npx", "jest", "--json", "/tmp/project/foo_last.test.ts", "-t", "inner 2" }, calls[1].cmd ) assert.are.same(calls[1].cmd, calls[2].cmd) assert.equals(calls[1].opts.cwd, calls[2].opts.cwd) end) end)