| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { spawn } from "node:child_process"; |
| 4 | import { createTailnetClient } from "./tailnet-client.mjs"; |
| 5 | |
| 6 | test("stdio requests are serialized, preserve component errors and reject wire commands", async () => { |
| 7 | const seen = []; |
| 8 | const fixture = `let text=''; process.stdin.on('data',chunk=>{text+=chunk;let end;while((end=text.indexOf('\\n'))>=0){const input=JSON.parse(text.slice(0,end));text=text.slice(end+1);setTimeout(()=>console.log(JSON.stringify(input.action==='add-key'?{error:'请粘贴有效 SSH 公钥'}:{installed:true,action:input.action})),10)}});`; |
| 9 | const client = createTailnetClient({ executable: process.execPath, start: (file,args,options) => { seen.push({file,args}); return spawn(process.execPath, ["-e", fixture], options); } }); |
| 10 | try { |
| 11 | const values = await Promise.all([client.request({action:"status"}), client.request({action:"connect"}), client.request({action:"add-key",key:"invalid"})]); |
| 12 | assert.deepEqual(values.map(v=>v.action||v.tailnet_error), ["status","connect","请粘贴有效 SSH 公钥"]); |
| 13 | assert.equal(seen.length,1); |
| 14 | await assert.rejects(client.request({action:"shell",command:"whoami"})); |
| 15 | await assert.rejects(client.request({action:"add-key",key:"x".repeat(8193)})); |
| 16 | assert.equal((await client.request({action:"status"})).installed,true); |
| 17 | } finally {client.close();} |
| 18 | }); |
| 19 | test("missing guest payload offers installation without spawning a host VPN", async()=>{ |
| 20 | const client=createTailnetClient({executable:"/nonexistent-cage-tailnet",start:()=>{throw Error("must not spawn")}}); |
| 21 | assert.equal((await client.request({action:"status"})).installed,false); |
| 22 | }); |
| 23 | test("service destinations are validated and forwarded through the same private protocol", async () => { |
| 24 | const client = createTailnetClient({ executable: process.execPath, start: (_file,_args,options) => spawn(process.execPath, ["-e", `let text='';process.stdin.on('data',chunk=>{text+=chunk;let end;while((end=text.indexOf('\\n'))>=0){const input=JSON.parse(text.slice(0,end));text=text.slice(end+1);console.log(JSON.stringify(input))}});`], options) }); |
| 25 | try { |
| 26 | const result = await client.request({action:"open-service",host:"100.88.2.4",port:8080,command:"ignored"}); |
| 27 | assert.deepEqual(result,{action:"open-service",host:"100.88.2.4",port:8080}); |
| 28 | for (const input of [{host:"100.88.2.4",port:0},{host:"100.88.2.4",port:65536},{host:"100.88.2.4",port:"80"},{host:"100.88.2.4\ncommand",port:80}]) await assert.rejects(client.request({action:"open-service",...input})); |
| 29 | assert.equal((await client.request({action:"status"})).action,"status"); |
| 30 | } finally {client.close();} |
| 31 | }); |