// Unit tests for the ported ServerUrl validation, run with `node --test`. // Mirrors the cases in android/.../test/.../ServerUrlTest.kt. import { test } from "node:test"; import assert from "node:assert/strict"; import { validateServerUrl, resolveArtifactUrl, ServerUrlKind } from "../src/serverUrl.js"; import { Protocol, resolveTarget, isTerminal } from "../src/protocol.js"; test("defaults a bare host to /connect over ws", () => { const v = validateServerUrl("ws://192.168.1.20:8081"); assert.equal(v.valid, true); assert.equal(v.wsUrl, "ws://192.168.1.20:8081/connect"); assert.equal(v.httpUrl, "http://192.168.1.20:8081/connect"); assert.equal(v.host, "192.168.1.20"); assert.equal(v.port, 8081); assert.equal(v.secure, false); }); test("treats a lone / as the default endpoint path", () => { const v = validateServerUrl("ws://host:1234/"); assert.equal(v.valid, true); assert.equal(v.wsUrl, "ws://host:1234/connect"); }); test("wss maps to https and defaults the port to 443", () => { const v = validateServerUrl("wss://example.test/connect"); assert.equal(v.valid, true); assert.equal(v.secure, true); assert.equal(v.port, 443); assert.equal(v.httpUrl, "https://example.test/connect"); }); test("rejects empty, non-ws schemes, userinfo and fragments", () => { assert.equal(validateServerUrl("").kind, ServerUrlKind.EMPTY); assert.equal(validateServerUrl(" ").kind, ServerUrlKind.EMPTY); assert.equal(validateServerUrl("http://host:8080/connect").kind, ServerUrlKind.UNSUPPORTED_SCHEME); assert.equal(validateServerUrl("ftp://host").kind, ServerUrlKind.UNSUPPORTED_SCHEME); assert.equal(validateServerUrl("ws://user:pass@host:8080/connect").kind, ServerUrlKind.USER_INFO); assert.equal(validateServerUrl("ws://host:8080/connect#frag").kind, ServerUrlKind.FRAGMENT); }); test("rejects malformed and hostless input", () => { assert.equal(validateServerUrl("not a url").kind, ServerUrlKind.MALFORMED); const hostless = validateServerUrl("ws://"); assert.ok(hostless.kind === ServerUrlKind.MALFORMED || hostless.kind === ServerUrlKind.MISSING_HOST); assert.equal(hostless.valid, false); }); test("preserves a query string", () => { const v = validateServerUrl("ws://host:8080/connect?token=abc"); assert.equal(v.valid, true); assert.equal(v.wsUrl, "ws://host:8080/connect?token=abc"); }); test("resolveArtifactUrl maps ws to http and drops /connect", () => { assert.equal( resolveArtifactUrl("ws://10.0.0.249:8080/connect", "/artifacts/55/4/shot.png"), "http://10.0.0.249:8080/artifacts/55/4/shot.png", ); }); test("resolveArtifactUrl uses https and omits the default port for wss", () => { assert.equal( resolveArtifactUrl("wss://example.test/connect", "artifacts/1/1/a.png"), "https://example.test/artifacts/1/1/a.png", ); }); test("resolveArtifactUrl returns an absolute url unchanged and null when unconfigured", () => { assert.equal( resolveArtifactUrl("ws://host:8080/connect", "https://cdn.test/a.png"), "https://cdn.test/a.png", ); assert.equal(resolveArtifactUrl("", "/artifacts/1/1/a.png"), null); assert.equal(resolveArtifactUrl("ws://host:8080/connect", " "), null); }); test("protocol target resolution mirrors the server shorthand", () => { assert.equal(resolveTarget("error", "#"), Protocol.FAILURE_END); assert.equal(resolveTarget("success", "#"), Protocol.SUCCESS_END); assert.equal(resolveTarget("error", "cleanup"), "cleanup"); assert.equal(isTerminal("#success"), true); assert.equal(isTerminal("#failure"), true); assert.equal(isTerminal("cleanup"), false); });