|
| 1 | +import type { Tunnel, TunnelOptions } from "@hongminhee/localtunnel"; |
| 2 | +import { assert, assertEquals, assertFalse, assertRejects } from "@std/assert"; |
| 3 | +import { assertSpyCall, stub } from "@std/testing/mock"; |
| 4 | +import type { Ora } from "ora"; |
| 5 | +import { command, tunnelAction } from "./tunnel.ts"; |
| 6 | + |
| 7 | +Deno.test("tunnel description", () => { |
| 8 | + // Test that the command is properly configured |
| 9 | + assert( |
| 10 | + command.getDescription().includes( |
| 11 | + "Expose a local HTTP server to the public internet using a secure tunnel.\n\n" + |
| 12 | + "Note that the HTTP requests through the tunnel have X-Forwarded-* headers.", |
| 13 | + ), |
| 14 | + ); |
| 15 | +}); |
| 16 | + |
| 17 | +Deno.test("tunnel command validates port argument", async () => { |
| 18 | + const exitStub = stub(Deno, "exit", () => { |
| 19 | + throw new Error("Process would exit"); |
| 20 | + }); |
| 21 | + |
| 22 | + try { |
| 23 | + await assertRejects( |
| 24 | + () => command.parse(["invalid-port"]), |
| 25 | + Error, |
| 26 | + "Process would exit", |
| 27 | + ); |
| 28 | + assertSpyCall(exitStub, 0, { args: [2] }); |
| 29 | + } finally { |
| 30 | + exitStub.restore(); |
| 31 | + } |
| 32 | +}); |
| 33 | + |
| 34 | +Deno.test("tunnel successfully creates and manages tunnel", async () => { |
| 35 | + // Track function calls |
| 36 | + let openTunnelCalled = false; |
| 37 | + let openTunnelArgs: TunnelOptions[] = []; |
| 38 | + let startCalled = false; |
| 39 | + let succeedCalled = false; |
| 40 | + let succeedArgs: string[] = []; |
| 41 | + let logArgs: string[] = []; |
| 42 | + let errorArgs: string[] = []; |
| 43 | + let addSignalListenerCalled = false; |
| 44 | + let exitCalled = false; |
| 45 | + |
| 46 | + // Create a mock tunnel object |
| 47 | + const mockTunnel = { |
| 48 | + url: new URL("https://abc123.localhost.run"), |
| 49 | + localPort: 3000, |
| 50 | + pid: 12345, |
| 51 | + close: () => Promise.resolve(), |
| 52 | + }; |
| 53 | + |
| 54 | + // Create mock dependencies |
| 55 | + const mockDeps = { |
| 56 | + openTunnel: (args: TunnelOptions) => { |
| 57 | + openTunnelCalled = true; |
| 58 | + openTunnelArgs = [args]; |
| 59 | + return Promise.resolve(mockTunnel as Tunnel); |
| 60 | + }, |
| 61 | + ora: () => ({ |
| 62 | + start() { |
| 63 | + startCalled = true; |
| 64 | + return this; |
| 65 | + }, |
| 66 | + succeed(...args: string[]) { |
| 67 | + succeedCalled = true; |
| 68 | + succeedArgs = args; |
| 69 | + return this; |
| 70 | + }, |
| 71 | + fail() { |
| 72 | + return this; |
| 73 | + }, |
| 74 | + } as unknown as Ora), |
| 75 | + console: { |
| 76 | + log: (...args: string[]) => { |
| 77 | + logArgs = args; |
| 78 | + }, |
| 79 | + error: (...args: string[]) => { |
| 80 | + errorArgs = args; |
| 81 | + }, |
| 82 | + } as Console, |
| 83 | + addSignalListener: (() => { |
| 84 | + addSignalListenerCalled = true; |
| 85 | + }) as typeof Deno.addSignalListener, |
| 86 | + exit: (() => { |
| 87 | + exitCalled = true; |
| 88 | + }) as typeof Deno.exit, |
| 89 | + }; |
| 90 | + |
| 91 | + await tunnelAction({ service: undefined }, 3000, mockDeps); |
| 92 | + |
| 93 | + // Verify all the expected interactions occurred |
| 94 | + assert(openTunnelCalled); |
| 95 | + assertEquals(openTunnelArgs, [{ port: 3000, service: undefined }]); |
| 96 | + assert(startCalled); |
| 97 | + assert(succeedCalled); |
| 98 | + assertEquals(succeedArgs, [ |
| 99 | + "Your local server at 3000 is now publicly accessible:\n", |
| 100 | + ]); |
| 101 | + assertEquals(logArgs, ["https://abc123.localhost.run/"]); |
| 102 | + assertEquals(errorArgs, ["\nPress ^C to close the tunnel."]); |
| 103 | + assert(addSignalListenerCalled); |
| 104 | + assertFalse(exitCalled); |
| 105 | +}); |
| 106 | + |
| 107 | +Deno.test("tunnel fails to create a secure tunnel and handles error", async () => { |
| 108 | + const exitStub = stub(Deno, "exit", () => { |
| 109 | + throw new Error("Process would exit"); |
| 110 | + }); |
| 111 | + |
| 112 | + // Track function calls |
| 113 | + let openTunnelCalled = false; |
| 114 | + let openTunnelArgs: TunnelOptions[] = []; |
| 115 | + let startCalled = false; |
| 116 | + let failCalled = false; |
| 117 | + let failArgs: string[] = []; |
| 118 | + let addSignalListenerCalled = false; |
| 119 | + |
| 120 | + const tunnelError = new Error("Failed to create a secure tunnel."); |
| 121 | + |
| 122 | + // Create mock dependencies that simulate failure |
| 123 | + const mockDeps = { |
| 124 | + openTunnel: (args: TunnelOptions) => { |
| 125 | + openTunnelCalled = true; |
| 126 | + openTunnelArgs = [args]; |
| 127 | + return Promise.reject(tunnelError); |
| 128 | + }, |
| 129 | + ora: () => ({ |
| 130 | + start() { |
| 131 | + startCalled = true; |
| 132 | + return this; |
| 133 | + }, |
| 134 | + succeed() { |
| 135 | + return this; |
| 136 | + }, |
| 137 | + fail(...args: string[]) { |
| 138 | + failCalled = true; |
| 139 | + failArgs = args; |
| 140 | + return this; |
| 141 | + }, |
| 142 | + } as unknown as Ora), |
| 143 | + console: { |
| 144 | + log: () => {}, |
| 145 | + error: () => {}, |
| 146 | + } as Console, |
| 147 | + addSignalListener: (() => { |
| 148 | + addSignalListenerCalled = true; |
| 149 | + }) as typeof Deno.addSignalListener, |
| 150 | + exit: (() => { |
| 151 | + throw new Error("Process would exit"); |
| 152 | + }) as typeof Deno.exit, |
| 153 | + }; |
| 154 | + |
| 155 | + try { |
| 156 | + await assertRejects( |
| 157 | + () => tunnelAction({ service: undefined }, 3000, mockDeps), |
| 158 | + Error, |
| 159 | + "Process would exit", |
| 160 | + ); |
| 161 | + } finally { |
| 162 | + exitStub.restore(); |
| 163 | + } |
| 164 | + |
| 165 | + // Verify error handling interactions |
| 166 | + assert(openTunnelCalled); |
| 167 | + assertEquals(openTunnelArgs, [{ port: 3000, service: undefined }]); |
| 168 | + assert(startCalled); |
| 169 | + assert(failCalled); |
| 170 | + assertEquals(failArgs, ["Failed to create a secure tunnel."]); |
| 171 | + assertFalse(addSignalListenerCalled); |
| 172 | +}); |
0 commit comments