|
1 | 1 | describe("peekstack.picker.snacks", function() |
2 | | - local config = require("peekstack.config") |
| 2 | + local picker = require("peekstack.picker.snacks") |
| 3 | + local original_snacks = nil |
| 4 | + local original_notify = nil |
3 | 5 |
|
4 | 6 | before_each(function() |
5 | | - config.setup({ |
6 | | - picker = { backend = "snacks" }, |
7 | | - }) |
| 7 | + original_snacks = package.loaded["snacks.picker"] |
| 8 | + original_notify = vim.notify |
8 | 9 | end) |
9 | 10 |
|
10 | | - it("should warn when snacks.nvim is not available", function() |
11 | | - -- Test by checking the module behavior when snacks is not installed |
12 | | - -- The pick function should handle the missing dependency gracefully |
13 | | - local location = require("peekstack.core.location") |
| 11 | + after_each(function() |
| 12 | + package.loaded["snacks.picker"] = original_snacks |
| 13 | + vim.notify = original_notify |
| 14 | + end) |
14 | 15 |
|
15 | | - local test_loc = { |
16 | | - uri = "file:///path/to/test.lua", |
17 | | - range = { start = { line = 0, character = 0 }, ["end"] = { line = 0, character = 10 } }, |
18 | | - provider = "test_provider", |
19 | | - } |
| 16 | + it("warns when snacks.nvim is not available", function() |
| 17 | + package.loaded["snacks.picker"] = nil |
| 18 | + local warned = false |
| 19 | + vim.notify = function(msg) |
| 20 | + if msg == "snacks.nvim not available" then |
| 21 | + warned = true |
| 22 | + end |
| 23 | + end |
20 | 24 |
|
21 | | - -- Just verify the location module works correctly |
22 | | - local display_text = location.display_text(test_loc, 1) |
23 | | - assert.is_not_nil(display_text) |
24 | | - assert.is_true(type(display_text) == "string") |
| 25 | + picker.pick({}, nil, function() end) |
25 | 26 |
|
26 | | - -- The snacks picker itself will warn when snacks is not available |
27 | | - -- but we can't easily test that in isolation without mocking |
| 27 | + assert.is_true(warned) |
28 | 28 | end) |
29 | 29 |
|
30 | | - it("should format locations correctly", function() |
31 | | - local location = require("peekstack.core.location") |
32 | | - |
33 | | - local test_loc = { |
34 | | - uri = "file:///path/to/test.lua", |
35 | | - range = { start = { line = 5, character = 10 }, ["end"] = { line = 5, character = 20 } }, |
36 | | - provider = "test_provider", |
| 30 | + it("passes file format items and confirms selected location", function() |
| 31 | + local picked = nil |
| 32 | + local closed = false |
| 33 | + local captured = nil |
| 34 | + package.loaded["snacks.picker"] = { |
| 35 | + pick = function(opts) |
| 36 | + captured = opts |
| 37 | + opts.confirm({ |
| 38 | + close = function() |
| 39 | + closed = true |
| 40 | + end, |
| 41 | + }, opts.items[2]) |
| 42 | + end, |
37 | 43 | } |
38 | 44 |
|
39 | | - local display_text = location.display_text(test_loc, 1) |
40 | | - assert.is_not_nil(display_text) |
41 | | - assert.is_true(type(display_text) == "string") |
42 | | - end) |
43 | | - |
44 | | - it("should be registered as a known backend", function() |
45 | | - local known_backends = { |
46 | | - "builtin", |
47 | | - "telescope", |
48 | | - "fzf-lua", |
49 | | - "snacks", |
| 45 | + local loc1 = { |
| 46 | + uri = "file:///tmp/a.lua", |
| 47 | + range = { start = { line = 3, character = 4 }, ["end"] = { line = 3, character = 4 } }, |
| 48 | + provider = "test", |
| 49 | + } |
| 50 | + local loc2 = { |
| 51 | + uri = "file:///tmp/b.lua", |
| 52 | + range = { start = { line = 7, character = 2 }, ["end"] = { line = 7, character = 2 } }, |
| 53 | + provider = "test", |
50 | 54 | } |
51 | 55 |
|
52 | | - assert.is_true(vim.list_contains(known_backends, "snacks")) |
| 56 | + picker.pick({ loc1, loc2 }, nil, function(choice) |
| 57 | + picked = choice |
| 58 | + end) |
| 59 | + |
| 60 | + assert.equals("Peekstack", captured.title) |
| 61 | + assert.equals("file", captured.format) |
| 62 | + assert.equals("/tmp/a.lua", captured.items[1].file) |
| 63 | + assert.same({ 4, 4 }, captured.items[1].pos) |
| 64 | + assert.is_nil(captured.items[1].loc) |
| 65 | + assert.are.same(loc1, captured.items[1].peekstack_loc) |
| 66 | + assert.are.same(loc2, picked) |
| 67 | + assert.is_true(closed) |
53 | 68 | end) |
54 | 69 | end) |
0 commit comments