Skip to content

Commit

Permalink
test: update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobLinCool committed Dec 11, 2024
1 parent 52570e5 commit 063c96f
Showing 1 changed file with 39 additions and 44 deletions.
83 changes: 39 additions & 44 deletions src/_tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { ChildProcess } from "node:child_process";
import fs from "node:fs";
import { bin, install, tunnel, service } from "../lib.js";
import { Tunnel, bin, install, service } from "../lib.js";
import { describe, it, expect, beforeAll } from "vitest";

process.env.VERBOSE = "1";

describe(
"install",
{ timeout: 60_000 },
() => {
it("should install binary", async () => {
if (fs.existsSync(bin)) {
Expand All @@ -18,61 +19,55 @@ describe(
expect(fs.existsSync(bin)).toBe(true);
});
},
{ timeout: 60_000 },
);

describe(
"tunnel",
{ timeout: 60_000 },
() => {
it("should create a tunnel", async () => {
const { url, connections, child, stop } = tunnel({
"--url": "localhost:8080",
"--no-autoupdate": "true",
});
const tunnel = new Tunnel(["tunnel", "--url", "localhost:8080", "--no-autoupdate"]);
const url = new Promise((resolve) => tunnel.once("url", resolve));
expect(await url).toMatch(/https?:\/\/[^\s]+/);
await connections[0]; // quick tunnel only has one connection
expect(child).toBeInstanceOf(ChildProcess);
stop();
const conn = new Promise((resolve) => tunnel.once("connected", resolve));
await conn; // quick tunnel only has one connection
expect(tunnel.process).toBeInstanceOf(ChildProcess);
tunnel.stop();
});
},
{ timeout: 60_000 },
);

describe(
"service",
() => {
const TOKEN = process.env.TUNNEL_TOKEN;
const should_run =
TOKEN &&
["darwin", "linux"].includes(process.platform) &&
!(process.platform === "linux" && process.getuid?.() !== 0);
if (should_run) {
beforeAll(() => {
if (service.exists()) {
service.uninstall();
}
});
}

it("should work", async (ctx) => {
if (!should_run) {
ctx.skip();
describe("service", { timeout: 60_000 }, () => {
const TOKEN = process.env.TUNNEL_TOKEN;
const should_run =
TOKEN &&
["darwin", "linux"].includes(process.platform) &&
!(process.platform === "linux" && process.getuid?.() !== 0);
if (should_run) {
beforeAll(() => {
if (service.exists()) {
service.uninstall();
}
expect(service.exists()).toBe(false);
service.install(TOKEN);
});
}

await new Promise((r) => setTimeout(r, 15_000));
it("should work", async (ctx) => {
if (!should_run) {
ctx.skip();
}
expect(service.exists()).toBe(false);
service.install(TOKEN);

expect(service.exists()).toBe(true);
const current = service.current();
expect(current.tunnelID.length).toBeGreaterThan(0);
expect(current.connectorID.length).toBeGreaterThan(0);
expect(current.connections.length).toBeGreaterThan(0);
expect(current.metrics.length).toBeGreaterThan(0);
expect(current.config.ingress?.length).toBeGreaterThan(0);
await new Promise((r) => setTimeout(r, 15_000));

service.uninstall();
});
},
{ timeout: 60_000 },
);
expect(service.exists()).toBe(true);
const current = service.current();
expect(current.tunnelID.length).toBeGreaterThan(0);
expect(current.connectorID.length).toBeGreaterThan(0);
expect(current.connections.length).toBeGreaterThan(0);
expect(current.metrics.length).toBeGreaterThan(0);
expect(current.config.ingress?.length).toBeGreaterThan(0);

service.uninstall();
});
});

0 comments on commit 063c96f

Please sign in to comment.