1import { assert, assertEquals, assertFalse } from "@std/assert";2import { looksLikeTicket } from "../src/ticket.ts";3import {4 decodeClose,5 decodeFrame,6 encodeClose,7 encodeFrame,8 FRAME_BIN,9 FRAME_CLOSE,10 FRAME_TEXT,11} from "../src/tunnel.ts";1213Deno.test("ticket shape check accepts plausible tickets, rejects junk", () => {14 assert(looksLikeTicket("endpointabcdefghijklmnopqrstuvwxyz234567abcdef"));15 assert(looksLikeTicket(" endpointabcdefghijklmnopqrstuvwxyz234567 "), "tolerates whitespace");16 assertFalse(looksLikeTicket(""));17 assertFalse(looksLikeTicket("short"));18 assertFalse(looksLikeTicket("two words that are definitely not a ticket at all"));19});2021Deno.test("frame round-trips type + payload", () => {22 const payload = crypto.getRandomValues(new Uint8Array(1024));23 for (const type of [FRAME_TEXT, FRAME_BIN, FRAME_CLOSE]) {24 const decoded = decodeFrame(encodeFrame(type, payload));25 assertEquals(decoded.type, type);26 assertEquals(new Uint8Array(decoded.payload), payload);27 }28});2930Deno.test("empty frame decodes to sentinel type", () => {31 assertEquals(decodeFrame(new Uint8Array(0)).type, -1);32});3334Deno.test("close payload round-trips code and reason", () => {35 const { code, reason } = decodeClose(encodeClose(4321, "going away"));36 assertEquals(code, 4321);37 assertEquals(reason, "going away");38 assertEquals(decodeClose(new Uint8Array(0)), { code: 1000, reason: "" });39});