Skip to content

Commit 063c96f

Browse files
committed
test: update tests
1 parent 52570e5 commit 063c96f

1 file changed

Lines changed: 39 additions & 44 deletions

File tree

src/_tests/index.test.ts

Lines changed: 39 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { ChildProcess } from "node:child_process";
22
import fs from "node:fs";
3-
import { bin, install, tunnel, service } from "../lib.js";
3+
import { Tunnel, bin, install, service } from "../lib.js";
44
import { describe, it, expect, beforeAll } from "vitest";
55

66
process.env.VERBOSE = "1";
77

88
describe(
99
"install",
10+
{ timeout: 60_000 },
1011
() => {
1112
it("should install binary", async () => {
1213
if (fs.existsSync(bin)) {
@@ -18,61 +19,55 @@ describe(
1819
expect(fs.existsSync(bin)).toBe(true);
1920
});
2021
},
21-
{ timeout: 60_000 },
2222
);
2323

2424
describe(
2525
"tunnel",
26+
{ timeout: 60_000 },
2627
() => {
2728
it("should create a tunnel", async () => {
28-
const { url, connections, child, stop } = tunnel({
29-
"--url": "localhost:8080",
30-
"--no-autoupdate": "true",
31-
});
29+
const tunnel = new Tunnel(["tunnel", "--url", "localhost:8080", "--no-autoupdate"]);
30+
const url = new Promise((resolve) => tunnel.once("url", resolve));
3231
expect(await url).toMatch(/https?:\/\/[^\s]+/);
33-
await connections[0]; // quick tunnel only has one connection
34-
expect(child).toBeInstanceOf(ChildProcess);
35-
stop();
32+
const conn = new Promise((resolve) => tunnel.once("connected", resolve));
33+
await conn; // quick tunnel only has one connection
34+
expect(tunnel.process).toBeInstanceOf(ChildProcess);
35+
tunnel.stop();
3636
});
3737
},
38-
{ timeout: 60_000 },
3938
);
4039

41-
describe(
42-
"service",
43-
() => {
44-
const TOKEN = process.env.TUNNEL_TOKEN;
45-
const should_run =
46-
TOKEN &&
47-
["darwin", "linux"].includes(process.platform) &&
48-
!(process.platform === "linux" && process.getuid?.() !== 0);
49-
if (should_run) {
50-
beforeAll(() => {
51-
if (service.exists()) {
52-
service.uninstall();
53-
}
54-
});
55-
}
56-
57-
it("should work", async (ctx) => {
58-
if (!should_run) {
59-
ctx.skip();
40+
describe("service", { timeout: 60_000 }, () => {
41+
const TOKEN = process.env.TUNNEL_TOKEN;
42+
const should_run =
43+
TOKEN &&
44+
["darwin", "linux"].includes(process.platform) &&
45+
!(process.platform === "linux" && process.getuid?.() !== 0);
46+
if (should_run) {
47+
beforeAll(() => {
48+
if (service.exists()) {
49+
service.uninstall();
6050
}
61-
expect(service.exists()).toBe(false);
62-
service.install(TOKEN);
51+
});
52+
}
6353

64-
await new Promise((r) => setTimeout(r, 15_000));
54+
it("should work", async (ctx) => {
55+
if (!should_run) {
56+
ctx.skip();
57+
}
58+
expect(service.exists()).toBe(false);
59+
service.install(TOKEN);
6560

66-
expect(service.exists()).toBe(true);
67-
const current = service.current();
68-
expect(current.tunnelID.length).toBeGreaterThan(0);
69-
expect(current.connectorID.length).toBeGreaterThan(0);
70-
expect(current.connections.length).toBeGreaterThan(0);
71-
expect(current.metrics.length).toBeGreaterThan(0);
72-
expect(current.config.ingress?.length).toBeGreaterThan(0);
61+
await new Promise((r) => setTimeout(r, 15_000));
7362

74-
service.uninstall();
75-
});
76-
},
77-
{ timeout: 60_000 },
78-
);
63+
expect(service.exists()).toBe(true);
64+
const current = service.current();
65+
expect(current.tunnelID.length).toBeGreaterThan(0);
66+
expect(current.connectorID.length).toBeGreaterThan(0);
67+
expect(current.connections.length).toBeGreaterThan(0);
68+
expect(current.metrics.length).toBeGreaterThan(0);
69+
expect(current.config.ingress?.length).toBeGreaterThan(0);
70+
71+
service.uninstall();
72+
});
73+
});

0 commit comments

Comments
 (0)