Skip to content

Commit

Permalink
test: Add E2E test for CodyChat command (#107)
Browse files Browse the repository at this point in the history
Co-authored-by: William Bezuidenhout <[email protected]>
Co-authored-by: TJ DeVries <[email protected]>
  • Loading branch information
3 people authored Sep 12, 2023
1 parent 1662df1 commit a6a6772
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 2 deletions.
59 changes: 59 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,62 @@ jobs:
run: |
nvim --version
nvim -l scripts/test.lua
e2e_tests:
name: e2e tests
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
include:
- os: ubuntu-22.04
rev: nightly/nvim-linux64.tar.gz
- os: ubuntu-22.04
rev: v0.9.0/nvim-linux64.tar.gz
- os: macos-latest
rev: nightly/nvim-macos.tar.gz
steps:
- uses: actions/checkout@v3
- run: date +%F > todays-date
- name: Restore from todays cache
uses: actions/cache@v3
with:
path: _neovim
key: ${{ runner.os }}-${{ matrix.rev }}-${{ hashFiles('todays-date') }}

- name: Cache rust items
uses: actions/cache@v3
with:
path: |
~/.rustup/
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

- name: Prepare
run: |
# ${{ matrix.manager }} update
# ${{ matrix.manager }} install ${{ matrix.packages }}
test -d _neovim || {
mkdir -p _neovim
curl -sL "https://github.com/neovim/neovim/releases/download/${{ matrix.rev }}" | tar xzf - --strip-components=1 -C "${PWD}/_neovim"
}
mkdir -p ~/.local/share/nvim/site/pack/vendor/start
git clone --depth 1 https://github.com/nvim-lua/plenary.nvim ~/.local/share/nvim/site/pack/vendor/start/plenary.nvim
ln -s $(pwd) ~/.local/share/nvim/site/pack/vendor/start
echo "${PWD}/_neovim/bin:${PATH}" >> $GITHUB_PATH
echo VIM="${PWD}/_neovim/share/nvim/runtime" >> $GITHUB_ENV
- name: Run build scripts
run: |
nvim -u scripts/init.lua -l build/init.lua
- name: Run tests
run: |
nvim --version
nvim -l scripts/e2e_test.lua
env:
SRC_ACCESS_TOKEN: ${{ secrets.SRC_ACCESS_TOKEN }}
SG_NVIM_E2E: true
56 changes: 56 additions & 0 deletions lua/e2e_tests/e2e_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
-- Force loading plugin files
vim.cmd [[runtime! after/plugin/cody.lua]]
vim.cmd [[runtime! after/plugin/cody-agent.lua]]

require("sg.cody.rpc").start()

require("plenary.async").tests.add_to_env()

local cody_commands = require "sg.cody.commands"
local rpc = assert(require "sg.cody.rpc", "able to load cody rpc")

-- Create a temp directory for the stable testing data used by these tests..
local tmp_dir = vim.loop.fs_mkdtemp(string.format("%s/cody-nvim-e2e-XXXXXXX", vim.loop.os_tmpdir()))
os.execute(string.format("git clone https://github.com/sourcegraph/e2e-sg.nvim %s", tmp_dir))

local find_initialized = function()
return vim.tbl_filter(function(msg)
return msg.type == "notify" and msg.method == "initialized"
end, rpc.messages)[1]
end

describe("cody e2e", function()
before_each(function()
if string.sub(vim.env.SRC_ACCESS_TOKEN, 1, 4) ~= "sgp_" then
error "Need a real token to run e2e test suite"
end

vim.cmd.cd(tmp_dir)
end)

a.it("should ask through chat what file we are in", function()
vim.wait(5000, find_initialized)

vim.cmd.edit "pool/pool.go"

vim.cmd.CodyChat()
cody_commands.focus_prompt()
local prompt_bufnr = vim.api.nvim_get_current_buf()

vim.api.nvim_buf_set_lines(prompt_bufnr, 0, -1, false, { "What file am I looking at" })
vim.cmd.CodySubmit()

cody_commands.focus_history()
local history_bufnr = vim.api.nvim_get_current_buf()

vim.wait(20000, function()
return vim.api.nvim_buf_line_count(history_bufnr) > 5
end)

local lines = table.concat(vim.api.nvim_buf_get_lines(history_bufnr, 0, -1, false), "\n")
assert(
string.find(lines, "/pool/pool.go"),
string.format("Cody told us the path to the current file:\n\n %s", lines)
)
end)
end)
3 changes: 3 additions & 0 deletions scripts/e2e_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(loadfile "./scripts/init.lua")()

require("plenary.test_harness").test_directory("lua/e2e_tests/", { minimal = "./scripts/init.lua" })
8 changes: 6 additions & 2 deletions scripts/init.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
vim.env.SG_NVIM_TESTING = "true"

vim.env.SRC_ENDPOINT = "https://sourcegraph.com"
vim.env.SRC_ACCESS_TOKEN = "testing-token-doesnt-work"
vim.env.SRC_ENDPOINT = "https://sourcegraph.sourcegraph.com"

-- when outside of github actions set the token to a fake token
if vim.env.SG_NVIM_E2E ~= "true" then
vim.env.SRC_ACCESS_TOKEN = "testing-token-doesnt-work"
end

vim.opt.rtp:append { ".", "../plenary.nvim", "../tree-sitter-lua" }

Expand Down

0 comments on commit a6a6772

Please sign in to comment.