From ce8707c6181a127ba615a990798cc62112e82dd6 Mon Sep 17 00:00:00 2001 From: vybe Date: Wed, 20 May 2026 19:21:17 +0000 Subject: [PATCH] feat: add buildToggleCommand for safe toggle command construction Replaces inline ternary in QML with a pure function that returns the correct tailscale up/down command based on connection state. --- tailscalectl/lib.js | 6 +++++- test/lib.test.js | 17 ++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/tailscalectl/lib.js b/tailscalectl/lib.js index 2c3eda4..563de83 100644 --- a/tailscalectl/lib.js +++ b/tailscalectl/lib.js @@ -38,6 +38,10 @@ function buildCopyCommand(text, clipboardCmd) { return ["sh", "-c", "printf '%s' '" + escaped + "' | " + clipboardCmd] } +function buildToggleCommand(isConnected) { + return isConnected ? ["tailscale", "down"] : ["tailscale", "up"] +} + function parseClipboardDetection(stdout) { var trimmed = typeof stdout === "string" ? stdout.trim() : "" return validateClipboardCmd(trimmed) ? trimmed : "none" @@ -57,5 +61,5 @@ function errorMessage(cmd) { // CommonJS export for Node.js tests (ignored by QML) if (typeof module !== "undefined" && module.exports) { - module.exports = { parsePeers, makeExitNodeCommand, findActiveExitNode, errorMessage, validateClipboardCmd, buildCopyCommand, parseClipboardDetection } + module.exports = { parsePeers, makeExitNodeCommand, findActiveExitNode, errorMessage, validateClipboardCmd, buildCopyCommand, parseClipboardDetection, buildToggleCommand } } diff --git a/test/lib.test.js b/test/lib.test.js index edc92a9..c5b5098 100644 --- a/test/lib.test.js +++ b/test/lib.test.js @@ -1,7 +1,7 @@ import { test } from "node:test" import assert from "node:assert" import lib from "../tailscalectl/lib.js" -const { parsePeers, makeExitNodeCommand, findActiveExitNode, errorMessage, validateClipboardCmd, buildCopyCommand, parseClipboardDetection } = lib +const { parsePeers, makeExitNodeCommand, findActiveExitNode, errorMessage, validateClipboardCmd, buildCopyCommand, parseClipboardDetection, buildToggleCommand } = lib test("parsePeers extracts exitNode from ExitNodeOption", () => { const peerMap = { @@ -143,3 +143,18 @@ test("parseClipboardDetection falls back to none for unexpected strings", () => assert.strictEqual(parseClipboardDetection("xclip -selection clipboard"), "none") assert.strictEqual(parseClipboardDetection("custom-tool"), "none") }) + +// --- buildToggleCommand --- + +test("buildToggleCommand returns down command when connected", () => { + assert.deepStrictEqual(buildToggleCommand(true), ["tailscale", "down"]) +}) + +test("buildToggleCommand returns up command when disconnected", () => { + assert.deepStrictEqual(buildToggleCommand(false), ["tailscale", "up"]) +}) + +test("buildToggleCommand treats null and undefined as disconnected", () => { + assert.deepStrictEqual(buildToggleCommand(null), ["tailscale", "up"]) + assert.deepStrictEqual(buildToggleCommand(undefined), ["tailscale", "up"]) +})