11import { assert , describe , it } from "@effect/vitest" ;
22import * as Effect from "effect/Effect" ;
33import * as Layer from "effect/Layer" ;
4- import { HttpClient } from "effect/http" ;
4+ import { HttpClient , HttpClientResponse } from "effect/http" ;
55import { ChildProcessSpawner } from "effect/process" ;
66
77import { resolveTailscaleAdvertisedEndpoints } from "./tailscaleEndpointProvider.ts" ;
@@ -22,11 +22,55 @@ const protonAndTailscaleInterfaces = {
2222 tailscale0 : [ { address : "100.74.126.34" , family : "IPv4" , internal : false } ] ,
2323} ;
2424
25+ /** The local backend reports `this-desktop`; the tailnet URL reports `servedEnvironmentId`. */
26+ const layerServeAnswers = ( servedEnvironmentId : string ) =>
27+ Layer . mergeAll (
28+ Layer . succeed (
29+ HttpClient . HttpClient ,
30+ HttpClient . make ( ( request ) => {
31+ const environmentId = request . url . startsWith ( "http://127.0.0.1:3773/" )
32+ ? "this-desktop"
33+ : servedEnvironmentId ;
34+ return Effect . succeed (
35+ HttpClientResponse . fromWeb ( request , new Response ( JSON . stringify ( { environmentId } ) ) ) ,
36+ ) ;
37+ } ) ,
38+ ) ,
39+ Layer . succeed (
40+ ChildProcessSpawner . ChildProcessSpawner ,
41+ ChildProcessSpawner . make ( ( ) => Effect . die ( "unexpected tailscale status process" ) ) ,
42+ ) ,
43+ ) ;
44+
45+ const resolveServeEndpointStatus = ( servedEnvironmentId : string ) =>
46+ resolveTailscaleAdvertisedEndpoints ( {
47+ port : 3773 ,
48+ serveEnabled : true ,
49+ localHttpUrl : "http://127.0.0.1:3773" ,
50+ networkInterfaces : { } ,
51+ statusJson : `{"Self":{"DNSName":"desktop.tail.ts.net.","TailscaleIPs":[]}}` ,
52+ } ) . pipe (
53+ Effect . map ( ( endpoints ) => endpoints . map ( ( endpoint ) => [ endpoint . httpBaseUrl , endpoint . status ] ) ) ,
54+ Effect . provide ( layerServeAnswers ( servedEnvironmentId ) ) ,
55+ ) ;
56+
2557describe ( "tailscale endpoint provider" , ( ) => {
58+ it . effect ( "lists Tailscale HTTPS as available only while it reaches this desktop's server" , ( ) =>
59+ Effect . gen ( function * ( ) {
60+ assert . deepEqual ( yield * resolveServeEndpointStatus ( "this-desktop" ) , [
61+ [ "https://desktop.tail.ts.net/" , "available" ] ,
62+ ] ) ;
63+ assert . deepEqual ( yield * resolveServeEndpointStatus ( "other-desktop" ) , [
64+ [ "https://desktop.tail.ts.net/" , "unavailable" ] ,
65+ ] ) ;
66+ } ) ,
67+ ) ;
68+
2669 it . effect ( "does not advertise Proton's CGNAT address as a Tailscale pairing endpoint" , ( ) =>
2770 Effect . gen ( function * ( ) {
2871 const endpoints = yield * resolveTailscaleAdvertisedEndpoints ( {
2972 port : 3773 ,
73+ localHttpUrl : "http://127.0.0.1:3773" ,
3074 networkInterfaces : protonAndTailscaleInterfaces ,
3175 statusJson : `{"Self":{"TailscaleIPs":["100.74.126.34"]}}` ,
3276 } ) ;
@@ -43,6 +87,7 @@ describe("tailscale endpoint provider", () => {
4387 for ( const statusJson of [ null , "not-json" , `{"Self":{"TailscaleIPs":[]}}` ] ) {
4488 const endpoints = yield * resolveTailscaleAdvertisedEndpoints ( {
4589 port : 3773 ,
90+ localHttpUrl : "http://127.0.0.1:3773" ,
4691 networkInterfaces : {
4792 pvpnksintrf1 : protonAndTailscaleInterfaces . pvpnksintrf1 ,
4893 [ interfaceName ] : protonAndTailscaleInterfaces . tailscale0 ,
@@ -64,6 +109,7 @@ describe("tailscale endpoint provider", () => {
64109 Effect . gen ( function * ( ) {
65110 const endpoints = yield * resolveTailscaleAdvertisedEndpoints ( {
66111 port : 3773 ,
112+ localHttpUrl : "http://127.0.0.1:3773" ,
67113 networkInterfaces : {
68114 utun3 : [ { address : "100.85.0.1" , family : "IPv4" , internal : false } ] ,
69115 utun4 : [
0 commit comments