Skip to content

Commit

Permalink
test: Add some UI integration tests (end-to-end).
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Feb 27, 2024
1 parent 9232cf2 commit 1235ca5
Show file tree
Hide file tree
Showing 9 changed files with 329 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,11 @@ jobs:
- name: Print log
run:
cat /__w/toxic/toxic/infer-out/report.txt

integration-test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Run integration tests
run: test/run
1 change: 1 addition & 0 deletions .restyled.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ restylers:
include:
- "**/*.cc"
- "**/*.hh"
- prettier
1 change: 1 addition & 0 deletions test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.tui-test/
43 changes: 43 additions & 0 deletions test/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
FROM alpine:3.19.0

RUN ["apk", "add", "--no-cache", \
"bash", \
"cmake", \
"curl-dev", \
"g++", \
"gcc", \
"git", \
"libconfig-dev", \
"libsodium-dev", \
"libvpx-dev", \
"linux-headers", \
"make", \
"ncurses-dev", \
"opus-dev", \
"pkgconfig", \
"samurai", \
"yarn"]

WORKDIR /build
RUN yarn add --dev @microsoft/tui-test
ENV PATH=$PATH:/build/node_modules/.bin

WORKDIR /build
RUN git clone --depth=1 --recursive https://github.com/TokTok/c-toxcore /build/c-toxcore \
&& cmake -GNinja -B/build/c-toxcore/_build -H/build/c-toxcore \
-DBOOTSTRAP_DAEMON=OFF \
-DENABLE_STATIC=OFF \
-DMUST_BUILD_TOXAV=ON \
&& cmake --build /build/c-toxcore/_build --target install

WORKDIR /build/toxic
COPY Makefile /build/toxic/
COPY cfg /build/toxic/cfg/
COPY misc /build/toxic/misc/
COPY sounds /build/toxic/sounds/
COPY src /build/toxic/src/
ENV CFLAGS="-D_GNU_SOURCE -Werror"
RUN make "-j$(nproc)" install

WORKDIR /build/toxic/test
ENTRYPOINT ["tui-test"]
9 changes: 9 additions & 0 deletions test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Integration test suite

`test/run` builds a docker image with tui-test and toxic in it and then runs
integration tests, i.e. launch the toxic binary in a virtual terminal and
mechanically interacts with it via stdin/stdout I/O. At the end of a test, it
may take a "screenshot" (black and white copy of the current screen) and
compare it against goldens, similar to Jest Snapshot Testing.

To update the goldens (when changing something in the UI), run `test/run -u`.
177 changes: 177 additions & 0 deletions test/__snapshots__/toxic.test.ts.snap

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions test/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "module",
"devDependencies": {
"@microsoft/tui-test": "^0.0.1"
}
}
19 changes: 19 additions & 0 deletions test/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/sh

set -eux

docker build -t toxchat/toxic:test -f test/Dockerfile .
if [ -t 1 ]; then
docker run \
--volume "$PWD/test:/build/toxic/test" \
--tmpfs /build/toxic/test/.tui-test \
--rm \
-it \
toxchat/toxic:test "$@"
else
docker run \
--volume "$PWD/test:/build/toxic/test" \
--tmpfs /build/toxic/test/.tui-test \
--rm \
toxchat/toxic:test "$@"
fi
65 changes: 65 additions & 0 deletions test/toxic.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { test, expect, Shell } from "@microsoft/tui-test";

test.use({ shell: Shell.Bash, rows: 25, columns: 90 });

const fast = { timeout: 100 };
const slow = { timeout: 1000 };

async function startToxic(terminal: Shell.Terminal) {
terminal.write("toxic\r");
return expect(terminal.getByText("Welcome to Toxic")).toBeVisible(slow);
}

test.describe("toxic", () => {
test("toxic --help shows usage message", async ({ terminal }) => {
terminal.write("toxic --help\r");
await expect(terminal.getByText("usage: toxic")).toBeVisible(fast);
await expect(terminal).toMatchSnapshot();
});

test("toxic shows intro message", async ({ terminal }) => {
terminal.write("toxic\r");
await expect(
terminal.getByText("Would you like to encrypt it")
).toBeVisible(fast);
terminal.write("n\r");
await expect(terminal.getByText("Welcome to Toxic")).toBeVisible(slow);
await expect(terminal).toMatchSnapshot();
});

test("help window works", async ({ terminal }) => {
await startToxic(terminal);
terminal.write("/help\r");
await expect(terminal.getByText("Help Menu")).toBeVisible(fast);
await expect(terminal).toMatchSnapshot();
});

test("global help window works", async ({ terminal }) => {
await startToxic(terminal);
terminal.write("/help\r");
await expect(terminal.getByText("Help Menu")).toBeVisible(fast);
terminal.write("g");
await expect(terminal.getByText("Global Commands")).toBeVisible(fast);
await expect(terminal).toMatchSnapshot();
});

test("help window can be exited", async ({ terminal }) => {
await startToxic(terminal);
terminal.write("/help\r");
await expect(terminal.getByText("Help Menu")).toBeVisible(fast);
terminal.write("x");
await expect(terminal.getByText("Help Menu")).not.toBeVisible(fast);
await expect(terminal).toMatchSnapshot();
});

test("terminal resize works", async ({ terminal }) => {
await startToxic(terminal);
terminal.write("/help\r");
await expect(terminal.getByText("Help Menu")).toBeVisible(fast);
terminal.resize(60, 20);
await expect(terminal.getByText("Help Menu")).toBeVisible(fast);
// Wait for one of the last things to be drawn before screenshotting.
await expect(terminal.getByText("[Contacts]")).toBeVisible(fast);
await expect(terminal).toMatchSnapshot();
});
});

0 comments on commit 1235ca5

Please sign in to comment.