@pi-chamber/web 1.0.4-rc.4 → 1.0.5-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/bin/cli.js +21 -2
- package/bin/cli.test.js +164 -4
- package/bin/lib/DOCUMENTATION.md +3 -3
- package/bin/lib/cli-args.js +96 -13
- package/bin/lib/commands-connect-url-tailscale.test.js +136 -0
- package/bin/lib/commands-connect-url.js +146 -39
- package/bin/lib/commands-connect-url.test.js +126 -0
- package/bin/lib/commands-serve.js +45 -2
- package/bin/lib/commands-status.js +28 -5
- package/bin/lib/commands-status.test.js +107 -0
- package/bin/lib/commands-tunnel.js +36 -10
- package/dist/assets/{DiagramView--BGR3PRZ.js → DiagramView-D-OSWTbO.js} +1 -1
- package/dist/assets/DiffView-BKT-MIH4.js +18 -0
- package/dist/assets/{FileSaveConflictDialog-CcrWHRvd.js → FileSaveConflictDialog-DBkVFssC.js} +2 -2
- package/dist/assets/FilesView-B7GIuRlh.js +6 -0
- package/dist/assets/{GitHubDetailScaffold-BZhP67Va.js → GitHubDetailScaffold-C9rCZMUT.js} +2 -2
- package/dist/assets/{GitHubLinkPicker-CJZmDljp.js → GitHubLinkPicker-Bm09hvhJ.js} +1 -1
- package/dist/assets/{GitHubListPrimitives-DhF42guB.js → GitHubListPrimitives-DlhAlxI6.js} +1 -1
- package/dist/assets/{GitView-JK-QCK9R.js → GitView-BgqdMb0m.js} +4 -4
- package/dist/assets/{IssuesSurface-CAqQzrsv.js → IssuesSurface-BZXAvt49.js} +3 -3
- package/dist/assets/{JsonTreeView-CKTqB1CD.js → JsonTreeView-BKgCQjh_.js} +1 -1
- package/dist/assets/{MarkdownRendererImpl-B5kJy9Nt.js → MarkdownRendererImpl-BdcyMPw-.js} +1 -1
- package/dist/assets/{PatchDiff-BQnRFN30.js → PatchDiff-2MmWXcGe.js} +1 -1
- package/dist/assets/PierreDiffViewer-D1F2WvD0.js +135 -0
- package/dist/assets/PullRequestsSurface-CN2z3DFv.js +2 -0
- package/dist/assets/SessionSwitcherContent-CCljxAZb.js +1 -0
- package/dist/assets/SessionSwitcherDropdown-DtNt3qw4.js +2 -0
- package/dist/assets/SessionUnreadDot-B9bHMy5q.js +1 -0
- package/dist/assets/SettingsView-DaZVCInW.js +6 -0
- package/dist/assets/{SettingsWindow-CAxKgYOu.js → SettingsWindow-C-ydfopH.js} +1 -1
- package/dist/assets/StickyHorizontalScrollbar-BMITjK4e.js +8 -0
- package/dist/assets/{ToolOutputDialog-lz5exVQ4.js → ToolOutputDialog-DJUAx_Oj.js} +4 -4
- package/dist/assets/{ToolPartDiffPreview-nIUiNLpP.js → ToolPartDiffPreview-DFyuJM4c.js} +1 -1
- package/dist/assets/{VirtualizedFileDiff-IWqCJNXu.js → VirtualizedFileDiff-BS-af6rn.js} +4 -4
- package/dist/assets/{Virtualizer-Cp_cJWbI.js → Virtualizer-DP6HbEeC.js} +1 -1
- package/dist/assets/{collapsible-C7oyikVm.js → collapsible-BOcd0lzY.js} +1 -1
- package/dist/assets/{computeVirtualFileMetrics-CyhUKEfv.js → computeVirtualFileMetrics-BI6wORhM.js} +1 -1
- package/dist/assets/{contextFileOpenGuard-DkjmJ-NU.js → contextFileOpenGuard-DUMSGR4M.js} +1 -1
- package/dist/assets/{gitStatusPredicates-DBmUY3kA.js → gitStatusPredicates-FdKYDZe3.js} +1 -1
- package/dist/assets/{githubLink-BdbPMMAK.js → githubLink-B3-BaGpb.js} +5 -5
- package/dist/assets/index-DWSrsQWl.js +11 -0
- package/dist/assets/{index-CKgYIOis.js → index-Df-vmvzu.js} +1 -1
- package/dist/assets/index-DsQIBAWK.css +1 -0
- package/dist/assets/main-61_cd7S7.js +2 -0
- package/dist/assets/main-q43jqZ2I.js +136 -0
- package/dist/assets/{markdownCore-D7V46WY9.js → markdownCore-CqHYlXi_.js} +2 -2
- package/dist/assets/{miniChat-PanPmbEK.js → miniChat-vOuWN5pV.js} +2 -2
- package/dist/assets/mobile-mrXWV5ky.js +2 -0
- package/dist/assets/{nativeFileSave-BzhtrDoD.js → nativeFileSave-CFuugNKV.js} +1 -1
- package/dist/assets/pi-session-focus-preference-BEufpwC6.js +9 -0
- package/dist/assets/{renderDiffWithHighlighter-ByZtK9za.js → renderDiffWithHighlighter-BD9CAhZU.js} +1 -1
- package/dist/assets/renderElectronMiniChatApp-QQFXkY5Y.js +2 -0
- package/dist/assets/{renderFileWithHighlighter-CnN0Tv_8.js → renderFileWithHighlighter-KiEWMtzI.js} +1 -1
- package/dist/assets/renderMobileApp-DIZYvrhi.js +11 -0
- package/dist/assets/runtimeEndpointReset-BA2uP2rj.js +62 -0
- package/dist/assets/{runtimeSurface-DBiUWYK2.js → runtimeSurface-Der4qUnI.js} +1 -1
- package/dist/assets/useAppFontEffects-Bfw_d5eB.js +212 -0
- package/dist/assets/{useFilesViewSearch-BWABG6pS.js → useFilesViewSearch-Budwejnx.js} +3 -3
- package/dist/assets/useGitHubRemoteSearch-gUdylYy3.js +1 -0
- package/dist/assets/{useSwitcherItems-BSTSev5C.js → useSwitcherItems-yHSMduoo.js} +1 -1
- package/dist/assets/{vendor-aparajita-capacitor-secure-storage-BYohCVY0.js → vendor-aparajita-capacitor-secure-storage-BDrMx_CE.js} +1 -1
- package/dist/assets/{vendor-base-ui-BscvGUFt.js → vendor-base-ui-CKRUCG-f.js} +1 -1
- package/dist/assets/{vendor-boot-Dgb9NVFe.js → vendor-boot-EC6IyLt1.js} +13 -13
- package/dist/assets/{vendor-capacitor-app-BjdQVib9.js → vendor-capacitor-app-BaupizkW.js} +1 -1
- package/dist/assets/{vendor-capacitor-keyboard-eTtI5go3.js → vendor-capacitor-keyboard-9BVxykO7.js} +1 -1
- package/dist/assets/{vendor-capacitor-status-bar-9rNIGxlO.js → vendor-capacitor-status-bar-BsrUwiaA.js} +1 -1
- package/dist/assets/{vendor-codemirror-autocomplete-DxS3jwvE.js → vendor-codemirror-autocomplete-CGpAsPWp.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-angular-DdjXl9e2.js → vendor-codemirror-lang-angular-CDJoHJSY.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-cpp--aMr7NVt.js → vendor-codemirror-lang-cpp-BmHofQls.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-css-CZ_qxZY9.js → vendor-codemirror-lang-css-RM2HqXZ0.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-go-Ca-y2Alu.js → vendor-codemirror-lang-go-Crwca_X7.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-html-COZ0L5wT.js → vendor-codemirror-lang-html-CHSHUpwq.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-java-D3Rf3QNJ.js → vendor-codemirror-lang-java-lhQXgWu0.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-javascript-DwJ-IvCs.js → vendor-codemirror-lang-javascript-Dw4kFO4e.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-jinja-D_5lod6k.js → vendor-codemirror-lang-jinja-_pAC3UTN.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-json-BG8C7OpC.js → vendor-codemirror-lang-json-B-b1vAv-.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-less-CVEkoaTw.js → vendor-codemirror-lang-less-xIgOi1ze.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-liquid-U9vj0m8w.js → vendor-codemirror-lang-liquid-CP49m5dB.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-markdown-DqlYH9gY.js → vendor-codemirror-lang-markdown-BTht2ATe.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-php-BFEiEVA1.js → vendor-codemirror-lang-php-BguZDhaO.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-python-Cz1dQXQd.js → vendor-codemirror-lang-python-CH6_em9L.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-rust-C_CoD9mj.js → vendor-codemirror-lang-rust-EG_DtGyi.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-sass-BuR0aKh5.js → vendor-codemirror-lang-sass-BknY5Qpo.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-sql-DggNUjdM.js → vendor-codemirror-lang-sql-B0f-qCZo.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-vue-DFZHi2J3.js → vendor-codemirror-lang-vue-CyGn9snv.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-wast-zuTN3SHT.js → vendor-codemirror-lang-wast-CsFfSyyB.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-xml-CrcvhI-k.js → vendor-codemirror-lang-xml-Cdan_Jp1.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-yaml-DcVWsakb.js → vendor-codemirror-lang-yaml-COI5hck8.js} +1 -1
- package/dist/assets/{vendor-codemirror-language-data-CCLITgCF.js → vendor-codemirror-language-data-CqU7kHmg.js} +2 -2
- package/dist/assets/{vendor-codemirror-search-H7NOEdbX.js → vendor-codemirror-search-DUfSCF9S.js} +1 -1
- package/dist/assets/{vendor-lezer-cpp-C8aD2KAr.js → vendor-lezer-cpp-DMZOJzBa.js} +1 -1
- package/dist/assets/{vendor-lezer-css-2lbTJF-Y.js → vendor-lezer-css-DUkkbMp7.js} +1 -1
- package/dist/assets/{vendor-lezer-go-Bw6LFTrZ.js → vendor-lezer-go-CHIhCNY6.js} +1 -1
- package/dist/assets/{vendor-lezer-html-D7ym2z7S.js → vendor-lezer-html-B3h0MOrG.js} +1 -1
- package/dist/assets/{vendor-lezer-java-CZhK7w21.js → vendor-lezer-java-tMazV_6X.js} +1 -1
- package/dist/assets/{vendor-lezer-javascript-rpKeTHZp.js → vendor-lezer-javascript-ctwLWf4v.js} +1 -1
- package/dist/assets/{vendor-lezer-json-C7jwsd2G.js → vendor-lezer-json-BP_UAzLL.js} +1 -1
- package/dist/assets/{vendor-lezer-lr-DR-ylqNN.js → vendor-lezer-lr-B8TQctNw.js} +1 -1
- package/dist/assets/{vendor-lezer-markdown-Bir0m5nB.js → vendor-lezer-markdown-aGLZf_3h.js} +1 -1
- package/dist/assets/{vendor-lezer-php-y0hIEfo1.js → vendor-lezer-php-D5zCNzlt.js} +1 -1
- package/dist/assets/{vendor-lezer-python-D64A9lNV.js → vendor-lezer-python-EJ4Im4y2.js} +1 -1
- package/dist/assets/{vendor-lezer-rust-E3ZA9T2L.js → vendor-lezer-rust-DO5c7OtA.js} +1 -1
- package/dist/assets/{vendor-lezer-sass-C5UFI9SP.js → vendor-lezer-sass-CMi6zfXI.js} +1 -1
- package/dist/assets/{vendor-lezer-xml-DOy7FzCR.js → vendor-lezer-xml-0MHMeZDp.js} +1 -1
- package/dist/assets/{vendor-lezer-yaml-kEjaSCvZ.js → vendor-lezer-yaml-BE2EFjpA.js} +1 -1
- package/dist/assets/{vendor-replit-codemirror-vim-BLfh0LPy.js → vendor-replit-codemirror-vim-AnOuN8yJ.js} +1 -1
- package/dist/assets/{web-components-CdMfsnXE.js → web-components-CeHLxvED.js} +3 -3
- package/dist/index.html +4 -4
- package/dist/mini-chat.html +3 -3
- package/dist/mobile.html +3 -3
- package/dist/sw.js +1 -1
- package/package.json +1 -2
- package/server/index.js +69 -30
- package/server/lib/client-auth/DOCUMENTATION.md +15 -1
- package/server/lib/client-auth/pairing.js +2 -12
- package/server/lib/client-auth/remote-clients.js +2 -12
- package/server/lib/cloudflare-tunnel.js +0 -132
- package/server/lib/fs/DOCUMENTATION.md +8 -0
- package/server/lib/fs/atomic-write.js +111 -0
- package/server/lib/fs/atomic-write.test.js +133 -0
- package/server/lib/notifications/DOCUMENTATION.md +43 -32
- package/server/lib/notifications/pi-notification-watcher.js +8 -7
- package/server/lib/notifications/pi-notification-watcher.test.js +88 -2
- package/server/lib/pi/routes-extension-events.test.js +231 -0
- package/server/lib/pi/routes.js +137 -4
- package/server/lib/pi/routes.test.js +248 -2
- package/server/lib/pi/session-daemon/DOCUMENTATION.md +17 -4
- package/server/lib/pi/session-daemon/daemon-process.js +2 -0
- package/server/lib/pi/session-daemon/engines/index.js +6 -0
- package/server/lib/pi/session-daemon/extension-bridge-draft.test.js +1 -1
- package/server/lib/pi/session-daemon/extension-bridge.js +24 -0
- package/server/lib/pi/session-daemon/extension-message-render.js +74 -0
- package/server/lib/pi/session-daemon/extension-message-render.test.js +164 -0
- package/server/lib/pi/session-daemon/extension-tool-render.js +13 -6
- package/server/lib/pi/session-daemon/pending-input.js +188 -0
- package/server/lib/pi/session-daemon/pending-input.test.js +202 -0
- package/server/lib/pi/session-daemon/session-daemon-engines.test.js +828 -0
- package/server/lib/pi/session-daemon/session-daemon-extensions.test.js +296 -6
- package/server/lib/pi/session-daemon/session-daemon-turn.e2e.test.js +9 -0
- package/server/lib/pi/session-daemon/session-daemon.js +219 -19
- package/server/lib/pi/session-daemon/session-daemon.test.js +30 -2
- package/server/lib/pi/session-daemon/session-engine-router.js +390 -0
- package/server/lib/pi/session-daemon/session-engine-router.test.js +515 -0
- package/server/lib/pi/session-daemon/session-engines.js +467 -0
- package/server/lib/pi/session-daemon/session-engines.test.js +383 -0
- package/server/lib/pi/snippets-store.js +8 -17
- package/server/lib/pi/ui-settings-store.js +6 -10
- package/server/lib/pi/ui-settings-store.test.js +37 -1
- package/server/lib/relay/DOCUMENTATION.md +6 -4
- package/server/lib/relay/identity-store.js +116 -0
- package/server/lib/relay/identity-store.test.js +169 -0
- package/server/lib/relay/identity.js +6 -4
- package/server/lib/relay/server-id.js +63 -0
- package/server/lib/relay/server-id.test.js +132 -0
- package/server/lib/relay/signing-key.js +14 -11
- package/server/lib/security/bind-host.js +6 -2
- package/server/lib/security/bind-host.test.js +17 -0
- package/server/lib/server/core-routes.js +27 -1
- package/server/lib/server/lan-addresses.js +18 -6
- package/server/lib/server/lan-addresses.test.js +28 -0
- package/server/lib/server/tunnel-auth.js +47 -7
- package/server/lib/server/tunnel-auth.rate-limit.test.js +164 -0
- package/server/lib/server/tunnel-service.DOCUMENTATION.md +3 -4
- package/server/lib/server/tunnel-service.js +23 -20
- package/server/lib/server/tunnel-service.test.js +19 -2
- package/server/lib/stt/config-store.js +5 -15
- package/server/lib/tailscale/DOCUMENTATION.md +264 -0
- package/server/lib/tailscale/routes.js +45 -0
- package/server/lib/tailscale/service.js +959 -0
- package/server/lib/tailscale/tailscale.js +302 -0
- package/server/lib/tailscale/tailscale.test.js +1861 -0
- package/server/lib/tunnels/DOCUMENTATION.md +3 -4
- package/server/lib/tunnels/providers/cloudflare.js +1 -22
- package/server/lib/tunnels/types.js +0 -2
- package/server/lib/ui-auth/DOCUMENTATION.md +31 -0
- package/server/lib/ui-auth/ui-auth.js +80 -6
- package/server/lib/ui-auth/ui-auth.test.js +136 -0
- package/server/lib/ui-auth/ui-passkeys.js +2 -13
- package/server/lib/update-job-store.js +4 -6
- package/dist/assets/DiffView-hz90_htv.js +0 -18
- package/dist/assets/FilesView-DaqkPMqx.js +0 -6
- package/dist/assets/PierreDiffViewer-BeGpdI0x.js +0 -134
- package/dist/assets/PullRequestsSurface-D9beq2x9.js +0 -2
- package/dist/assets/SessionSwitcherContent-DElQ_cjm.js +0 -1
- package/dist/assets/SessionSwitcherDropdown-JaIDcDIj.js +0 -2
- package/dist/assets/SessionUnreadDot-Czfvjbdi.js +0 -1
- package/dist/assets/SettingsView-C_GC1Mx3.js +0 -6
- package/dist/assets/index-BnZVqcki.js +0 -11
- package/dist/assets/index-BnieExes.css +0 -1
- package/dist/assets/main-6YKi4QM2.js +0 -2
- package/dist/assets/main-CWdWdhdL.js +0 -136
- package/dist/assets/mobile-0LY-2fxw.js +0 -2
- package/dist/assets/pi-session-focus-preference-BC-ze1oC.js +0 -10
- package/dist/assets/renderElectronMiniChatApp-DETQLjFi.js +0 -2
- package/dist/assets/renderMobileApp-DS0FDQLg.js +0 -11
- package/dist/assets/runtimeEndpointReset-BqqXMwE9.js +0 -62
- package/dist/assets/useAppFontEffects-BisjofNZ.js +0 -212
- package/dist/assets/useGitHubRemoteSearch-Bsf2YeQP.js +0 -1
- package/dist/assets/vendor-capacitor-push-notifications-CBKq2Arn.js +0 -1
- package/server/lib/notifications/delivery-runtime.js +0 -314
- package/server/lib/notifications/delivery-runtime.test.js +0 -85
- package/server/lib/notifications/routes.js +0 -74
- package/server/lib/relay/service.js +0 -9
|
@@ -0,0 +1,1861 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
buildTailscaleServeArgs,
|
|
5
|
+
buildTailscaleUrl,
|
|
6
|
+
checkTailscaleAuthGate,
|
|
7
|
+
classifyApplyFailure,
|
|
8
|
+
classifyServeMapping,
|
|
9
|
+
conflictMessageForPort,
|
|
10
|
+
extractApprovalUrl,
|
|
11
|
+
findServeMappingForPort,
|
|
12
|
+
isPermissionDeniedOutput,
|
|
13
|
+
normalizeTailscaleConfig,
|
|
14
|
+
parseTailscaleStatus,
|
|
15
|
+
resolveTailscaleExecutable,
|
|
16
|
+
tailscaleCommandForPlatform,
|
|
17
|
+
validateTailscaleConfig,
|
|
18
|
+
} from './tailscale.js';
|
|
19
|
+
import { createTailscaleService } from './service.js';
|
|
20
|
+
|
|
21
|
+
describe('tailscale status parsing', () => {
|
|
22
|
+
it('strips the trailing dot from DNSName and reports login state', () => {
|
|
23
|
+
expect(parseTailscaleStatus(JSON.stringify({
|
|
24
|
+
BackendState: 'Running',
|
|
25
|
+
Self: { DNSName: 'machine.tail123.ts.net.', TailscaleIPs: ['100.64.0.1'] },
|
|
26
|
+
CertDomains: ['machine.tail123.ts.net'],
|
|
27
|
+
}))).toEqual({
|
|
28
|
+
running: true,
|
|
29
|
+
loggedIn: true,
|
|
30
|
+
magicDnsName: 'machine.tail123.ts.net',
|
|
31
|
+
httpsCertsAvailable: true,
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('reports logged out when Self is missing', () => {
|
|
36
|
+
expect(parseTailscaleStatus(JSON.stringify({ BackendState: 'NoState' }))).toEqual({
|
|
37
|
+
running: false,
|
|
38
|
+
loggedIn: false,
|
|
39
|
+
magicDnsName: null,
|
|
40
|
+
httpsCertsAvailable: null,
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('reports unknown cert availability when CertDomains is absent', () => {
|
|
45
|
+
const parsed = parseTailscaleStatus(JSON.stringify({
|
|
46
|
+
BackendState: 'Running',
|
|
47
|
+
Self: { DNSName: 'machine.tail123.ts.net.' },
|
|
48
|
+
}));
|
|
49
|
+
expect(parsed.loggedIn).toBe(true);
|
|
50
|
+
expect(parsed.httpsCertsAvailable).toBeNull();
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('never throws on malformed JSON', () => {
|
|
54
|
+
expect(parseTailscaleStatus('not json{{{')).toEqual({
|
|
55
|
+
running: false,
|
|
56
|
+
loggedIn: false,
|
|
57
|
+
magicDnsName: null,
|
|
58
|
+
httpsCertsAvailable: null,
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
describe('executable resolution', () => {
|
|
64
|
+
it('uses tailscale on posix and tailscale.exe on Windows', () => {
|
|
65
|
+
expect(tailscaleCommandForPlatform('darwin')).toBe('tailscale');
|
|
66
|
+
expect(tailscaleCommandForPlatform('linux')).toBe('tailscale');
|
|
67
|
+
expect(tailscaleCommandForPlatform('win32')).toBe('tailscale.exe');
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('falls back to the macOS app bundle location', () => {
|
|
71
|
+
const resolved = resolveTailscaleExecutable({ platform: 'darwin', existsSync: () => false });
|
|
72
|
+
expect(resolved.command).toBe('tailscale');
|
|
73
|
+
expect(resolved.fallback).toBe('/Applications/Tailscale.app/Contents/MacOS/Tailscale');
|
|
74
|
+
const bundled = resolveTailscaleExecutable({
|
|
75
|
+
platform: 'darwin',
|
|
76
|
+
existsSync: (candidate) => candidate === '/Applications/Tailscale.app/Contents/MacOS/Tailscale',
|
|
77
|
+
});
|
|
78
|
+
expect(bundled.command).toBe('/Applications/Tailscale.app/Contents/MacOS/Tailscale');
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('falls back to Program Files on Windows', () => {
|
|
82
|
+
const resolved = resolveTailscaleExecutable({ platform: 'win32', env: {}, existsSync: () => false });
|
|
83
|
+
expect(resolved.command).toBe('tailscale.exe');
|
|
84
|
+
expect(resolved.fallback).toContain('Tailscale');
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
describe('config validation', () => {
|
|
89
|
+
it('accepts partial updates and normalizes defaults', () => {
|
|
90
|
+
expect(validateTailscaleConfig({ enabled: true, mode: 'public', httpsPort: 8443 })).toEqual({ ok: true, errors: [] });
|
|
91
|
+
expect(normalizeTailscaleConfig(null)).toEqual({ enabled: false, mode: 'private', httpsPort: 443 });
|
|
92
|
+
expect(normalizeTailscaleConfig({ enabled: true })).toEqual({ enabled: true, mode: 'private', httpsPort: 443 });
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('rejects bad mode, port and enabled values', () => {
|
|
96
|
+
expect(validateTailscaleConfig({ mode: 'funnel' }).ok).toBe(false);
|
|
97
|
+
expect(validateTailscaleConfig({ httpsPort: 3000 }).ok).toBe(false);
|
|
98
|
+
expect(validateTailscaleConfig({ httpsPort: '443' }).ok).toBe(false);
|
|
99
|
+
expect(validateTailscaleConfig({ enabled: 'yes' }).ok).toBe(false);
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
describe('auth gate', () => {
|
|
104
|
+
it('allows anything while disabled', () => {
|
|
105
|
+
expect(checkTailscaleAuthGate({ enabled: false, mode: 'public', uiPasswordConfigured: false }).allowed).toBe(true);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it('requires UI auth for private mode without the escape hatch', () => {
|
|
109
|
+
const blocked = checkTailscaleAuthGate({ enabled: true, mode: 'private', uiPasswordConfigured: false, unsafeUnauthenticatedLanAllowed: false });
|
|
110
|
+
expect(blocked.allowed).toBe(false);
|
|
111
|
+
expect(blocked.code).toBe('auth_required');
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('honors the escape hatch for private mode', () => {
|
|
115
|
+
expect(checkTailscaleAuthGate({ enabled: true, mode: 'private', uiPasswordConfigured: false, unsafeUnauthenticatedLanAllowed: true }).allowed).toBe(true);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('never honors the escape hatch for public mode', () => {
|
|
119
|
+
const blocked = checkTailscaleAuthGate({ enabled: true, mode: 'public', uiPasswordConfigured: false, unsafeUnauthenticatedLanAllowed: true });
|
|
120
|
+
expect(blocked.allowed).toBe(false);
|
|
121
|
+
expect(blocked.code).toBe('auth_required');
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it('allows both modes with a UI password', () => {
|
|
125
|
+
for (const mode of ['private', 'public']) {
|
|
126
|
+
expect(checkTailscaleAuthGate({ enabled: true, mode, uiPasswordConfigured: true }).allowed).toBe(true);
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
describe('serve status conflict detection', () => {
|
|
132
|
+
const serveStatus = {
|
|
133
|
+
TCP: { 443: { HTTPS: true } },
|
|
134
|
+
Web: {
|
|
135
|
+
'machine.tail123.ts.net:443': {
|
|
136
|
+
Handlers: { '/': 'http://127.0.0.1:3000/' },
|
|
137
|
+
},
|
|
138
|
+
'machine.tail123.ts.net:8443': {
|
|
139
|
+
Handlers: { '/': 'http://127.0.0.1:9999' },
|
|
140
|
+
},
|
|
141
|
+
},
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
it('finds mappings keyed by <host>:<port>', () => {
|
|
145
|
+
expect(findServeMappingForPort(serveStatus, 443)?.key).toBe('machine.tail123.ts.net:443');
|
|
146
|
+
expect(findServeMappingForPort(serveStatus, 10000)).toBeNull();
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it('recognizes our own mapping', () => {
|
|
150
|
+
const mapping = findServeMappingForPort(serveStatus, 443);
|
|
151
|
+
expect(classifyServeMapping({ mapping, httpsPort: 443, boundPort: 3000 }).kind).toBe('ours');
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it('flags a foreign mapping as conflict-worthy', () => {
|
|
155
|
+
const mapping = findServeMappingForPort(serveStatus, 8443);
|
|
156
|
+
const classification = classifyServeMapping({ mapping, httpsPort: 8443, boundPort: 3000 });
|
|
157
|
+
expect(classification.kind).toBe('foreign');
|
|
158
|
+
expect(conflictMessageForPort(8443)).toContain('8443');
|
|
159
|
+
expect(conflictMessageForPort(8443)).not.toContain('8443 or 8443');
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it('treats absent mappings as free', () => {
|
|
163
|
+
expect(classifyServeMapping({ mapping: null, httpsPort: 10000, boundPort: 3000 }).kind).toBe('absent');
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
describe('approval and permission detection', () => {
|
|
168
|
+
it('extracts the login approval URL from streamed output', () => {
|
|
169
|
+
expect(extractApprovalUrl('visit https://login.tailscale.com/f/serve?node=abc123 to approve\nwaiting...'))
|
|
170
|
+
.toBe('https://login.tailscale.com/f/serve?node=abc123');
|
|
171
|
+
expect(extractApprovalUrl('visit https://login.tailscale.com/f/funnel?node=xyz to enable funnel.'))
|
|
172
|
+
.toBe('https://login.tailscale.com/f/funnel?node=xyz');
|
|
173
|
+
expect(extractApprovalUrl('all good, no url here')).toBeNull();
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it('detects Linux operator permission errors', () => {
|
|
177
|
+
expect(isPermissionDeniedOutput('Error: access denied: serve config denied')).toBe(true);
|
|
178
|
+
expect(isPermissionDeniedOutput('failed: must be root or operator')).toBe(true);
|
|
179
|
+
expect(isPermissionDeniedOutput('some other failure')).toBe(false);
|
|
180
|
+
expect(classifyApplyFailure('Error: access denied').code).toBe('permission_denied');
|
|
181
|
+
expect(classifyApplyFailure('tailscale is not logged in').code).toBe('not_logged_in');
|
|
182
|
+
expect(classifyApplyFailure('visit https://login.tailscale.com/f/serve?node=x').code).toBe('needs_approval');
|
|
183
|
+
expect(classifyApplyFailure('weird unknown boom')).toBeNull();
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it('builds serve args and public URLs', () => {
|
|
187
|
+
expect(buildTailscaleServeArgs({ mode: 'private', httpsPort: 443, localPort: 3000 }))
|
|
188
|
+
.toEqual(['serve', '--bg', '--https=443', 'http://127.0.0.1:3000']);
|
|
189
|
+
expect(buildTailscaleServeArgs({ mode: 'public', httpsPort: 8443, localPort: 3000 })[0]).toBe('funnel');
|
|
190
|
+
expect(buildTailscaleUrl({ magicDnsName: 'm.ts.net', httpsPort: 443 })).toBe('https://m.ts.net');
|
|
191
|
+
expect(buildTailscaleUrl({ magicDnsName: 'm.ts.net', httpsPort: 10000 })).toBe('https://m.ts.net:10000');
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
const STATUS_JSON = JSON.stringify({
|
|
196
|
+
BackendState: 'Running',
|
|
197
|
+
Self: { DNSName: 'mymachine.tailabc.ts.net.' },
|
|
198
|
+
CertDomains: ['mymachine.tailabc.ts.net'],
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
// Stable identity injected into services under test: the probe compares
|
|
202
|
+
// `/health`'s serverId against this same id (the relay signing-key serverId
|
|
203
|
+
// in production) and only goes `active` on a match.
|
|
204
|
+
const TEST_SERVER_ID = 'test-server-id';
|
|
205
|
+
const matchingFetchImpl = async () => ({ ok: true, status: 200, json: async () => ({ serverId: TEST_SERVER_ID }) });
|
|
206
|
+
|
|
207
|
+
/** In-memory fs + scripted command runner for lifecycle tests. */
|
|
208
|
+
const createHarness = ({ handlers, port = 3000, uiPassword = 'secret' } = {}) => {
|
|
209
|
+
const files = new Map();
|
|
210
|
+
const calls = [];
|
|
211
|
+
const fsPromises = {
|
|
212
|
+
readFile: async (filePath) => {
|
|
213
|
+
if (!files.has(filePath)) {
|
|
214
|
+
const error = new Error('missing');
|
|
215
|
+
error.code = 'ENOENT';
|
|
216
|
+
throw error;
|
|
217
|
+
}
|
|
218
|
+
return files.get(filePath);
|
|
219
|
+
},
|
|
220
|
+
writeFile: async (filePath, content) => {
|
|
221
|
+
files.set(filePath, String(content));
|
|
222
|
+
},
|
|
223
|
+
mkdir: async () => {},
|
|
224
|
+
rename: async (from, to) => {
|
|
225
|
+
files.set(to, files.get(from));
|
|
226
|
+
files.delete(from);
|
|
227
|
+
},
|
|
228
|
+
unlink: async (filePath) => {
|
|
229
|
+
if (!files.delete(filePath)) {
|
|
230
|
+
const error = new Error('missing');
|
|
231
|
+
error.code = 'ENOENT';
|
|
232
|
+
throw error;
|
|
233
|
+
}
|
|
234
|
+
},
|
|
235
|
+
};
|
|
236
|
+
const runner = {
|
|
237
|
+
runTailscale: async (args, { onOutput } = {}) => {
|
|
238
|
+
calls.push(args);
|
|
239
|
+
const handler = handlers[args.slice(1).join(' ')] || handlers['*'];
|
|
240
|
+
const result = typeof handler === 'function' ? await handler(args, onOutput) : handler;
|
|
241
|
+
if (result?.stream) {
|
|
242
|
+
for (const chunk of result.stream) onOutput?.(chunk);
|
|
243
|
+
}
|
|
244
|
+
return result || { ok: true, code: 0, stdout: '', stderr: '', spawnError: null, timedOut: false };
|
|
245
|
+
},
|
|
246
|
+
};
|
|
247
|
+
const service = createTailscaleService({
|
|
248
|
+
dataDir: '/data',
|
|
249
|
+
getPort: () => port,
|
|
250
|
+
isUiAuthEnabled: () => uiPassword !== null,
|
|
251
|
+
isUnsafeUnauthenticatedLanAllowed: () => false,
|
|
252
|
+
runner,
|
|
253
|
+
fsPromises,
|
|
254
|
+
existsSync: () => false,
|
|
255
|
+
fetchImpl: async () => ({ ok: false, status: 404, json: async () => null }),
|
|
256
|
+
logWarn: () => {},
|
|
257
|
+
});
|
|
258
|
+
return { service, calls, files, fsPromises };
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
describe('tailscale executable resolution', () => {
|
|
262
|
+
it('runs the macOS app-bundle CLI when it exists', async () => {
|
|
263
|
+
const bundle = '/Applications/Tailscale.app/Contents/MacOS/Tailscale';
|
|
264
|
+
const calls = [];
|
|
265
|
+
const service = createTailscaleService({
|
|
266
|
+
dataDir: '/data',
|
|
267
|
+
getPort: () => 3000,
|
|
268
|
+
platform: 'darwin',
|
|
269
|
+
existsSync: (candidate) => candidate === bundle,
|
|
270
|
+
runner: {
|
|
271
|
+
runTailscale: async (args) => {
|
|
272
|
+
calls.push(args);
|
|
273
|
+
return { ok: true, code: 0, stdout: '{}', stderr: '', spawnError: null, timedOut: false };
|
|
274
|
+
},
|
|
275
|
+
},
|
|
276
|
+
fsPromises: {
|
|
277
|
+
readFile: async () => { const e = new Error('x'); e.code = 'ENOENT'; throw e; },
|
|
278
|
+
writeFile: async () => {},
|
|
279
|
+
mkdir: async () => {},
|
|
280
|
+
rename: async () => {},
|
|
281
|
+
unlink: async () => {},
|
|
282
|
+
},
|
|
283
|
+
logWarn: () => {},
|
|
284
|
+
});
|
|
285
|
+
await service.retry();
|
|
286
|
+
expect(calls.length).toBeGreaterThan(0);
|
|
287
|
+
expect(calls.every((args) => args[0] === bundle)).toBe(true);
|
|
288
|
+
service.dispose();
|
|
289
|
+
});
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
describe('status auth gate', () => {
|
|
293
|
+
it('reports per-mode auth-gate flags on the status model', async () => {
|
|
294
|
+
const { service } = createHarness({ uiPassword: null });
|
|
295
|
+
await service.loadConfig();
|
|
296
|
+
const blocked = service.getStatus();
|
|
297
|
+
expect(blocked.authGate).toEqual({ privateAllowed: false, publicAllowed: false });
|
|
298
|
+
service.dispose();
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
it('honors the LAN escape hatch for private only', async () => {
|
|
302
|
+
const fsPromises = {
|
|
303
|
+
readFile: async () => { const e = new Error('x'); e.code = 'ENOENT'; throw e; },
|
|
304
|
+
writeFile: async () => {},
|
|
305
|
+
mkdir: async () => {},
|
|
306
|
+
rename: async () => {},
|
|
307
|
+
unlink: async () => {},
|
|
308
|
+
};
|
|
309
|
+
const service = createTailscaleService({
|
|
310
|
+
existsSync: () => false,
|
|
311
|
+
dataDir: '/data',
|
|
312
|
+
getPort: () => 3000,
|
|
313
|
+
isUiAuthEnabled: () => false,
|
|
314
|
+
isUnsafeUnauthenticatedLanAllowed: () => true,
|
|
315
|
+
runner: { runTailscale: async () => ({ ok: true, code: 0, stdout: '{}', stderr: '', spawnError: null, timedOut: false }) },
|
|
316
|
+
fsPromises,
|
|
317
|
+
fetchImpl: async () => ({ ok: false, status: 404, json: async () => null }),
|
|
318
|
+
logWarn: () => {},
|
|
319
|
+
});
|
|
320
|
+
await service.loadConfig();
|
|
321
|
+
expect(service.getStatus().authGate).toEqual({ privateAllowed: true, publicAllowed: false });
|
|
322
|
+
service.dispose();
|
|
323
|
+
});
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
describe('tailscale service lifecycle', () => {
|
|
327
|
+
it('applies on start and probes to active', async () => {
|
|
328
|
+
const { service, calls } = createHarness({
|
|
329
|
+
handlers: {
|
|
330
|
+
'status --json': { ok: true, code: 0, stdout: STATUS_JSON, stderr: '', spawnError: null, timedOut: false },
|
|
331
|
+
'serve status --json': { ok: true, code: 0, stdout: JSON.stringify({ Web: {} }), stderr: '', spawnError: null, timedOut: false },
|
|
332
|
+
},
|
|
333
|
+
});
|
|
334
|
+
service.fetchImpl = undefined;
|
|
335
|
+
const probing = createTailscaleService({
|
|
336
|
+
existsSync: () => false,
|
|
337
|
+
dataDir: '/data',
|
|
338
|
+
getPort: () => 3000,
|
|
339
|
+
isUiAuthEnabled: () => true,
|
|
340
|
+
isUnsafeUnauthenticatedLanAllowed: () => false,
|
|
341
|
+
runner: {
|
|
342
|
+
runTailscale: async (args) => {
|
|
343
|
+
calls.push(args);
|
|
344
|
+
if (args.includes('status') && args.includes('--json') && !args.includes('serve')) {
|
|
345
|
+
return { ok: true, code: 0, stdout: STATUS_JSON, stderr: '', spawnError: null, timedOut: false };
|
|
346
|
+
}
|
|
347
|
+
if (args.includes('serve') && args.includes('status')) {
|
|
348
|
+
return { ok: true, code: 0, stdout: JSON.stringify({ Web: {} }), stderr: '', spawnError: null, timedOut: false };
|
|
349
|
+
}
|
|
350
|
+
return { ok: true, code: 0, stdout: '', stderr: '', spawnError: null, timedOut: false };
|
|
351
|
+
},
|
|
352
|
+
},
|
|
353
|
+
fsPromises: {
|
|
354
|
+
readFile: async () => { const e = new Error('x'); e.code = 'ENOENT'; throw e; },
|
|
355
|
+
writeFile: async () => {},
|
|
356
|
+
mkdir: async () => {},
|
|
357
|
+
rename: async () => {},
|
|
358
|
+
unlink: async () => {},
|
|
359
|
+
},
|
|
360
|
+
getServerId: async () => TEST_SERVER_ID,
|
|
361
|
+
fetchImpl: matchingFetchImpl,
|
|
362
|
+
logWarn: () => {},
|
|
363
|
+
});
|
|
364
|
+
await probing.loadConfig();
|
|
365
|
+
const result = await probing.setConfig({ enabled: true, mode: 'private', httpsPort: 443 });
|
|
366
|
+
expect(result.state).toBe('active');
|
|
367
|
+
expect(result.url).toBe('https://mymachine.tailabc.ts.net');
|
|
368
|
+
expect(calls.some((args) => args.includes('serve') && args.includes('--bg'))).toBe(true);
|
|
369
|
+
expect(probing.getPairingCandidate()).toEqual({ type: 'tailscale', url: 'https://mymachine.tailabc.ts.net', mode: 'private', priority: 20 });
|
|
370
|
+
probing.dispose();
|
|
371
|
+
expect(service).toBeDefined();
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
it('keeps a mismatching /health serverId at starting (never active)', async () => {
|
|
375
|
+
const memFs = new Map();
|
|
376
|
+
const fsPromises = {
|
|
377
|
+
readFile: async (p) => {
|
|
378
|
+
if (!memFs.has(p)) {
|
|
379
|
+
const e = new Error('x');
|
|
380
|
+
e.code = 'ENOENT';
|
|
381
|
+
throw e;
|
|
382
|
+
}
|
|
383
|
+
return memFs.get(p);
|
|
384
|
+
},
|
|
385
|
+
writeFile: async (p, c) => {
|
|
386
|
+
memFs.set(p, String(c));
|
|
387
|
+
},
|
|
388
|
+
mkdir: async () => {},
|
|
389
|
+
rename: async (a, b) => {
|
|
390
|
+
memFs.set(b, memFs.get(a));
|
|
391
|
+
memFs.delete(a);
|
|
392
|
+
},
|
|
393
|
+
unlink: async () => {},
|
|
394
|
+
};
|
|
395
|
+
const service = createTailscaleService({
|
|
396
|
+
existsSync: () => false,
|
|
397
|
+
dataDir: '/data',
|
|
398
|
+
getPort: () => 3000,
|
|
399
|
+
isUiAuthEnabled: () => true,
|
|
400
|
+
isUnsafeUnauthenticatedLanAllowed: () => false,
|
|
401
|
+
runner: {
|
|
402
|
+
runTailscale: async (args) => {
|
|
403
|
+
if (args.includes('serve') && args.includes('status')) {
|
|
404
|
+
return { ok: true, code: 0, stdout: JSON.stringify({ Web: {} }), stderr: '', spawnError: null, timedOut: false };
|
|
405
|
+
}
|
|
406
|
+
return { ok: true, code: 0, stdout: STATUS_JSON, stderr: '', spawnError: null, timedOut: false };
|
|
407
|
+
},
|
|
408
|
+
},
|
|
409
|
+
fsPromises,
|
|
410
|
+
getServerId: async () => TEST_SERVER_ID,
|
|
411
|
+
// A different machine answering on the ts.net URL: the probe fails,
|
|
412
|
+
// so the mapping is never advertised (dispose clears the re-probe).
|
|
413
|
+
fetchImpl: async () => ({ ok: true, status: 200, json: async () => ({ serverId: 'some-other-server' }) }),
|
|
414
|
+
logWarn: () => {},
|
|
415
|
+
});
|
|
416
|
+
await service.loadConfig();
|
|
417
|
+
const result = await service.setConfig({ enabled: true, mode: 'private', httpsPort: 443 });
|
|
418
|
+
expect(result.state).toBe('starting');
|
|
419
|
+
expect(result.url).toBeNull();
|
|
420
|
+
expect(service.getPairingCandidate()).toBeNull();
|
|
421
|
+
service.dispose();
|
|
422
|
+
});
|
|
423
|
+
|
|
424
|
+
it('reports conflict instead of overwriting a foreign mapping', async () => {
|
|
425
|
+
const memFs = new Map();
|
|
426
|
+
const fsPromises = {
|
|
427
|
+
readFile: async (p) => {
|
|
428
|
+
if (!memFs.has(p)) {
|
|
429
|
+
const e = new Error('x');
|
|
430
|
+
e.code = 'ENOENT';
|
|
431
|
+
throw e;
|
|
432
|
+
}
|
|
433
|
+
return memFs.get(p);
|
|
434
|
+
},
|
|
435
|
+
writeFile: async (p, c) => {
|
|
436
|
+
memFs.set(p, String(c));
|
|
437
|
+
},
|
|
438
|
+
mkdir: async () => {},
|
|
439
|
+
rename: async (a, b) => {
|
|
440
|
+
memFs.set(b, memFs.get(a));
|
|
441
|
+
memFs.delete(a);
|
|
442
|
+
},
|
|
443
|
+
unlink: async () => {},
|
|
444
|
+
};
|
|
445
|
+
const service = createTailscaleService({
|
|
446
|
+
existsSync: () => false,
|
|
447
|
+
dataDir: '/data',
|
|
448
|
+
getPort: () => 3000,
|
|
449
|
+
isUiAuthEnabled: () => true,
|
|
450
|
+
isUnsafeUnauthenticatedLanAllowed: () => false,
|
|
451
|
+
runner: {
|
|
452
|
+
runTailscale: async (args) => {
|
|
453
|
+
if (args.includes('serve') && args.includes('status')) {
|
|
454
|
+
return {
|
|
455
|
+
ok: true, code: 0, stdout: JSON.stringify({ Web: { 'm.tail.ts.net:443': { Handlers: { '/': 'http://127.0.0.1:9999' } } } }), stderr: '', spawnError: null, timedOut: false,
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
return { ok: true, code: 0, stdout: STATUS_JSON, stderr: '', spawnError: null, timedOut: false };
|
|
459
|
+
},
|
|
460
|
+
},
|
|
461
|
+
fsPromises,
|
|
462
|
+
fetchImpl: async () => ({ ok: false, status: 404, json: async () => null }),
|
|
463
|
+
logWarn: () => {},
|
|
464
|
+
});
|
|
465
|
+
await service.loadConfig();
|
|
466
|
+
const result = await service.setConfig({ enabled: true, mode: 'private', httpsPort: 443 });
|
|
467
|
+
expect(result.state).toBe('conflict');
|
|
468
|
+
expect(result.errorCode).toBe('conflict');
|
|
469
|
+
expect(result.url).toBeNull();
|
|
470
|
+
service.dispose();
|
|
471
|
+
});
|
|
472
|
+
|
|
473
|
+
it('blocks enabling without UI auth (public has no escape hatch)', async () => {
|
|
474
|
+
const { service } = createHarness({ uiPassword: null });
|
|
475
|
+
await service.loadConfig();
|
|
476
|
+
await expect(service.setConfig({ enabled: true, mode: 'public', httpsPort: 443 })).rejects.toMatchObject({ code: 'auth_required' });
|
|
477
|
+
service.dispose();
|
|
478
|
+
});
|
|
479
|
+
|
|
480
|
+
it('removes its own mapping on shutdown but never a foreign one', async () => {
|
|
481
|
+
const removed = [];
|
|
482
|
+
const memFs = new Map();
|
|
483
|
+
const fsPromises = {
|
|
484
|
+
readFile: async (p) => {
|
|
485
|
+
if (!memFs.has(p)) {
|
|
486
|
+
const e = new Error('x');
|
|
487
|
+
e.code = 'ENOENT';
|
|
488
|
+
throw e;
|
|
489
|
+
}
|
|
490
|
+
return memFs.get(p);
|
|
491
|
+
},
|
|
492
|
+
writeFile: async (p, c) => {
|
|
493
|
+
memFs.set(p, String(c));
|
|
494
|
+
},
|
|
495
|
+
mkdir: async () => {},
|
|
496
|
+
rename: async (a, b) => {
|
|
497
|
+
memFs.set(b, memFs.get(a));
|
|
498
|
+
memFs.delete(a);
|
|
499
|
+
},
|
|
500
|
+
unlink: async (p) => {
|
|
501
|
+
memFs.delete(p);
|
|
502
|
+
},
|
|
503
|
+
};
|
|
504
|
+
const service = createTailscaleService({
|
|
505
|
+
existsSync: () => false,
|
|
506
|
+
dataDir: '/data',
|
|
507
|
+
getPort: () => 3000,
|
|
508
|
+
isUiAuthEnabled: () => true,
|
|
509
|
+
isUnsafeUnauthenticatedLanAllowed: () => false,
|
|
510
|
+
runner: {
|
|
511
|
+
runTailscale: async (args) => {
|
|
512
|
+
if (args.includes('off')) {
|
|
513
|
+
removed.push(args);
|
|
514
|
+
return { ok: true, code: 0, stdout: '', stderr: '', spawnError: null, timedOut: false };
|
|
515
|
+
}
|
|
516
|
+
if (args.includes('serve') && args.includes('status')) {
|
|
517
|
+
return { ok: true, code: 0, stdout: JSON.stringify({ Web: {} }), stderr: '', spawnError: null, timedOut: false };
|
|
518
|
+
}
|
|
519
|
+
return { ok: true, code: 0, stdout: STATUS_JSON, stderr: '', spawnError: null, timedOut: false };
|
|
520
|
+
},
|
|
521
|
+
},
|
|
522
|
+
fsPromises,
|
|
523
|
+
fetchImpl: async () => ({ ok: false, status: 404, json: async () => null }),
|
|
524
|
+
logWarn: () => {},
|
|
525
|
+
});
|
|
526
|
+
await service.loadConfig();
|
|
527
|
+
await service.setConfig({ enabled: true, mode: 'private', httpsPort: 443 });
|
|
528
|
+
service.dispose();
|
|
529
|
+
// Re-create (simulates a restart): the persisted record drives removal.
|
|
530
|
+
const service2 = createTailscaleService({
|
|
531
|
+
existsSync: () => false,
|
|
532
|
+
dataDir: '/data',
|
|
533
|
+
getPort: () => 3000,
|
|
534
|
+
isUiAuthEnabled: () => true,
|
|
535
|
+
isUnsafeUnauthenticatedLanAllowed: () => false,
|
|
536
|
+
runner: {
|
|
537
|
+
runTailscale: async (args) => {
|
|
538
|
+
if (args.includes('off')) {
|
|
539
|
+
removed.push(args);
|
|
540
|
+
return { ok: true, code: 0, stdout: '', stderr: '', spawnError: null, timedOut: false };
|
|
541
|
+
}
|
|
542
|
+
if (args.includes('serve') && args.includes('status')) {
|
|
543
|
+
// The persisted record still fronts this server: shutdown must
|
|
544
|
+
// verify ownership, then remove it.
|
|
545
|
+
return { ok: true, code: 0, stdout: JSON.stringify({ Web: { 'mymachine.tailabc.ts.net:443': { Handlers: { '/': 'http://127.0.0.1:3000' } } } }), stderr: '', spawnError: null, timedOut: false };
|
|
546
|
+
}
|
|
547
|
+
return { ok: true, code: 0, stdout: STATUS_JSON, stderr: '', spawnError: null, timedOut: false };
|
|
548
|
+
},
|
|
549
|
+
},
|
|
550
|
+
fsPromises,
|
|
551
|
+
fetchImpl: async () => ({ ok: false, status: 404, json: async () => null }),
|
|
552
|
+
logWarn: () => {},
|
|
553
|
+
});
|
|
554
|
+
await service2.loadConfig();
|
|
555
|
+
await service2.shutdown();
|
|
556
|
+
expect(removed.length).toBeGreaterThan(0);
|
|
557
|
+
expect(removed[0]).toContain('off');
|
|
558
|
+
service2.dispose();
|
|
559
|
+
});
|
|
560
|
+
|
|
561
|
+
it('surfaces needs-approval with the approval URL from streamed output', async () => {
|
|
562
|
+
const memFs = new Map();
|
|
563
|
+
const fsPromises = {
|
|
564
|
+
readFile: async (p) => {
|
|
565
|
+
if (!memFs.has(p)) {
|
|
566
|
+
const e = new Error('x');
|
|
567
|
+
e.code = 'ENOENT';
|
|
568
|
+
throw e;
|
|
569
|
+
}
|
|
570
|
+
return memFs.get(p);
|
|
571
|
+
},
|
|
572
|
+
writeFile: async (p, c) => {
|
|
573
|
+
memFs.set(p, String(c));
|
|
574
|
+
},
|
|
575
|
+
mkdir: async () => {},
|
|
576
|
+
rename: async (a, b) => {
|
|
577
|
+
memFs.set(b, memFs.get(a));
|
|
578
|
+
memFs.delete(a);
|
|
579
|
+
},
|
|
580
|
+
unlink: async () => {},
|
|
581
|
+
};
|
|
582
|
+
const service = createTailscaleService({
|
|
583
|
+
existsSync: () => false,
|
|
584
|
+
dataDir: '/data',
|
|
585
|
+
getPort: () => 3000,
|
|
586
|
+
isUiAuthEnabled: () => true,
|
|
587
|
+
isUnsafeUnauthenticatedLanAllowed: () => false,
|
|
588
|
+
runner: {
|
|
589
|
+
runTailscale: async (args, { onOutput } = {}) => {
|
|
590
|
+
if (args.includes('serve') && args.includes('status')) {
|
|
591
|
+
return { ok: true, code: 0, stdout: JSON.stringify({ Web: {} }), stderr: '', spawnError: null, timedOut: false };
|
|
592
|
+
}
|
|
593
|
+
if (args.includes('--bg')) {
|
|
594
|
+
onOutput?.('visit https://login.tailscale.com/f/serve?node=n123 to approve\n');
|
|
595
|
+
return { ok: false, code: 1, stdout: '', stderr: 'visit https://login.tailscale.com/f/serve?node=n123', spawnError: null, timedOut: false };
|
|
596
|
+
}
|
|
597
|
+
return { ok: true, code: 0, stdout: STATUS_JSON, stderr: '', spawnError: null, timedOut: false };
|
|
598
|
+
},
|
|
599
|
+
},
|
|
600
|
+
fsPromises,
|
|
601
|
+
fetchImpl: async () => ({ ok: false, status: 404, json: async () => null }),
|
|
602
|
+
logWarn: () => {},
|
|
603
|
+
});
|
|
604
|
+
await service.loadConfig();
|
|
605
|
+
const result = await service.setConfig({ enabled: true, mode: 'private', httpsPort: 443 });
|
|
606
|
+
expect(result.state).toBe('needs-approval');
|
|
607
|
+
expect(result.approvalUrl).toBe('https://login.tailscale.com/f/serve?node=n123');
|
|
608
|
+
service.dispose();
|
|
609
|
+
});
|
|
610
|
+
|
|
611
|
+
it('reports unavailable when the executable is missing', async () => {
|
|
612
|
+
const { service } = createHarness({
|
|
613
|
+
handlers: {
|
|
614
|
+
'*': { ok: false, code: null, stdout: '', stderr: '', spawnError: 'spawn tailscale ENOENT', timedOut: false },
|
|
615
|
+
},
|
|
616
|
+
});
|
|
617
|
+
await service.loadConfig();
|
|
618
|
+
const result = await service.setConfig({ enabled: true, mode: 'private', httpsPort: 443 });
|
|
619
|
+
expect(result.state).toBe('unavailable');
|
|
620
|
+
expect(result.errorCode).toBe('not_installed');
|
|
621
|
+
service.dispose();
|
|
622
|
+
});
|
|
623
|
+
|
|
624
|
+
it('rejects enabling with an invalid port', async () => {
|
|
625
|
+
const { service } = createHarness();
|
|
626
|
+
await service.loadConfig();
|
|
627
|
+
await expect(service.setConfig({ enabled: true, httpsPort: 3000 })).rejects.toMatchObject({ code: 'invalid_config' });
|
|
628
|
+
service.dispose();
|
|
629
|
+
});
|
|
630
|
+
|
|
631
|
+
it('retry helper re-runs reconciliation', async () => {
|
|
632
|
+
const spy = vi.fn();
|
|
633
|
+
const { service } = createHarness({
|
|
634
|
+
handlers: {
|
|
635
|
+
'*': async () => {
|
|
636
|
+
spy();
|
|
637
|
+
return { ok: false, code: null, stdout: '', stderr: '', spawnError: 'spawn tailscale ENOENT', timedOut: false };
|
|
638
|
+
},
|
|
639
|
+
},
|
|
640
|
+
});
|
|
641
|
+
await service.loadConfig();
|
|
642
|
+
await service.setConfig({ enabled: true, mode: 'private', httpsPort: 443 });
|
|
643
|
+
const before = spy.mock.calls.length;
|
|
644
|
+
await service.retry();
|
|
645
|
+
expect(spy.mock.calls.length).toBeGreaterThan(before);
|
|
646
|
+
service.dispose();
|
|
647
|
+
});
|
|
648
|
+
});
|
|
649
|
+
|
|
650
|
+
describe('tailscale exact port matching (F14)', () => {
|
|
651
|
+
const webFor = (keys) => ({ Web: Object.fromEntries(keys.map((key) => [key, { Handlers: { '/': 'x' } }])) });
|
|
652
|
+
it('matches <host>:<port> exactly and never by suffix', () => {
|
|
653
|
+
const status = webFor(['m.tail.ts.net:443', 'm.tail.ts.net:8443', 'm.tail.ts.net:4433']);
|
|
654
|
+
expect(findServeMappingForPort(status, 443)?.key).toBe('m.tail.ts.net:443');
|
|
655
|
+
expect(findServeMappingForPort(status, 8443)?.key).toBe('m.tail.ts.net:8443');
|
|
656
|
+
expect(findServeMappingForPort(status, 10000)).toBeNull();
|
|
657
|
+
// 443 must not match :4433; a bare port key still matches.
|
|
658
|
+
expect(findServeMappingForPort(webFor(['m.tail.ts.net:4433']), 443)).toBeNull();
|
|
659
|
+
expect(findServeMappingForPort(webFor(['443']), 443)?.key).toBe('443');
|
|
660
|
+
expect(findServeMappingForPort(null, 443)).toBeNull();
|
|
661
|
+
});
|
|
662
|
+
});
|
|
663
|
+
|
|
664
|
+
describe('tailscale blocked-state cleanup (F2)', () => {
|
|
665
|
+
it('removes its own live mapping before reporting blocked', async () => {
|
|
666
|
+
const removed = [];
|
|
667
|
+
const memFs = new Map();
|
|
668
|
+
const fsPromises = {
|
|
669
|
+
readFile: async (p) => {
|
|
670
|
+
if (!memFs.has(p)) { const e = new Error('x'); e.code = 'ENOENT'; throw e; }
|
|
671
|
+
return memFs.get(p);
|
|
672
|
+
},
|
|
673
|
+
writeFile: async (p, c) => { memFs.set(p, String(c)); },
|
|
674
|
+
mkdir: async () => {},
|
|
675
|
+
rename: async (a, b) => { memFs.set(b, memFs.get(a)); memFs.delete(a); },
|
|
676
|
+
unlink: async (p) => { memFs.delete(p); },
|
|
677
|
+
};
|
|
678
|
+
const oursStatus = (port, localPort) => JSON.stringify({
|
|
679
|
+
Web: { [`mymachine.tailabc.ts.net:${port}`]: { Handlers: { '/': `http://127.0.0.1:${localPort}` } } },
|
|
680
|
+
});
|
|
681
|
+
const runner = {
|
|
682
|
+
runTailscale: async (args) => {
|
|
683
|
+
if (args.includes('off')) {
|
|
684
|
+
removed.push(args);
|
|
685
|
+
return { ok: true, code: 0, stdout: '', stderr: '', spawnError: null, timedOut: false };
|
|
686
|
+
}
|
|
687
|
+
if (args.includes('serve') && args.includes('status')) {
|
|
688
|
+
return { ok: true, code: 0, stdout: oursStatus(443, 3000), stderr: '', spawnError: null, timedOut: false };
|
|
689
|
+
}
|
|
690
|
+
return { ok: true, code: 0, stdout: STATUS_JSON, stderr: '', spawnError: null, timedOut: false };
|
|
691
|
+
},
|
|
692
|
+
};
|
|
693
|
+
// First enable with a password so the record + mapping exist.
|
|
694
|
+
const enabled = createTailscaleService({
|
|
695
|
+
existsSync: () => false,
|
|
696
|
+
dataDir: '/data',
|
|
697
|
+
getPort: () => 3000,
|
|
698
|
+
isUiAuthEnabled: () => true,
|
|
699
|
+
isUnsafeUnauthenticatedLanAllowed: () => false,
|
|
700
|
+
runner,
|
|
701
|
+
fsPromises,
|
|
702
|
+
getServerId: async () => TEST_SERVER_ID,
|
|
703
|
+
fetchImpl: matchingFetchImpl,
|
|
704
|
+
logWarn: () => {},
|
|
705
|
+
});
|
|
706
|
+
await enabled.loadConfig();
|
|
707
|
+
const active = await enabled.setConfig({ enabled: true, mode: 'private', httpsPort: 443 });
|
|
708
|
+
expect(active.state).toBe('active');
|
|
709
|
+
enabled.dispose();
|
|
710
|
+
// Restart without the password: reconcile must remove the own mapping,
|
|
711
|
+
// then report blocked.
|
|
712
|
+
const blocked = createTailscaleService({
|
|
713
|
+
existsSync: () => false,
|
|
714
|
+
dataDir: '/data',
|
|
715
|
+
getPort: () => 3000,
|
|
716
|
+
isUiAuthEnabled: () => false,
|
|
717
|
+
isUnsafeUnauthenticatedLanAllowed: () => false,
|
|
718
|
+
runner,
|
|
719
|
+
fsPromises,
|
|
720
|
+
fetchImpl: async () => ({ ok: false, status: 404, json: async () => null }),
|
|
721
|
+
logWarn: () => {},
|
|
722
|
+
});
|
|
723
|
+
await blocked.loadConfig();
|
|
724
|
+
const result = await blocked.reconcile();
|
|
725
|
+
expect(result.state).toBe('blocked');
|
|
726
|
+
expect(result.errorCode).toBe('auth_required');
|
|
727
|
+
expect(removed.some((args) => args.includes('serve') && args.includes('off'))).toBe(true);
|
|
728
|
+
blocked.dispose();
|
|
729
|
+
});
|
|
730
|
+
});
|
|
731
|
+
|
|
732
|
+
describe('tailscale disabled/blocked host prerequisites', () => {
|
|
733
|
+
it('reports real host prerequisites while disabled and runs only status --json', async () => {
|
|
734
|
+
const { service, calls } = createHarness({
|
|
735
|
+
handlers: {
|
|
736
|
+
'status --json': { ok: true, code: 0, stdout: STATUS_JSON, stderr: '', spawnError: null, timedOut: false },
|
|
737
|
+
},
|
|
738
|
+
});
|
|
739
|
+
await service.loadConfig();
|
|
740
|
+
const result = await service.reconcile();
|
|
741
|
+
expect(result.state).toBe('off');
|
|
742
|
+
expect(result.installed).toBe(true);
|
|
743
|
+
expect(result.running).toBe(true);
|
|
744
|
+
expect(result.loggedIn).toBe(true);
|
|
745
|
+
expect(result.magicDnsName).toBe('mymachine.tailabc.ts.net');
|
|
746
|
+
expect(result.errorCode).toBeNull();
|
|
747
|
+
// Never-enabled stays never-enabled: only `tailscale status --json` ran,
|
|
748
|
+
// no serve/funnel apply (`--bg`) or removal (`off`).
|
|
749
|
+
expect(calls.map((args) => args.slice(1))).toEqual([['status', '--json']]);
|
|
750
|
+
service.dispose();
|
|
751
|
+
});
|
|
752
|
+
|
|
753
|
+
it('reports not-installed while disabled when the executable is missing', async () => {
|
|
754
|
+
const { service, calls } = createHarness({
|
|
755
|
+
handlers: {
|
|
756
|
+
'*': { ok: false, code: null, stdout: '', stderr: '', spawnError: 'spawn tailscale ENOENT', timedOut: false },
|
|
757
|
+
},
|
|
758
|
+
});
|
|
759
|
+
await service.loadConfig();
|
|
760
|
+
const result = await service.reconcile();
|
|
761
|
+
expect(result.state).toBe('off');
|
|
762
|
+
expect(result.installed).toBe(false);
|
|
763
|
+
expect(result.running).toBe(false);
|
|
764
|
+
expect(result.loggedIn).toBe(false);
|
|
765
|
+
expect(result.magicDnsName).toBeNull();
|
|
766
|
+
expect(result.errorCode).toBeNull();
|
|
767
|
+
expect(calls.map((args) => args.slice(1))).toEqual([['status', '--json']]);
|
|
768
|
+
service.dispose();
|
|
769
|
+
});
|
|
770
|
+
|
|
771
|
+
it('reports host prerequisites while blocked without prerequisite error codes', async () => {
|
|
772
|
+
const harness = createHarness({
|
|
773
|
+
uiPassword: null,
|
|
774
|
+
handlers: {
|
|
775
|
+
'status --json': { ok: true, code: 0, stdout: STATUS_JSON, stderr: '', spawnError: null, timedOut: false },
|
|
776
|
+
},
|
|
777
|
+
});
|
|
778
|
+
// setConfig would reject via the auth gate, so persist the enabled
|
|
779
|
+
// config directly like a pre-existing startup config.
|
|
780
|
+
harness.files.set('/data/tailscale-config.json', JSON.stringify({ enabled: true, mode: 'private', httpsPort: 443 }));
|
|
781
|
+
await harness.service.loadConfig();
|
|
782
|
+
const result = await harness.service.reconcile();
|
|
783
|
+
expect(result.state).toBe('blocked');
|
|
784
|
+
expect(result.errorCode).toBe('auth_required');
|
|
785
|
+
expect(result.installed).toBe(true);
|
|
786
|
+
expect(result.running).toBe(true);
|
|
787
|
+
expect(result.loggedIn).toBe(true);
|
|
788
|
+
expect(result.magicDnsName).toBe('mymachine.tailabc.ts.net');
|
|
789
|
+
// Only `tailscale status --json` ran: no serve/funnel apply (`--bg`).
|
|
790
|
+
expect(harness.calls.map((args) => args.slice(1))).toEqual([['status', '--json']]);
|
|
791
|
+
harness.service.dispose();
|
|
792
|
+
});
|
|
793
|
+
});
|
|
794
|
+
|
|
795
|
+
describe('tailscale shutdown ownership verification (F3)', () => {
|
|
796
|
+
const memFsFor = () => {
|
|
797
|
+
const memFs = new Map();
|
|
798
|
+
return {
|
|
799
|
+
memFs,
|
|
800
|
+
fsPromises: {
|
|
801
|
+
readFile: async (p) => {
|
|
802
|
+
if (!memFs.has(p)) { const e = new Error('x'); e.code = 'ENOENT'; throw e; }
|
|
803
|
+
return memFs.get(p);
|
|
804
|
+
},
|
|
805
|
+
writeFile: async (p, c) => { memFs.set(p, String(c)); },
|
|
806
|
+
mkdir: async () => {},
|
|
807
|
+
rename: async (a, b) => { memFs.set(b, memFs.get(a)); memFs.delete(a); },
|
|
808
|
+
unlink: async (p) => { memFs.delete(p); },
|
|
809
|
+
},
|
|
810
|
+
};
|
|
811
|
+
};
|
|
812
|
+
const mappingPath = '/data/tailscale-mapping.json';
|
|
813
|
+
|
|
814
|
+
it('leaves a foreign mapping alone on shutdown', async () => {
|
|
815
|
+
const { memFs, fsPromises } = memFsFor();
|
|
816
|
+
memFs.set(mappingPath, JSON.stringify({ httpsPort: 443, mode: 'private', localPort: 3000 }));
|
|
817
|
+
const removed = [];
|
|
818
|
+
const service = createTailscaleService({
|
|
819
|
+
existsSync: () => false,
|
|
820
|
+
dataDir: '/data',
|
|
821
|
+
getPort: () => 3000,
|
|
822
|
+
isUiAuthEnabled: () => true,
|
|
823
|
+
isUnsafeUnauthenticatedLanAllowed: () => false,
|
|
824
|
+
runner: {
|
|
825
|
+
runTailscale: async (args) => {
|
|
826
|
+
if (args.includes('off')) {
|
|
827
|
+
removed.push(args);
|
|
828
|
+
return { ok: true, code: 0, stdout: '', stderr: '', spawnError: null, timedOut: false };
|
|
829
|
+
}
|
|
830
|
+
if (args.includes('serve') && args.includes('status')) {
|
|
831
|
+
return { ok: true, code: 0, stdout: JSON.stringify({ Web: { 'other.tail.ts.net:443': { Handlers: { '/': 'http://127.0.0.1:9999' } } } }), stderr: '', spawnError: null, timedOut: false };
|
|
832
|
+
}
|
|
833
|
+
return { ok: true, code: 0, stdout: STATUS_JSON, stderr: '', spawnError: null, timedOut: false };
|
|
834
|
+
},
|
|
835
|
+
},
|
|
836
|
+
fsPromises,
|
|
837
|
+
fetchImpl: async () => ({ ok: false, status: 404, json: async () => null }),
|
|
838
|
+
logWarn: () => {},
|
|
839
|
+
});
|
|
840
|
+
await service.loadConfig();
|
|
841
|
+
await service.shutdown();
|
|
842
|
+
expect(removed).toEqual([]);
|
|
843
|
+
service.dispose();
|
|
844
|
+
});
|
|
845
|
+
|
|
846
|
+
it('bounds the shutdown query and removal inside the Electron quit timeout', async () => {
|
|
847
|
+
const { memFs, fsPromises } = memFsFor();
|
|
848
|
+
memFs.set(mappingPath, JSON.stringify({ httpsPort: 443, mode: 'private', localPort: 3000 }));
|
|
849
|
+
const timeouts = [];
|
|
850
|
+
const service = createTailscaleService({
|
|
851
|
+
existsSync: () => false,
|
|
852
|
+
dataDir: '/data',
|
|
853
|
+
getPort: () => 3000,
|
|
854
|
+
isUiAuthEnabled: () => true,
|
|
855
|
+
isUnsafeUnauthenticatedLanAllowed: () => false,
|
|
856
|
+
runner: {
|
|
857
|
+
runTailscale: async (args, options) => {
|
|
858
|
+
timeouts.push(options?.timeoutMs);
|
|
859
|
+
if (args.includes('serve') && args.includes('status')) {
|
|
860
|
+
return { ok: true, code: 0, stdout: JSON.stringify({ Web: { 'm.tail.ts.net:443': { Handlers: { '/': 'http://127.0.0.1:3000' } } } }), stderr: '', spawnError: null, timedOut: false };
|
|
861
|
+
}
|
|
862
|
+
return { ok: true, code: 0, stdout: '', stderr: '', spawnError: null, timedOut: false };
|
|
863
|
+
},
|
|
864
|
+
},
|
|
865
|
+
fsPromises,
|
|
866
|
+
fetchImpl: async () => ({ ok: false, status: 404, json: async () => null }),
|
|
867
|
+
logWarn: () => {},
|
|
868
|
+
});
|
|
869
|
+
await service.loadConfig();
|
|
870
|
+
await service.shutdown();
|
|
871
|
+
expect(timeouts).toHaveLength(2);
|
|
872
|
+
expect(timeouts.every((ms) => Number.isFinite(ms))).toBe(true);
|
|
873
|
+
// QUIT_SERVER_STOP_TIMEOUT_MS in packages/electron/quit-server-stop.mjs.
|
|
874
|
+
expect(timeouts[0] + timeouts[1]).toBeLessThan(8_000);
|
|
875
|
+
expect(memFs.has(mappingPath)).toBe(false);
|
|
876
|
+
service.dispose();
|
|
877
|
+
});
|
|
878
|
+
|
|
879
|
+
it('issues no off and keeps the record when the status query fails', async () => {
|
|
880
|
+
const { memFs, fsPromises } = memFsFor();
|
|
881
|
+
memFs.set(mappingPath, JSON.stringify({ httpsPort: 443, mode: 'private', localPort: 3000 }));
|
|
882
|
+
const removed = [];
|
|
883
|
+
const warnings = [];
|
|
884
|
+
const service = createTailscaleService({
|
|
885
|
+
existsSync: () => false,
|
|
886
|
+
dataDir: '/data',
|
|
887
|
+
getPort: () => 3000,
|
|
888
|
+
isUiAuthEnabled: () => true,
|
|
889
|
+
isUnsafeUnauthenticatedLanAllowed: () => false,
|
|
890
|
+
runner: {
|
|
891
|
+
runTailscale: async (args) => {
|
|
892
|
+
if (args.includes('off')) {
|
|
893
|
+
removed.push(args);
|
|
894
|
+
return { ok: true, code: 0, stdout: '', stderr: '', spawnError: null, timedOut: false };
|
|
895
|
+
}
|
|
896
|
+
if (args.includes('serve') && args.includes('status')) {
|
|
897
|
+
return { ok: false, code: 1, stdout: '', stderr: 'boom', spawnError: null, timedOut: false };
|
|
898
|
+
}
|
|
899
|
+
return { ok: true, code: 0, stdout: STATUS_JSON, stderr: '', spawnError: null, timedOut: false };
|
|
900
|
+
},
|
|
901
|
+
},
|
|
902
|
+
fsPromises,
|
|
903
|
+
fetchImpl: async () => ({ ok: false, status: 404, json: async () => null }),
|
|
904
|
+
logWarn: (message) => warnings.push(String(message)),
|
|
905
|
+
});
|
|
906
|
+
await service.loadConfig();
|
|
907
|
+
await service.shutdown();
|
|
908
|
+
expect(removed).toEqual([]);
|
|
909
|
+
expect(warnings.some((message) => /status query failed/i.test(message))).toBe(true);
|
|
910
|
+
// Record kept for next-start stale cleanup.
|
|
911
|
+
expect(memFs.has(mappingPath)).toBe(true);
|
|
912
|
+
service.dispose();
|
|
913
|
+
});
|
|
914
|
+
});
|
|
915
|
+
|
|
916
|
+
describe('tailscale unknown-vs-absent status queries (F4)', () => {
|
|
917
|
+
const memFsFor = () => {
|
|
918
|
+
const memFs = new Map();
|
|
919
|
+
return {
|
|
920
|
+
memFs,
|
|
921
|
+
fsPromises: {
|
|
922
|
+
readFile: async (p) => {
|
|
923
|
+
if (!memFs.has(p)) { const e = new Error('x'); e.code = 'ENOENT'; throw e; }
|
|
924
|
+
return memFs.get(p);
|
|
925
|
+
},
|
|
926
|
+
writeFile: async (p, c) => { memFs.set(p, String(c)); },
|
|
927
|
+
mkdir: async () => {},
|
|
928
|
+
rename: async (a, b) => { memFs.set(b, memFs.get(a)); memFs.delete(a); },
|
|
929
|
+
unlink: async (p) => { memFs.delete(p); },
|
|
930
|
+
},
|
|
931
|
+
};
|
|
932
|
+
};
|
|
933
|
+
const mappingPath = '/data/tailscale-mapping.json';
|
|
934
|
+
|
|
935
|
+
it('refuses to apply on a failed status query and never clears the record', async () => {
|
|
936
|
+
const { memFs, fsPromises } = memFsFor();
|
|
937
|
+
const applied = [];
|
|
938
|
+
const removed = [];
|
|
939
|
+
const service = createTailscaleService({
|
|
940
|
+
existsSync: () => false,
|
|
941
|
+
dataDir: '/data',
|
|
942
|
+
getPort: () => 3000,
|
|
943
|
+
isUiAuthEnabled: () => true,
|
|
944
|
+
isUnsafeUnauthenticatedLanAllowed: () => false,
|
|
945
|
+
runner: {
|
|
946
|
+
runTailscale: async (args) => {
|
|
947
|
+
if (args.includes('--bg')) {
|
|
948
|
+
applied.push(args);
|
|
949
|
+
return { ok: true, code: 0, stdout: '', stderr: '', spawnError: null, timedOut: false };
|
|
950
|
+
}
|
|
951
|
+
if (args.includes('off')) {
|
|
952
|
+
removed.push(args);
|
|
953
|
+
return { ok: true, code: 0, stdout: '', stderr: '', spawnError: null, timedOut: false };
|
|
954
|
+
}
|
|
955
|
+
if (args.includes('serve') && args.includes('status')) {
|
|
956
|
+
return { ok: false, code: 1, stdout: '', stderr: 'boom', spawnError: null, timedOut: false };
|
|
957
|
+
}
|
|
958
|
+
return { ok: true, code: 0, stdout: STATUS_JSON, stderr: '', spawnError: null, timedOut: false };
|
|
959
|
+
},
|
|
960
|
+
},
|
|
961
|
+
fsPromises,
|
|
962
|
+
fetchImpl: async () => ({ ok: false, status: 404, json: async () => null }),
|
|
963
|
+
logWarn: () => {},
|
|
964
|
+
});
|
|
965
|
+
await service.loadConfig();
|
|
966
|
+
const result = await service.setConfig({ enabled: true, mode: 'private', httpsPort: 443 });
|
|
967
|
+
expect(result.state).toBe('error');
|
|
968
|
+
expect(result.errorCode).toBe('status_query_failed');
|
|
969
|
+
expect(applied).toEqual([]);
|
|
970
|
+
expect(removed).toEqual([]);
|
|
971
|
+
expect(memFs.has(mappingPath)).toBe(false);
|
|
972
|
+
service.dispose();
|
|
973
|
+
});
|
|
974
|
+
|
|
975
|
+
it('ends a public-to-private switch in error with the old funnel record kept', async () => {
|
|
976
|
+
const { memFs, fsPromises } = memFsFor();
|
|
977
|
+
// Stale public record from a previous run; the funnel status query fails.
|
|
978
|
+
memFs.set(mappingPath, JSON.stringify({ httpsPort: 443, mode: 'public', localPort: 3000 }));
|
|
979
|
+
const applied = [];
|
|
980
|
+
const service = createTailscaleService({
|
|
981
|
+
existsSync: () => false,
|
|
982
|
+
dataDir: '/data',
|
|
983
|
+
getPort: () => 3000,
|
|
984
|
+
isUiAuthEnabled: () => true,
|
|
985
|
+
isUnsafeUnauthenticatedLanAllowed: () => false,
|
|
986
|
+
runner: {
|
|
987
|
+
runTailscale: async (args) => {
|
|
988
|
+
if (args.includes('--bg')) {
|
|
989
|
+
applied.push(args);
|
|
990
|
+
return { ok: true, code: 0, stdout: '', stderr: '', spawnError: null, timedOut: false };
|
|
991
|
+
}
|
|
992
|
+
if (args[0] === 'tailscale' && args[1] === 'funnel' && args.includes('status')) {
|
|
993
|
+
return { ok: false, code: 1, stdout: '', stderr: 'boom', spawnError: null, timedOut: false };
|
|
994
|
+
}
|
|
995
|
+
if (args.includes('serve') && args.includes('status')) {
|
|
996
|
+
return { ok: true, code: 0, stdout: JSON.stringify({ Web: {} }), stderr: '', spawnError: null, timedOut: false };
|
|
997
|
+
}
|
|
998
|
+
return { ok: true, code: 0, stdout: STATUS_JSON, stderr: '', spawnError: null, timedOut: false };
|
|
999
|
+
},
|
|
1000
|
+
},
|
|
1001
|
+
fsPromises,
|
|
1002
|
+
fetchImpl: async () => ({ ok: false, status: 404, json: async () => null }),
|
|
1003
|
+
logWarn: () => {},
|
|
1004
|
+
});
|
|
1005
|
+
await service.loadConfig();
|
|
1006
|
+
// Bypass setConfig persistence: enable private directly, so reconcile
|
|
1007
|
+
// must first clean the stale public record (whose query fails).
|
|
1008
|
+
const { persistTailscaleConfigForStartup } = await import('./service.js');
|
|
1009
|
+
await persistTailscaleConfigForStartup({
|
|
1010
|
+
dataDir: '/data',
|
|
1011
|
+
patch: { enabled: true, mode: 'private', httpsPort: 443 },
|
|
1012
|
+
fsPromises,
|
|
1013
|
+
uiPasswordConfigured: true,
|
|
1014
|
+
});
|
|
1015
|
+
await service.loadConfig();
|
|
1016
|
+
const result = await service.reconcile();
|
|
1017
|
+
expect(result.state).toBe('error');
|
|
1018
|
+
expect(result.errorCode).toBe('status_query_failed');
|
|
1019
|
+
// No new mapping applied; the old funnel record stays for cleanup.
|
|
1020
|
+
expect(applied).toEqual([]);
|
|
1021
|
+
expect(memFs.has(mappingPath)).toBe(true);
|
|
1022
|
+
service.dispose();
|
|
1023
|
+
});
|
|
1024
|
+
});
|
|
1025
|
+
|
|
1026
|
+
describe('tailscale generation bump cancels in-flight apply (F6)', () => {
|
|
1027
|
+
it('aborts the stale apply and removes the mapping it created', async () => {
|
|
1028
|
+
const memFs = new Map();
|
|
1029
|
+
const mappingPath = '/data/tailscale-mapping.json';
|
|
1030
|
+
const fsPromises = {
|
|
1031
|
+
readFile: async (p) => {
|
|
1032
|
+
if (!memFs.has(p)) { const e = new Error('x'); e.code = 'ENOENT'; throw e; }
|
|
1033
|
+
return memFs.get(p);
|
|
1034
|
+
},
|
|
1035
|
+
writeFile: async (p, c) => { memFs.set(p, String(c)); },
|
|
1036
|
+
mkdir: async () => {},
|
|
1037
|
+
rename: async (a, b) => { memFs.set(b, memFs.get(a)); memFs.delete(a); },
|
|
1038
|
+
unlink: async (p) => { memFs.delete(p); },
|
|
1039
|
+
};
|
|
1040
|
+
const removed = [];
|
|
1041
|
+
let apply443Started = null;
|
|
1042
|
+
const apply443Aborted = [];
|
|
1043
|
+
const serveWeb = () => JSON.stringify({
|
|
1044
|
+
Web: { 'mymachine.tailabc.ts.net:443': { Handlers: { '/': 'http://127.0.0.1:3000' } } },
|
|
1045
|
+
});
|
|
1046
|
+
const runner = {
|
|
1047
|
+
runTailscale: (args, { signal } = {}) => {
|
|
1048
|
+
if (args.includes('--bg') && args.includes('--https=443')) {
|
|
1049
|
+
apply443Started?.();
|
|
1050
|
+
// Block until the generation bump aborts this apply.
|
|
1051
|
+
return new Promise((resolve) => {
|
|
1052
|
+
const done = (aborted) => resolve({
|
|
1053
|
+
ok: false, code: aborted ? null : 0, stdout: '', stderr: '', spawnError: aborted ? 'aborted' : null, timedOut: false,
|
|
1054
|
+
});
|
|
1055
|
+
if (signal?.aborted) return done(true);
|
|
1056
|
+
signal?.addEventListener?.('abort', () => done(true), { once: true });
|
|
1057
|
+
apply443Aborted.push(signal);
|
|
1058
|
+
});
|
|
1059
|
+
}
|
|
1060
|
+
if (args.includes('off')) {
|
|
1061
|
+
removed.push(args);
|
|
1062
|
+
return Promise.resolve({ ok: true, code: 0, stdout: '', stderr: '', spawnError: null, timedOut: false });
|
|
1063
|
+
}
|
|
1064
|
+
if (args.includes('serve') && args.includes('status')) {
|
|
1065
|
+
return Promise.resolve({ ok: true, code: 0, stdout: serveWeb(), stderr: '', spawnError: null, timedOut: false });
|
|
1066
|
+
}
|
|
1067
|
+
return Promise.resolve({ ok: true, code: 0, stdout: STATUS_JSON, stderr: '', spawnError: null, timedOut: false });
|
|
1068
|
+
},
|
|
1069
|
+
};
|
|
1070
|
+
const service = createTailscaleService({
|
|
1071
|
+
existsSync: () => false,
|
|
1072
|
+
dataDir: '/data',
|
|
1073
|
+
getPort: () => 3000,
|
|
1074
|
+
isUiAuthEnabled: () => true,
|
|
1075
|
+
isUnsafeUnauthenticatedLanAllowed: () => false,
|
|
1076
|
+
runner,
|
|
1077
|
+
fsPromises,
|
|
1078
|
+
getServerId: async () => TEST_SERVER_ID,
|
|
1079
|
+
fetchImpl: matchingFetchImpl,
|
|
1080
|
+
logWarn: () => {},
|
|
1081
|
+
});
|
|
1082
|
+
await service.loadConfig();
|
|
1083
|
+
let startedResolve;
|
|
1084
|
+
const started = new Promise((resolve) => { startedResolve = resolve; });
|
|
1085
|
+
apply443Started = startedResolve;
|
|
1086
|
+
const first = service.setConfig({ enabled: true, mode: 'private', httpsPort: 443 });
|
|
1087
|
+
await started;
|
|
1088
|
+
// Generation bump while the 443 apply is in flight.
|
|
1089
|
+
const second = service.setConfig({ enabled: true, mode: 'private', httpsPort: 8443 });
|
|
1090
|
+
const [firstResult, secondResult] = await Promise.all([first, second]);
|
|
1091
|
+
// The superseded reconcile resolves (never hangs on the killed apply).
|
|
1092
|
+
expect(firstResult).toBeDefined();
|
|
1093
|
+
expect(apply443Aborted.length).toBe(1);
|
|
1094
|
+
expect(apply443Aborted[0]?.aborted).toBe(true);
|
|
1095
|
+
// The stale 443 mapping (still live, pointing at our port) is removed;
|
|
1096
|
+
// the live generation owns 8443.
|
|
1097
|
+
expect(removed.some((args) => args.includes('--https=443') && args.includes('off'))).toBe(true);
|
|
1098
|
+
expect(secondResult.state).toBe('active');
|
|
1099
|
+
expect(secondResult.url).toBe('https://mymachine.tailabc.ts.net:8443');
|
|
1100
|
+
service.dispose();
|
|
1101
|
+
});
|
|
1102
|
+
});
|
|
1103
|
+
|
|
1104
|
+
describe('tailscale crash-restart repoint (record-owned mapping on a new port)', () => {
|
|
1105
|
+
const memFsFor = () => {
|
|
1106
|
+
const memFs = new Map();
|
|
1107
|
+
return {
|
|
1108
|
+
memFs,
|
|
1109
|
+
fsPromises: {
|
|
1110
|
+
readFile: async (p) => {
|
|
1111
|
+
if (!memFs.has(p)) { const e = new Error('x'); e.code = 'ENOENT'; throw e; }
|
|
1112
|
+
return memFs.get(p);
|
|
1113
|
+
},
|
|
1114
|
+
writeFile: async (p, c) => { memFs.set(p, String(c)); },
|
|
1115
|
+
mkdir: async () => {},
|
|
1116
|
+
rename: async (a, b) => { memFs.set(b, memFs.get(a)); memFs.delete(a); },
|
|
1117
|
+
unlink: async (p) => { memFs.delete(p); },
|
|
1118
|
+
},
|
|
1119
|
+
};
|
|
1120
|
+
};
|
|
1121
|
+
const serveWeb = (httpsPort, localPort) => JSON.stringify({
|
|
1122
|
+
Web: { [`mymachine.tailabc.ts.net:${httpsPort}`]: { Handlers: { '/': `http://127.0.0.1:${localPort}` } } },
|
|
1123
|
+
});
|
|
1124
|
+
// Simulates a restart on a new bound port with a persisted record+config.
|
|
1125
|
+
// `liveLocalPort` fronts the HTTPS port in serve/funnel status; `record`
|
|
1126
|
+
// is the persisted mapping record (null = crash left no record).
|
|
1127
|
+
const restart = async ({ mode, httpsPort, boundPort, liveLocalPort, record, serveStatus }) => {
|
|
1128
|
+
const { memFs, fsPromises } = memFsFor();
|
|
1129
|
+
memFs.set('/data/tailscale-config.json', JSON.stringify({ enabled: true, mode, httpsPort }));
|
|
1130
|
+
if (record) memFs.set('/data/tailscale-mapping.json', JSON.stringify(record));
|
|
1131
|
+
const applied = [];
|
|
1132
|
+
const subcommand = mode === 'public' ? 'funnel' : 'serve';
|
|
1133
|
+
const service = createTailscaleService({
|
|
1134
|
+
existsSync: () => false,
|
|
1135
|
+
dataDir: '/data',
|
|
1136
|
+
getPort: () => boundPort,
|
|
1137
|
+
isUiAuthEnabled: () => true,
|
|
1138
|
+
isUnsafeUnauthenticatedLanAllowed: () => false,
|
|
1139
|
+
runner: {
|
|
1140
|
+
runTailscale: async (args) => {
|
|
1141
|
+
if (args.includes('--bg')) {
|
|
1142
|
+
applied.push(args);
|
|
1143
|
+
return { ok: true, code: 0, stdout: '', stderr: '', spawnError: null, timedOut: false };
|
|
1144
|
+
}
|
|
1145
|
+
if (args.includes(subcommand) && args.includes('status')) {
|
|
1146
|
+
return { ok: true, code: 0, stdout: serveStatus ?? serveWeb(httpsPort, liveLocalPort), stderr: '', spawnError: null, timedOut: false };
|
|
1147
|
+
}
|
|
1148
|
+
return { ok: true, code: 0, stdout: STATUS_JSON, stderr: '', spawnError: null, timedOut: false };
|
|
1149
|
+
},
|
|
1150
|
+
},
|
|
1151
|
+
fsPromises,
|
|
1152
|
+
getServerId: async () => TEST_SERVER_ID,
|
|
1153
|
+
fetchImpl: matchingFetchImpl,
|
|
1154
|
+
logWarn: () => {},
|
|
1155
|
+
});
|
|
1156
|
+
await service.loadConfig();
|
|
1157
|
+
const result = await service.reconcile();
|
|
1158
|
+
const recordRaw = memFs.has('/data/tailscale-mapping.json') ? memFs.get('/data/tailscale-mapping.json') : null;
|
|
1159
|
+
service.dispose();
|
|
1160
|
+
return { result, applied, record: recordRaw ? JSON.parse(recordRaw) : null };
|
|
1161
|
+
};
|
|
1162
|
+
|
|
1163
|
+
it('repoints a record-owned mapping to the new bound port instead of conflict', async () => {
|
|
1164
|
+
const { result, applied, record } = await restart({
|
|
1165
|
+
mode: 'private', httpsPort: 443, boundPort: 3001, liveLocalPort: 3000,
|
|
1166
|
+
record: { httpsPort: 443, mode: 'private', localPort: 3000 },
|
|
1167
|
+
});
|
|
1168
|
+
expect(result.state).toBe('active');
|
|
1169
|
+
expect(result.url).toBe('https://mymachine.tailabc.ts.net');
|
|
1170
|
+
expect(applied).toEqual([
|
|
1171
|
+
['tailscale', ...buildTailscaleServeArgs({ mode: 'private', httpsPort: 443, localPort: 3001 })],
|
|
1172
|
+
]);
|
|
1173
|
+
expect(record).toEqual({ httpsPort: 443, mode: 'private', localPort: 3001 });
|
|
1174
|
+
});
|
|
1175
|
+
|
|
1176
|
+
it('reports conflict when the live mapping points at neither the recorded nor the current port', async () => {
|
|
1177
|
+
const { result, applied, record } = await restart({
|
|
1178
|
+
mode: 'private', httpsPort: 443, boundPort: 3001, liveLocalPort: 4000,
|
|
1179
|
+
record: { httpsPort: 443, mode: 'private', localPort: 3000 },
|
|
1180
|
+
});
|
|
1181
|
+
expect(result.state).toBe('conflict');
|
|
1182
|
+
expect(result.errorCode).toBe('conflict');
|
|
1183
|
+
expect(applied).toEqual([]);
|
|
1184
|
+
expect(record).toEqual({ httpsPort: 443, mode: 'private', localPort: 3000 });
|
|
1185
|
+
});
|
|
1186
|
+
|
|
1187
|
+
it('reports conflict when a stale live mapping has no record', async () => {
|
|
1188
|
+
const { result, applied, record } = await restart({
|
|
1189
|
+
mode: 'private', httpsPort: 443, boundPort: 3001, liveLocalPort: 3000, record: null,
|
|
1190
|
+
});
|
|
1191
|
+
expect(result.state).toBe('conflict');
|
|
1192
|
+
expect(result.errorCode).toBe('conflict');
|
|
1193
|
+
expect(applied).toEqual([]);
|
|
1194
|
+
expect(record).toBeNull();
|
|
1195
|
+
});
|
|
1196
|
+
|
|
1197
|
+
it('repoints a record-owned funnel mapping to the new bound port', async () => {
|
|
1198
|
+
const { result, applied, record } = await restart({
|
|
1199
|
+
mode: 'public', httpsPort: 443, boundPort: 3001, liveLocalPort: 3000,
|
|
1200
|
+
record: { httpsPort: 443, mode: 'public', localPort: 3000 },
|
|
1201
|
+
});
|
|
1202
|
+
expect(result.state).toBe('active');
|
|
1203
|
+
expect(applied).toEqual([
|
|
1204
|
+
['tailscale', ...buildTailscaleServeArgs({ mode: 'public', httpsPort: 443, localPort: 3001 })],
|
|
1205
|
+
]);
|
|
1206
|
+
expect(applied[0][1]).toBe('funnel');
|
|
1207
|
+
expect(record).toEqual({ httpsPort: 443, mode: 'public', localPort: 3001 });
|
|
1208
|
+
});
|
|
1209
|
+
});
|
|
1210
|
+
|
|
1211
|
+
describe('tailscale removal-failure handling (remove_failed)', () => {
|
|
1212
|
+
const mappingPath = '/data/tailscale-mapping.json';
|
|
1213
|
+
const memFsFor = () => {
|
|
1214
|
+
const memFs = new Map();
|
|
1215
|
+
return {
|
|
1216
|
+
memFs,
|
|
1217
|
+
fsPromises: {
|
|
1218
|
+
readFile: async (p) => {
|
|
1219
|
+
if (!memFs.has(p)) { const e = new Error('x'); e.code = 'ENOENT'; throw e; }
|
|
1220
|
+
return memFs.get(p);
|
|
1221
|
+
},
|
|
1222
|
+
writeFile: async (p, c) => { memFs.set(p, String(c)); },
|
|
1223
|
+
mkdir: async () => {},
|
|
1224
|
+
rename: async (a, b) => { memFs.set(b, memFs.get(a)); memFs.delete(a); },
|
|
1225
|
+
unlink: async (p) => { memFs.delete(p); },
|
|
1226
|
+
},
|
|
1227
|
+
};
|
|
1228
|
+
};
|
|
1229
|
+
const liveWeb = (httpsPort, localPort) => JSON.stringify({
|
|
1230
|
+
Web: { [`mymachine.tailabc.ts.net:${httpsPort}`]: { Handlers: { '/': `http://127.0.0.1:${localPort}` } } },
|
|
1231
|
+
});
|
|
1232
|
+
// A failed `off` must not look like the "no handler" success case.
|
|
1233
|
+
const removalFailure = () => ({
|
|
1234
|
+
ok: false, code: 1, stdout: '', stderr: 'Error: tailscaled timed out removing the mapping', spawnError: null, timedOut: false,
|
|
1235
|
+
});
|
|
1236
|
+
const removalSuccess = () => ({ ok: true, code: 0, stdout: '', stderr: '', spawnError: null, timedOut: false });
|
|
1237
|
+
|
|
1238
|
+
it('disabled + failed removal reports remove_failed, keeps the record, and retry clears it', async () => {
|
|
1239
|
+
const { memFs, fsPromises } = memFsFor();
|
|
1240
|
+
memFs.set(mappingPath, JSON.stringify({ httpsPort: 443, mode: 'private', localPort: 3000 }));
|
|
1241
|
+
let failRemoval = true;
|
|
1242
|
+
const removed = [];
|
|
1243
|
+
const service = createTailscaleService({
|
|
1244
|
+
existsSync: () => false,
|
|
1245
|
+
dataDir: '/data',
|
|
1246
|
+
getPort: () => 3000,
|
|
1247
|
+
isUiAuthEnabled: () => true,
|
|
1248
|
+
isUnsafeUnauthenticatedLanAllowed: () => false,
|
|
1249
|
+
runner: {
|
|
1250
|
+
runTailscale: async (args) => {
|
|
1251
|
+
if (args.includes('off')) {
|
|
1252
|
+
removed.push(args);
|
|
1253
|
+
return failRemoval ? removalFailure() : removalSuccess();
|
|
1254
|
+
}
|
|
1255
|
+
if (args.includes('serve') && args.includes('status')) {
|
|
1256
|
+
return { ok: true, code: 0, stdout: liveWeb(443, 3000), stderr: '', spawnError: null, timedOut: false };
|
|
1257
|
+
}
|
|
1258
|
+
return { ok: true, code: 0, stdout: STATUS_JSON, stderr: '', spawnError: null, timedOut: false };
|
|
1259
|
+
},
|
|
1260
|
+
},
|
|
1261
|
+
fsPromises,
|
|
1262
|
+
fetchImpl: async () => ({ ok: false, status: 404, json: async () => null }),
|
|
1263
|
+
logWarn: () => {},
|
|
1264
|
+
});
|
|
1265
|
+
await service.loadConfig();
|
|
1266
|
+
const result = await service.reconcile();
|
|
1267
|
+
expect(result.state).toBe('error');
|
|
1268
|
+
expect(result.errorCode).toBe('remove_failed');
|
|
1269
|
+
expect(result.url).toBeNull();
|
|
1270
|
+
expect(result.errorMessage).toContain('may still be reachable');
|
|
1271
|
+
expect(result.errorMessage).not.toContain('reset');
|
|
1272
|
+
// Host prerequisites still refresh on the error return.
|
|
1273
|
+
expect(result.installed).toBe(true);
|
|
1274
|
+
expect(result.loggedIn).toBe(true);
|
|
1275
|
+
// The record survives so retry can remove the still-live mapping.
|
|
1276
|
+
expect(memFs.has(mappingPath)).toBe(true);
|
|
1277
|
+
failRemoval = false;
|
|
1278
|
+
const retried = await service.retry();
|
|
1279
|
+
expect(retried.state).toBe('off');
|
|
1280
|
+
expect(retried.errorCode).toBeNull();
|
|
1281
|
+
expect(memFs.has(mappingPath)).toBe(false);
|
|
1282
|
+
expect(removed.length).toBe(2);
|
|
1283
|
+
service.dispose();
|
|
1284
|
+
});
|
|
1285
|
+
|
|
1286
|
+
it('public-to-private switch with failed funnel removal applies nothing and keeps the old record', async () => {
|
|
1287
|
+
const { memFs, fsPromises } = memFsFor();
|
|
1288
|
+
memFs.set('/data/tailscale-config.json', JSON.stringify({ enabled: true, mode: 'private', httpsPort: 443 }));
|
|
1289
|
+
const oldRecord = { httpsPort: 8443, mode: 'public', localPort: 3000 };
|
|
1290
|
+
memFs.set(mappingPath, JSON.stringify(oldRecord));
|
|
1291
|
+
const applied = [];
|
|
1292
|
+
const removed = [];
|
|
1293
|
+
const service = createTailscaleService({
|
|
1294
|
+
existsSync: () => false,
|
|
1295
|
+
dataDir: '/data',
|
|
1296
|
+
getPort: () => 3000,
|
|
1297
|
+
isUiAuthEnabled: () => true,
|
|
1298
|
+
isUnsafeUnauthenticatedLanAllowed: () => false,
|
|
1299
|
+
runner: {
|
|
1300
|
+
runTailscale: async (args) => {
|
|
1301
|
+
if (args.includes('off')) {
|
|
1302
|
+
removed.push(args);
|
|
1303
|
+
return removalFailure();
|
|
1304
|
+
}
|
|
1305
|
+
if (args.includes('--bg')) {
|
|
1306
|
+
applied.push(args);
|
|
1307
|
+
return removalSuccess();
|
|
1308
|
+
}
|
|
1309
|
+
if (args[1] === 'funnel' && args.includes('status')) {
|
|
1310
|
+
return { ok: true, code: 0, stdout: liveWeb(8443, 3000), stderr: '', spawnError: null, timedOut: false };
|
|
1311
|
+
}
|
|
1312
|
+
return { ok: true, code: 0, stdout: STATUS_JSON, stderr: '', spawnError: null, timedOut: false };
|
|
1313
|
+
},
|
|
1314
|
+
},
|
|
1315
|
+
fsPromises,
|
|
1316
|
+
fetchImpl: async () => ({ ok: false, status: 404, json: async () => null }),
|
|
1317
|
+
logWarn: () => {},
|
|
1318
|
+
});
|
|
1319
|
+
await service.loadConfig();
|
|
1320
|
+
const result = await service.reconcile();
|
|
1321
|
+
expect(result.state).toBe('error');
|
|
1322
|
+
expect(result.errorCode).toBe('remove_failed');
|
|
1323
|
+
expect(result.errorMessage).toContain('Funnel');
|
|
1324
|
+
expect(result.errorMessage).toContain('8443');
|
|
1325
|
+
// The funnel removal was attempted, but no new serve mapping applied.
|
|
1326
|
+
expect(removed.some((args) => args[1] === 'funnel' && args.includes('off'))).toBe(true);
|
|
1327
|
+
expect(applied).toEqual([]);
|
|
1328
|
+
expect(JSON.parse(memFs.get(mappingPath))).toEqual(oldRecord);
|
|
1329
|
+
service.dispose();
|
|
1330
|
+
});
|
|
1331
|
+
|
|
1332
|
+
it('blocked + failed removal reports remove_failed and keeps the record', async () => {
|
|
1333
|
+
const { memFs, fsPromises } = memFsFor();
|
|
1334
|
+
memFs.set('/data/tailscale-config.json', JSON.stringify({ enabled: true, mode: 'private', httpsPort: 443 }));
|
|
1335
|
+
memFs.set(mappingPath, JSON.stringify({ httpsPort: 443, mode: 'private', localPort: 3000 }));
|
|
1336
|
+
const service = createTailscaleService({
|
|
1337
|
+
existsSync: () => false,
|
|
1338
|
+
dataDir: '/data',
|
|
1339
|
+
getPort: () => 3000,
|
|
1340
|
+
isUiAuthEnabled: () => false,
|
|
1341
|
+
isUnsafeUnauthenticatedLanAllowed: () => false,
|
|
1342
|
+
runner: {
|
|
1343
|
+
runTailscale: async (args) => {
|
|
1344
|
+
if (args.includes('off')) return removalFailure();
|
|
1345
|
+
if (args.includes('serve') && args.includes('status')) {
|
|
1346
|
+
return { ok: true, code: 0, stdout: liveWeb(443, 3000), stderr: '', spawnError: null, timedOut: false };
|
|
1347
|
+
}
|
|
1348
|
+
return { ok: true, code: 0, stdout: STATUS_JSON, stderr: '', spawnError: null, timedOut: false };
|
|
1349
|
+
},
|
|
1350
|
+
},
|
|
1351
|
+
fsPromises,
|
|
1352
|
+
fetchImpl: async () => ({ ok: false, status: 404, json: async () => null }),
|
|
1353
|
+
logWarn: () => {},
|
|
1354
|
+
});
|
|
1355
|
+
await service.loadConfig();
|
|
1356
|
+
// A surviving mapping without auth must never be reported as blocked.
|
|
1357
|
+
const result = await service.reconcile();
|
|
1358
|
+
expect(result.state).toBe('error');
|
|
1359
|
+
expect(result.errorCode).toBe('remove_failed');
|
|
1360
|
+
expect(memFs.has(mappingPath)).toBe(true);
|
|
1361
|
+
service.dispose();
|
|
1362
|
+
});
|
|
1363
|
+
|
|
1364
|
+
it('blocked + status query failure reports status_query_failed and keeps the record', async () => {
|
|
1365
|
+
const { memFs, fsPromises } = memFsFor();
|
|
1366
|
+
memFs.set('/data/tailscale-config.json', JSON.stringify({ enabled: true, mode: 'private', httpsPort: 443 }));
|
|
1367
|
+
memFs.set(mappingPath, JSON.stringify({ httpsPort: 443, mode: 'private', localPort: 3000 }));
|
|
1368
|
+
const removed = [];
|
|
1369
|
+
const service = createTailscaleService({
|
|
1370
|
+
existsSync: () => false,
|
|
1371
|
+
dataDir: '/data',
|
|
1372
|
+
getPort: () => 3000,
|
|
1373
|
+
isUiAuthEnabled: () => false,
|
|
1374
|
+
isUnsafeUnauthenticatedLanAllowed: () => false,
|
|
1375
|
+
runner: {
|
|
1376
|
+
runTailscale: async (args) => {
|
|
1377
|
+
if (args.includes('off')) {
|
|
1378
|
+
removed.push(args);
|
|
1379
|
+
return removalSuccess();
|
|
1380
|
+
}
|
|
1381
|
+
if (args.includes('serve') && args.includes('status')) {
|
|
1382
|
+
return { ok: false, code: 1, stdout: '', stderr: 'boom', spawnError: null, timedOut: false };
|
|
1383
|
+
}
|
|
1384
|
+
return { ok: true, code: 0, stdout: STATUS_JSON, stderr: '', spawnError: null, timedOut: false };
|
|
1385
|
+
},
|
|
1386
|
+
},
|
|
1387
|
+
fsPromises,
|
|
1388
|
+
fetchImpl: async () => ({ ok: false, status: 404, json: async () => null }),
|
|
1389
|
+
logWarn: () => {},
|
|
1390
|
+
});
|
|
1391
|
+
await service.loadConfig();
|
|
1392
|
+
const result = await service.reconcile();
|
|
1393
|
+
expect(result.state).toBe('error');
|
|
1394
|
+
expect(result.errorCode).toBe('status_query_failed');
|
|
1395
|
+
expect(removed).toEqual([]);
|
|
1396
|
+
expect(memFs.has(mappingPath)).toBe(true);
|
|
1397
|
+
service.dispose();
|
|
1398
|
+
});
|
|
1399
|
+
|
|
1400
|
+
it('shutdown with failed removal keeps the record and warns; success clears it', async () => {
|
|
1401
|
+
const { memFs, fsPromises } = memFsFor();
|
|
1402
|
+
memFs.set(mappingPath, JSON.stringify({ httpsPort: 443, mode: 'private', localPort: 3000 }));
|
|
1403
|
+
let failRemoval = true;
|
|
1404
|
+
const warnings = [];
|
|
1405
|
+
const service = createTailscaleService({
|
|
1406
|
+
existsSync: () => false,
|
|
1407
|
+
dataDir: '/data',
|
|
1408
|
+
getPort: () => 3000,
|
|
1409
|
+
isUiAuthEnabled: () => true,
|
|
1410
|
+
isUnsafeUnauthenticatedLanAllowed: () => false,
|
|
1411
|
+
runner: {
|
|
1412
|
+
runTailscale: async (args) => {
|
|
1413
|
+
if (args.includes('off')) return failRemoval ? removalFailure() : removalSuccess();
|
|
1414
|
+
if (args.includes('serve') && args.includes('status')) {
|
|
1415
|
+
return { ok: true, code: 0, stdout: liveWeb(443, 3000), stderr: '', spawnError: null, timedOut: false };
|
|
1416
|
+
}
|
|
1417
|
+
return { ok: true, code: 0, stdout: STATUS_JSON, stderr: '', spawnError: null, timedOut: false };
|
|
1418
|
+
},
|
|
1419
|
+
},
|
|
1420
|
+
fsPromises,
|
|
1421
|
+
fetchImpl: async () => ({ ok: false, status: 404, json: async () => null }),
|
|
1422
|
+
logWarn: (message) => warnings.push(String(message)),
|
|
1423
|
+
});
|
|
1424
|
+
await service.loadConfig();
|
|
1425
|
+
await service.shutdown();
|
|
1426
|
+
expect(memFs.has(mappingPath)).toBe(true);
|
|
1427
|
+
expect(warnings.some((message) => /leaving it for next-start cleanup/i.test(message))).toBe(true);
|
|
1428
|
+
service.dispose();
|
|
1429
|
+
|
|
1430
|
+
failRemoval = false;
|
|
1431
|
+
warnings.length = 0;
|
|
1432
|
+
const retry = createTailscaleService({
|
|
1433
|
+
existsSync: () => false,
|
|
1434
|
+
dataDir: '/data',
|
|
1435
|
+
getPort: () => 3000,
|
|
1436
|
+
isUiAuthEnabled: () => true,
|
|
1437
|
+
isUnsafeUnauthenticatedLanAllowed: () => false,
|
|
1438
|
+
runner: {
|
|
1439
|
+
runTailscale: async (args) => {
|
|
1440
|
+
if (args.includes('off')) return removalSuccess();
|
|
1441
|
+
if (args.includes('serve') && args.includes('status')) {
|
|
1442
|
+
return { ok: true, code: 0, stdout: liveWeb(443, 3000), stderr: '', spawnError: null, timedOut: false };
|
|
1443
|
+
}
|
|
1444
|
+
return { ok: true, code: 0, stdout: STATUS_JSON, stderr: '', spawnError: null, timedOut: false };
|
|
1445
|
+
},
|
|
1446
|
+
},
|
|
1447
|
+
fsPromises,
|
|
1448
|
+
fetchImpl: async () => ({ ok: false, status: 404, json: async () => null }),
|
|
1449
|
+
logWarn: (message) => warnings.push(String(message)),
|
|
1450
|
+
});
|
|
1451
|
+
await retry.loadConfig();
|
|
1452
|
+
await retry.shutdown();
|
|
1453
|
+
expect(memFs.has(mappingPath)).toBe(false);
|
|
1454
|
+
retry.dispose();
|
|
1455
|
+
});
|
|
1456
|
+
});
|
|
1457
|
+
|
|
1458
|
+
describe('tailscale config-response grace (slow apply)', () => {
|
|
1459
|
+
const memFsFor = () => {
|
|
1460
|
+
const memFs = new Map();
|
|
1461
|
+
return {
|
|
1462
|
+
memFs,
|
|
1463
|
+
fsPromises: {
|
|
1464
|
+
readFile: async (p) => {
|
|
1465
|
+
if (!memFs.has(p)) { const e = new Error('x'); e.code = 'ENOENT'; throw e; }
|
|
1466
|
+
return memFs.get(p);
|
|
1467
|
+
},
|
|
1468
|
+
writeFile: async (p, c) => { memFs.set(p, String(c)); },
|
|
1469
|
+
mkdir: async () => {},
|
|
1470
|
+
rename: async (a, b) => { memFs.set(b, memFs.get(a)); memFs.delete(a); },
|
|
1471
|
+
unlink: async (p) => { memFs.delete(p); },
|
|
1472
|
+
},
|
|
1473
|
+
};
|
|
1474
|
+
};
|
|
1475
|
+
const waitFor = async (predicate, timeoutMs = 2000) => {
|
|
1476
|
+
const deadline = Date.now() + timeoutMs;
|
|
1477
|
+
for (;;) {
|
|
1478
|
+
if (predicate()) return;
|
|
1479
|
+
if (Date.now() >= deadline) throw new Error('timed out waiting for background reconcile');
|
|
1480
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
1481
|
+
}
|
|
1482
|
+
};
|
|
1483
|
+
|
|
1484
|
+
it('setConfig returns transitional within the grace while the apply waits, then goes active', async () => {
|
|
1485
|
+
const { memFs, fsPromises } = memFsFor();
|
|
1486
|
+
let releaseApply;
|
|
1487
|
+
const applyGate = new Promise((resolve) => { releaseApply = resolve; });
|
|
1488
|
+
let service;
|
|
1489
|
+
service = createTailscaleService({
|
|
1490
|
+
existsSync: () => false,
|
|
1491
|
+
dataDir: '/data',
|
|
1492
|
+
getPort: () => 3000,
|
|
1493
|
+
isUiAuthEnabled: () => true,
|
|
1494
|
+
isUnsafeUnauthenticatedLanAllowed: () => false,
|
|
1495
|
+
configResponseGraceMs: 20,
|
|
1496
|
+
runner: {
|
|
1497
|
+
runTailscale: (args, { onOutput } = {}) => {
|
|
1498
|
+
if (args.includes('serve') && args.includes('status')) {
|
|
1499
|
+
return Promise.resolve({ ok: true, code: 0, stdout: JSON.stringify({ Web: {} }), stderr: '', spawnError: null, timedOut: false });
|
|
1500
|
+
}
|
|
1501
|
+
if (args.includes('--bg')) {
|
|
1502
|
+
onOutput?.('visit https://login.tailscale.com/f/serve?node=n123 to approve\n');
|
|
1503
|
+
return applyGate.then(() => ({ ok: true, code: 0, stdout: '', stderr: '', spawnError: null, timedOut: false }));
|
|
1504
|
+
}
|
|
1505
|
+
return Promise.resolve({ ok: true, code: 0, stdout: STATUS_JSON, stderr: '', spawnError: null, timedOut: false });
|
|
1506
|
+
},
|
|
1507
|
+
},
|
|
1508
|
+
fsPromises,
|
|
1509
|
+
getServerId: async () => TEST_SERVER_ID,
|
|
1510
|
+
fetchImpl: matchingFetchImpl,
|
|
1511
|
+
logWarn: () => {},
|
|
1512
|
+
});
|
|
1513
|
+
await service.loadConfig();
|
|
1514
|
+
const startedAt = Date.now();
|
|
1515
|
+
const early = await service.setConfig({ enabled: true, mode: 'private', httpsPort: 443 });
|
|
1516
|
+
// Grace (20ms) elapses while the apply is still gated: quick,
|
|
1517
|
+
// transitional response for pollers to follow up on.
|
|
1518
|
+
expect(Date.now() - startedAt).toBeLessThan(2000);
|
|
1519
|
+
expect(['starting', 'needs-approval']).toContain(early.state);
|
|
1520
|
+
releaseApply();
|
|
1521
|
+
await waitFor(() => service.getStatus().state === 'active');
|
|
1522
|
+
expect(service.getStatus().url).toBe('https://mymachine.tailabc.ts.net');
|
|
1523
|
+
expect(memFs.has('/data/tailscale-mapping.json')).toBe(true);
|
|
1524
|
+
service.dispose();
|
|
1525
|
+
});
|
|
1526
|
+
|
|
1527
|
+
it('retry returns transitional within the grace while the apply waits, then goes active', async () => {
|
|
1528
|
+
const { fsPromises } = memFsFor();
|
|
1529
|
+
let releaseApply;
|
|
1530
|
+
const applyGate = new Promise((resolve) => { releaseApply = resolve; });
|
|
1531
|
+
let service;
|
|
1532
|
+
// Pre-existing enabled config (as if set before the approval wait).
|
|
1533
|
+
const seedFs = {
|
|
1534
|
+
readFile: fsPromises.readFile,
|
|
1535
|
+
writeFile: fsPromises.writeFile,
|
|
1536
|
+
mkdir: fsPromises.mkdir,
|
|
1537
|
+
rename: fsPromises.rename,
|
|
1538
|
+
unlink: fsPromises.unlink,
|
|
1539
|
+
};
|
|
1540
|
+
await seedFs.writeFile('/data/tailscale-config.json', JSON.stringify({ enabled: true, mode: 'private', httpsPort: 443 }));
|
|
1541
|
+
service = createTailscaleService({
|
|
1542
|
+
existsSync: () => false,
|
|
1543
|
+
dataDir: '/data',
|
|
1544
|
+
getPort: () => 3000,
|
|
1545
|
+
isUiAuthEnabled: () => true,
|
|
1546
|
+
isUnsafeUnauthenticatedLanAllowed: () => false,
|
|
1547
|
+
configResponseGraceMs: 20,
|
|
1548
|
+
runner: {
|
|
1549
|
+
runTailscale: (args) => {
|
|
1550
|
+
if (args.includes('serve') && args.includes('status')) {
|
|
1551
|
+
return Promise.resolve({ ok: true, code: 0, stdout: JSON.stringify({ Web: {} }), stderr: '', spawnError: null, timedOut: false });
|
|
1552
|
+
}
|
|
1553
|
+
if (args.includes('--bg')) {
|
|
1554
|
+
return applyGate.then(() => ({ ok: true, code: 0, stdout: '', stderr: '', spawnError: null, timedOut: false }));
|
|
1555
|
+
}
|
|
1556
|
+
return Promise.resolve({ ok: true, code: 0, stdout: STATUS_JSON, stderr: '', spawnError: null, timedOut: false });
|
|
1557
|
+
},
|
|
1558
|
+
},
|
|
1559
|
+
fsPromises,
|
|
1560
|
+
getServerId: async () => TEST_SERVER_ID,
|
|
1561
|
+
fetchImpl: matchingFetchImpl,
|
|
1562
|
+
logWarn: () => {},
|
|
1563
|
+
});
|
|
1564
|
+
await service.loadConfig();
|
|
1565
|
+
const early = await service.retry();
|
|
1566
|
+
expect(early.state).toBe('starting');
|
|
1567
|
+
releaseApply();
|
|
1568
|
+
await waitFor(() => service.getStatus().state === 'active');
|
|
1569
|
+
expect(service.getStatus().url).toBe('https://mymachine.tailabc.ts.net');
|
|
1570
|
+
service.dispose();
|
|
1571
|
+
});
|
|
1572
|
+
|
|
1573
|
+
it('fast reconcile inside the grace returns the final status (disable -> off)', async () => {
|
|
1574
|
+
const { fsPromises } = memFsFor();
|
|
1575
|
+
let service;
|
|
1576
|
+
service = createTailscaleService({
|
|
1577
|
+
existsSync: () => false,
|
|
1578
|
+
dataDir: '/data',
|
|
1579
|
+
getPort: () => 3000,
|
|
1580
|
+
isUiAuthEnabled: () => true,
|
|
1581
|
+
isUnsafeUnauthenticatedLanAllowed: () => false,
|
|
1582
|
+
configResponseGraceMs: 20,
|
|
1583
|
+
runner: {
|
|
1584
|
+
runTailscale: async (args) => {
|
|
1585
|
+
if (args.includes('serve') && args.includes('status')) {
|
|
1586
|
+
return { ok: true, code: 0, stdout: JSON.stringify({ Web: {} }), stderr: '', spawnError: null, timedOut: false };
|
|
1587
|
+
}
|
|
1588
|
+
return { ok: true, code: 0, stdout: STATUS_JSON, stderr: '', spawnError: null, timedOut: false };
|
|
1589
|
+
},
|
|
1590
|
+
},
|
|
1591
|
+
fsPromises,
|
|
1592
|
+
getServerId: async () => TEST_SERVER_ID,
|
|
1593
|
+
fetchImpl: matchingFetchImpl,
|
|
1594
|
+
logWarn: () => {},
|
|
1595
|
+
});
|
|
1596
|
+
await service.loadConfig();
|
|
1597
|
+
const enabled = await service.setConfig({ enabled: true, mode: 'private', httpsPort: 443 });
|
|
1598
|
+
expect(enabled.state).toBe('active');
|
|
1599
|
+
const disabled = await service.setConfig({ enabled: false });
|
|
1600
|
+
expect(disabled.state).toBe('off');
|
|
1601
|
+
expect(disabled.errorCode).toBeNull();
|
|
1602
|
+
service.dispose();
|
|
1603
|
+
});
|
|
1604
|
+
|
|
1605
|
+
it('a rejected background reconcile is logged and recorded as error/unknown without an unhandled rejection', async () => {
|
|
1606
|
+
const { fsPromises } = memFsFor();
|
|
1607
|
+
const warnings = [];
|
|
1608
|
+
const unhandled = [];
|
|
1609
|
+
const onUnhandled = (reason) => unhandled.push(reason);
|
|
1610
|
+
process.on('unhandledRejection', onUnhandled);
|
|
1611
|
+
let service;
|
|
1612
|
+
try {
|
|
1613
|
+
// Mapping-record writes throw; config persistence still succeeds so
|
|
1614
|
+
// setConfig kicks off reconcile and the failure lands in background.
|
|
1615
|
+
const failingFs = {
|
|
1616
|
+
...fsPromises,
|
|
1617
|
+
writeFile: async (p, c) => {
|
|
1618
|
+
if (String(p).includes('tailscale-mapping')) throw new Error('disk full');
|
|
1619
|
+
return fsPromises.writeFile(p, c);
|
|
1620
|
+
},
|
|
1621
|
+
};
|
|
1622
|
+
service = createTailscaleService({
|
|
1623
|
+
existsSync: () => false,
|
|
1624
|
+
dataDir: '/data',
|
|
1625
|
+
getPort: () => 3000,
|
|
1626
|
+
isUiAuthEnabled: () => true,
|
|
1627
|
+
isUnsafeUnauthenticatedLanAllowed: () => false,
|
|
1628
|
+
configResponseGraceMs: 20,
|
|
1629
|
+
runner: {
|
|
1630
|
+
runTailscale: async (args) => {
|
|
1631
|
+
if (args.includes('serve') && args.includes('status')) {
|
|
1632
|
+
return { ok: true, code: 0, stdout: JSON.stringify({ Web: {} }), stderr: '', spawnError: null, timedOut: false };
|
|
1633
|
+
}
|
|
1634
|
+
return { ok: true, code: 0, stdout: STATUS_JSON, stderr: '', spawnError: null, timedOut: false };
|
|
1635
|
+
},
|
|
1636
|
+
},
|
|
1637
|
+
fsPromises: failingFs,
|
|
1638
|
+
getServerId: async () => TEST_SERVER_ID,
|
|
1639
|
+
fetchImpl: matchingFetchImpl,
|
|
1640
|
+
logWarn: (message) => warnings.push(String(message)),
|
|
1641
|
+
});
|
|
1642
|
+
await service.loadConfig();
|
|
1643
|
+
const early = await service.setConfig({ enabled: true, mode: 'private', httpsPort: 443 });
|
|
1644
|
+
// The mapping-record write fails fast: the background settles inside
|
|
1645
|
+
// the grace, so the early response may already be the final error.
|
|
1646
|
+
expect(['starting', 'needs-approval', 'error']).toContain(early.state);
|
|
1647
|
+
await waitFor(() => service.getStatus().state === 'error');
|
|
1648
|
+
expect(service.getStatus().errorCode).toBe('unknown');
|
|
1649
|
+
expect(warnings.some((message) => /Background reconcile failed/i.test(message))).toBe(true);
|
|
1650
|
+
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
1651
|
+
expect(unhandled).toEqual([]);
|
|
1652
|
+
} finally {
|
|
1653
|
+
process.removeListener('unhandledRejection', onUnhandled);
|
|
1654
|
+
service?.dispose();
|
|
1655
|
+
}
|
|
1656
|
+
});
|
|
1657
|
+
});
|
|
1658
|
+
|
|
1659
|
+
describe('tailscale reconcile serialization (Off/Private overlap)', () => {
|
|
1660
|
+
const mappingPath = '/data/tailscale-mapping.json';
|
|
1661
|
+
const memFsFor = () => {
|
|
1662
|
+
const memFs = new Map();
|
|
1663
|
+
return {
|
|
1664
|
+
memFs,
|
|
1665
|
+
fsPromises: {
|
|
1666
|
+
readFile: async (p) => {
|
|
1667
|
+
if (!memFs.has(p)) { const e = new Error('x'); e.code = 'ENOENT'; throw e; }
|
|
1668
|
+
return memFs.get(p);
|
|
1669
|
+
},
|
|
1670
|
+
writeFile: async (p, c) => { memFs.set(p, String(c)); },
|
|
1671
|
+
mkdir: async () => {},
|
|
1672
|
+
rename: async (a, b) => { memFs.set(b, memFs.get(a)); memFs.delete(a); },
|
|
1673
|
+
unlink: async (p) => { memFs.delete(p); },
|
|
1674
|
+
},
|
|
1675
|
+
};
|
|
1676
|
+
};
|
|
1677
|
+
const okResult = (stdout = '') => ({ ok: true, code: 0, stdout, stderr: '', spawnError: null, timedOut: false });
|
|
1678
|
+
const oursWeb = (httpsPort, localPort) => JSON.stringify({
|
|
1679
|
+
Web: { [`mymachine.tailabc.ts.net:${httpsPort}`]: { Handlers: { '/': `http://127.0.0.1:${localPort}` } } },
|
|
1680
|
+
});
|
|
1681
|
+
const waitFor = async (predicate, timeoutMs = 2000) => {
|
|
1682
|
+
const deadline = Date.now() + timeoutMs;
|
|
1683
|
+
for (;;) {
|
|
1684
|
+
if (predicate()) return;
|
|
1685
|
+
if (Date.now() >= deadline) throw new Error('timed out waiting for background reconcile');
|
|
1686
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
1687
|
+
}
|
|
1688
|
+
};
|
|
1689
|
+
|
|
1690
|
+
it('a superseded Off reconcile never removes the mapping the Private reconcile just verified', async () => {
|
|
1691
|
+
const { memFs, fsPromises } = memFsFor();
|
|
1692
|
+
const removed = [];
|
|
1693
|
+
// Armed after the initial enable: the next serve-status query blocks
|
|
1694
|
+
// until the test releases it, so the Off reconcile is stuck between
|
|
1695
|
+
// "query says ours" and "run off" while Private is requested.
|
|
1696
|
+
let blockServeStatus = false;
|
|
1697
|
+
let serveQueryStartedResolve = null;
|
|
1698
|
+
let releaseServeStatus = null;
|
|
1699
|
+
let serveGate = Promise.resolve();
|
|
1700
|
+
const service = createTailscaleService({
|
|
1701
|
+
existsSync: () => false,
|
|
1702
|
+
dataDir: '/data',
|
|
1703
|
+
getPort: () => 3000,
|
|
1704
|
+
isUiAuthEnabled: () => true,
|
|
1705
|
+
isUnsafeUnauthenticatedLanAllowed: () => false,
|
|
1706
|
+
configResponseGraceMs: 20,
|
|
1707
|
+
runner: {
|
|
1708
|
+
runTailscale: async (args) => {
|
|
1709
|
+
if (args.includes('off')) {
|
|
1710
|
+
removed.push(args);
|
|
1711
|
+
return okResult();
|
|
1712
|
+
}
|
|
1713
|
+
if (args.includes('serve') && args.includes('status')) {
|
|
1714
|
+
if (blockServeStatus) {
|
|
1715
|
+
blockServeStatus = false;
|
|
1716
|
+
serveQueryStartedResolve?.();
|
|
1717
|
+
await serveGate;
|
|
1718
|
+
}
|
|
1719
|
+
return okResult(oursWeb(443, 3000));
|
|
1720
|
+
}
|
|
1721
|
+
return okResult(STATUS_JSON);
|
|
1722
|
+
},
|
|
1723
|
+
},
|
|
1724
|
+
fsPromises,
|
|
1725
|
+
getServerId: async () => TEST_SERVER_ID,
|
|
1726
|
+
fetchImpl: matchingFetchImpl,
|
|
1727
|
+
logWarn: () => {},
|
|
1728
|
+
});
|
|
1729
|
+
await service.loadConfig();
|
|
1730
|
+
try {
|
|
1731
|
+
const enabled = await service.setConfig({ enabled: true, mode: 'private', httpsPort: 443 });
|
|
1732
|
+
expect(enabled.state).toBe('active');
|
|
1733
|
+
expect(memFs.has(mappingPath)).toBe(true);
|
|
1734
|
+
|
|
1735
|
+
let startedResolve;
|
|
1736
|
+
const started = new Promise((resolve) => { startedResolve = resolve; });
|
|
1737
|
+
serveQueryStartedResolve = startedResolve;
|
|
1738
|
+
serveGate = new Promise((resolve) => { releaseServeStatus = resolve; });
|
|
1739
|
+
blockServeStatus = true;
|
|
1740
|
+
// Returns after the short grace while the Off reconcile is still
|
|
1741
|
+
// blocked in its serve-status query.
|
|
1742
|
+
const offResponse = await service.setConfig({ enabled: false });
|
|
1743
|
+
expect(offResponse).toBeDefined();
|
|
1744
|
+
await started;
|
|
1745
|
+
// Re-enable while the Off reconcile is in flight; with serialization
|
|
1746
|
+
// the Private reconcile queues behind it.
|
|
1747
|
+
const onResponse = await service.setConfig({ enabled: true, mode: 'private', httpsPort: 443 });
|
|
1748
|
+
expect(onResponse).toBeDefined();
|
|
1749
|
+
releaseServeStatus();
|
|
1750
|
+
|
|
1751
|
+
await waitFor(() => service.getStatus().state === 'active');
|
|
1752
|
+
const final = service.getStatus();
|
|
1753
|
+
expect(final.state).toBe('active');
|
|
1754
|
+
expect(final.url).toBe('https://mymachine.tailabc.ts.net');
|
|
1755
|
+
// The superseded Off reconcile issued no removal and kept the
|
|
1756
|
+
// record the live Private mapping owns.
|
|
1757
|
+
expect(removed).toEqual([]);
|
|
1758
|
+
expect(memFs.has(mappingPath)).toBe(true);
|
|
1759
|
+
} finally {
|
|
1760
|
+
releaseServeStatus?.();
|
|
1761
|
+
service.dispose();
|
|
1762
|
+
}
|
|
1763
|
+
});
|
|
1764
|
+
|
|
1765
|
+
it('two back-to-back retries never query serve status concurrently and skip the superseded one', async () => {
|
|
1766
|
+
const { fsPromises } = memFsFor();
|
|
1767
|
+
let inFlight = 0;
|
|
1768
|
+
let maxInFlight = 0;
|
|
1769
|
+
let serveStatusCount = 0;
|
|
1770
|
+
const service = createTailscaleService({
|
|
1771
|
+
existsSync: () => false,
|
|
1772
|
+
dataDir: '/data',
|
|
1773
|
+
getPort: () => 3000,
|
|
1774
|
+
isUiAuthEnabled: () => true,
|
|
1775
|
+
isUnsafeUnauthenticatedLanAllowed: () => false,
|
|
1776
|
+
runner: {
|
|
1777
|
+
runTailscale: async (args) => {
|
|
1778
|
+
if (args.includes('serve') && args.includes('status')) {
|
|
1779
|
+
serveStatusCount += 1;
|
|
1780
|
+
inFlight += 1;
|
|
1781
|
+
maxInFlight = Math.max(maxInFlight, inFlight);
|
|
1782
|
+
try {
|
|
1783
|
+
// Wide enough that overlapping reconciles would coincide.
|
|
1784
|
+
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
1785
|
+
return okResult(oursWeb(443, 3000));
|
|
1786
|
+
} finally {
|
|
1787
|
+
inFlight -= 1;
|
|
1788
|
+
}
|
|
1789
|
+
}
|
|
1790
|
+
return okResult(STATUS_JSON);
|
|
1791
|
+
},
|
|
1792
|
+
},
|
|
1793
|
+
fsPromises,
|
|
1794
|
+
getServerId: async () => TEST_SERVER_ID,
|
|
1795
|
+
fetchImpl: matchingFetchImpl,
|
|
1796
|
+
logWarn: () => {},
|
|
1797
|
+
});
|
|
1798
|
+
await service.loadConfig();
|
|
1799
|
+
try {
|
|
1800
|
+
const enabled = await service.setConfig({ enabled: true, mode: 'private', httpsPort: 443 });
|
|
1801
|
+
expect(enabled.state).toBe('active');
|
|
1802
|
+
serveStatusCount = 0;
|
|
1803
|
+
maxInFlight = 0;
|
|
1804
|
+
const [first, second] = await Promise.all([service.retry(), service.retry()]);
|
|
1805
|
+
expect(first).toBeDefined();
|
|
1806
|
+
expect(second).toBeDefined();
|
|
1807
|
+
expect(maxInFlight).toBeLessThanOrEqual(1);
|
|
1808
|
+
// The first retry was superseded while queued and skipped its work;
|
|
1809
|
+
// only the live retry queried serve status.
|
|
1810
|
+
expect(serveStatusCount).toBe(1);
|
|
1811
|
+
expect(service.getStatus().state).toBe('active');
|
|
1812
|
+
} finally {
|
|
1813
|
+
service.dispose();
|
|
1814
|
+
}
|
|
1815
|
+
});
|
|
1816
|
+
|
|
1817
|
+
it('a reconcile whose body throws does not block the next reconcile', async () => {
|
|
1818
|
+
const { memFs, fsPromises } = memFsFor();
|
|
1819
|
+
const warnings = [];
|
|
1820
|
+
let failMappingWrite = true;
|
|
1821
|
+
const flakyFs = {
|
|
1822
|
+
...fsPromises,
|
|
1823
|
+
writeFile: async (p, c) => {
|
|
1824
|
+
if (failMappingWrite && String(p).includes('tailscale-mapping')) throw new Error('disk full');
|
|
1825
|
+
return fsPromises.writeFile(p, c);
|
|
1826
|
+
},
|
|
1827
|
+
};
|
|
1828
|
+
const service = createTailscaleService({
|
|
1829
|
+
existsSync: () => false,
|
|
1830
|
+
dataDir: '/data',
|
|
1831
|
+
getPort: () => 3000,
|
|
1832
|
+
isUiAuthEnabled: () => true,
|
|
1833
|
+
isUnsafeUnauthenticatedLanAllowed: () => false,
|
|
1834
|
+
runner: {
|
|
1835
|
+
runTailscale: async (args) => {
|
|
1836
|
+
if (args.includes('serve') && args.includes('status')) return okResult(JSON.stringify({ Web: {} }));
|
|
1837
|
+
return okResult(STATUS_JSON);
|
|
1838
|
+
},
|
|
1839
|
+
},
|
|
1840
|
+
fsPromises: flakyFs,
|
|
1841
|
+
getServerId: async () => TEST_SERVER_ID,
|
|
1842
|
+
fetchImpl: matchingFetchImpl,
|
|
1843
|
+
logWarn: (message) => warnings.push(String(message)),
|
|
1844
|
+
});
|
|
1845
|
+
await service.loadConfig();
|
|
1846
|
+
try {
|
|
1847
|
+
const failed = await service.setConfig({ enabled: true, mode: 'private', httpsPort: 443 });
|
|
1848
|
+
expect(['starting', 'needs-approval', 'error']).toContain(failed.state);
|
|
1849
|
+
await waitFor(() => service.getStatus().state === 'error');
|
|
1850
|
+
expect(warnings.some((message) => /Background reconcile failed/i.test(message))).toBe(true);
|
|
1851
|
+
// The chain swallowed the failure: the next reconcile still runs.
|
|
1852
|
+
failMappingWrite = false;
|
|
1853
|
+
const retried = await service.retry();
|
|
1854
|
+
expect(retried.state).toBe('active');
|
|
1855
|
+
expect(retried.url).toBe('https://mymachine.tailabc.ts.net');
|
|
1856
|
+
expect(memFs.has(mappingPath)).toBe(true);
|
|
1857
|
+
} finally {
|
|
1858
|
+
service.dispose();
|
|
1859
|
+
}
|
|
1860
|
+
});
|
|
1861
|
+
});
|