11import { describe , expect , it } from "@effect/vitest" ;
22import * as Effect from "effect/Effect" ;
3+ import * as Deferred from "effect/Deferred" ;
4+ import * as Fiber from "effect/Fiber" ;
35import * as FileSystem from "effect/FileSystem" ;
46import * as NodeServices from "@effect/platform-node/NodeServices" ;
57import { PGlite } from "@electric-sql/pglite" ;
@@ -56,14 +58,14 @@ describe("EnvironmentLinks", () => {
5658 expect ( query . sql ) . toContain ( '"relay_environment_links"."environment_id" = $2' ) ;
5759 expect ( query . sql ) . toContain ( '"relay_environment_links"."environment_public_key" = $3' ) ;
5860 expect ( query . sql ) . toContain ( '"relay_environment_links"."revoked_at" is null' ) ;
59- expect ( query . sql ) . toContain ( '"relay_environment_links"."updated_at" <= $4' ) ;
60- expect ( query . sql ) . toContain ( '"relay_environment_links"."label_updated_at" <= $5' ) ;
61+ expect ( query . sql ) . toContain (
62+ 'coalesce("relay_environment_links"."label_updated_at", "relay_environment_links"."updated_at") <= $4' ,
63+ ) ;
6164 expect ( query . params ) . toEqual ( [
6265 "user-1" ,
6366 "env-1" ,
6467 "verified-public-key" ,
6568 "2026-08-20T12:00:00.000Z" ,
66- "2026-08-20T12:00:00.000Z" ,
6769 ] ) ;
6870 } ) . pipe (
6971 Effect . provide (
@@ -97,13 +99,10 @@ describe("EnvironmentLinks", () => {
9799 const query = new PgDialect ( ) . sqlToQuery ( whereConditions [ 0 ] as never ) ;
98100 expect ( query . sql ) . not . toContain ( "user_id" ) ;
99101 expect ( query . sql ) . toContain ( '"relay_environment_links"."environment_public_key" = $2' ) ;
100- expect ( query . sql ) . toContain ( '"relay_environment_links"."updated_at" <= $3' ) ;
101- expect ( query . params ) . toEqual ( [
102- "env-1" ,
103- "verified-public-key" ,
104- "1970-01-01T00:00:00.000Z" ,
105- "1970-01-01T00:00:00.000Z" ,
106- ] ) ;
102+ expect ( query . sql ) . toContain (
103+ 'coalesce("relay_environment_links"."label_updated_at", "relay_environment_links"."updated_at") <= $3' ,
104+ ) ;
105+ expect ( query . params ) . toEqual ( [ "env-1" , "verified-public-key" , "1970-01-01T00:00:00.000Z" ] ) ;
107106 } ) . pipe (
108107 Effect . provide (
109108 EnvironmentLinks . layer . pipe ( Layer . provide ( Layer . succeed ( RelayDb . RelayDb , fakeDb ) ) ) ,
@@ -148,6 +147,9 @@ describe("EnvironmentLinks", () => {
148147 ) ;
149148 const database = drizzle ( { client : pg } ) ;
150149 const insertion = database . insert ( relayEnvironmentLinks ) ;
150+ const updating = database . update ( relayEnvironmentLinks ) ;
151+ const pushStarted = yield * Deferred . make < void > ( ) ;
152+ const releasePush = yield * Deferred . make < void > ( ) ;
151153 const db = {
152154 insert : ( ) => ( {
153155 values : ( values : Parameters < typeof insertion . values > [ 0 ] ) => ( {
@@ -163,15 +165,17 @@ describe("EnvironmentLinks", () => {
163165 } ) ,
164166 } ) ,
165167 update : ( ) => ( {
166- set : ( values : Parameters < ReturnType < typeof database . update > [ "set" ] > [ 0 ] ) => ( {
167- where : (
168- condition : Parameters <
169- ReturnType < ReturnType < typeof database . update > [ "set" ] > [ "where" ]
170- > [ 0 ] ,
171- ) =>
172- Effect . promise ( ( ) =>
173- database . update ( relayEnvironmentLinks ) . set ( values ) . where ( condition ) ,
174- ) ,
168+ set : ( values : Parameters < typeof updating . set > [ 0 ] ) => ( {
169+ where : ( condition : Parameters < ReturnType < typeof updating . set > [ "where" ] > [ 0 ] ) =>
170+ Effect . gen ( function * ( ) {
171+ if ( values . environmentLabel === "In-flight name" ) {
172+ yield * Deferred . succeed ( pushStarted , undefined ) ;
173+ yield * Deferred . await ( releasePush ) ;
174+ }
175+ return yield * Effect . promise ( ( ) =>
176+ database . update ( relayEnvironmentLinks ) . set ( values ) . where ( condition ) ,
177+ ) ;
178+ } ) ,
175179 } ) ,
176180 } ) ,
177181 } as unknown as RelayDb . RelayDb [ "Service" ] ;
@@ -216,8 +220,8 @@ describe("EnvironmentLinks", () => {
216220 yield * TestClock . setTime ( Date . parse ( at ( 40 ) ) ) ;
217221 for ( const [ challengeAt , key , proofLabel , expectedLabel , expectedWatermark ] of [
218222 [ 20 , "key" , "Stale link proof" , "Latest name" , 30 ] ,
219- [ 35 , "key" , "Fresh link proof" , "Fresh link proof" , 40 ] ,
220- [ 15 , "replacement-key" , "Replacement host" , "Replacement host" , 40 ] ,
223+ [ 35 , "key" , "Fresh link proof" , "Fresh link proof" , 35 ] ,
224+ [ 15 , "replacement-key" , "Replacement host" , "Replacement host" , 15 ] ,
221225 ] as const ) {
222226 yield * links . upsert ( {
223227 userId : "user-1" ,
@@ -249,6 +253,53 @@ describe("EnvironmentLinks", () => {
249253 updatedAt : at ( 40 ) ,
250254 } ) ;
251255 }
256+ yield * Effect . promise ( ( ) =>
257+ database . update ( relayEnvironmentLinks ) . set ( {
258+ environmentPublicKey : "key" ,
259+ environmentLabel : "Before rename" ,
260+ labelUpdatedAt : at ( 41 ) ,
261+ updatedAt : at ( 41 ) ,
262+ } ) ,
263+ ) ;
264+ yield * TestClock . setTime ( Date . parse ( at ( 45 ) ) ) ;
265+ const inFlightPush = yield * links
266+ . recordEnvironmentLabel ( {
267+ environmentId : "env-1" ,
268+ environmentPublicKey : "key" ,
269+ label : "In-flight name" ,
270+ } )
271+ . pipe ( Effect . forkChild ) ;
272+ yield * Deferred . await ( pushStarted ) ;
273+ yield * TestClock . setTime ( Date . parse ( at ( 50 ) ) ) ;
274+ yield * links . upsert ( {
275+ userId : "user-1" ,
276+ challengeIssuedAt : at ( 42 ) ,
277+ request : {
278+ notificationsEnabled : false ,
279+ liveActivitiesEnabled : false ,
280+ managedTunnelsEnabled : false ,
281+ } as never ,
282+ proof : {
283+ environmentId : "env-1" ,
284+ environmentPublicKey : "key" ,
285+ descriptor : { label : "Old proof" } ,
286+ } as never ,
287+ endpoint : {
288+ httpBaseUrl : "https://e" ,
289+ wsBaseUrl : "wss://e" ,
290+ providerKind : "direct" ,
291+ } as never ,
292+ } ) ;
293+ yield * Deferred . succeed ( releasePush , undefined ) ;
294+ yield * Fiber . join ( inFlightPush ) ;
295+ const [ afterRace ] = yield * Effect . promise ( ( ) =>
296+ database . select ( ) . from ( relayEnvironmentLinks ) ,
297+ ) ;
298+ expect ( afterRace ) . toMatchObject ( {
299+ environmentLabel : "In-flight name" ,
300+ labelUpdatedAt : at ( 45 ) ,
301+ updatedAt : at ( 50 ) ,
302+ } ) ;
252303 } ) . pipe ( Effect . provide ( NodeServices . layer ) ) ,
253304 ) ;
254305
0 commit comments