Open History to see the last commit
1import { describe, expect, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4
5import * as RelayDb from "../db.ts";
6import { relayManagedEndpointAllocations } from "../persistence/schema.ts";
7import * as ManagedEndpointAllocations from "./ManagedEndpointAllocations.ts";
8
9const layerWithDb = (db: RelayDb.RelayDb["Service"]) =>
10 ManagedEndpointAllocations.layer.pipe(Layer.provide(Layer.succeed(RelayDb.RelayDb, db)));
11
12describe("ManagedEndpointAllocations", () => {
13 it.effect("returns a claim generation only when deprovision wins the allocation CAS", () => {
14 let claimedAt: string | undefined;
15 const fakeDb = {
16 update: (table: unknown) => {
17 expect(table).toBe(relayManagedEndpointAllocations);
18 return {
19 set: (values: { readonly updatedAt: string }) => {
20 claimedAt = values.updatedAt;
21 return {
22 where: () => ({
23 returning: () => Effect.succeed([{ userId: "user-1" }]),
24 }),
25 };
26 },
27 };
28 },
29 } as unknown as RelayDb.RelayDb["Service"];
30
31 return Effect.gen(function* () {
32 const allocations = yield* ManagedEndpointAllocations.ManagedEndpointAllocations;
33 const generation = yield* allocations.claimDeprovision({
34 userId: "user-1",
35 environmentId: "environment-1",
36 updatedAt: "captured-generation",
37 });
38
39 expect(generation).toBe(claimedAt);
40 expect(generation).not.toBeNull();
41 }).pipe(Effect.provide(layerWithDb(fakeDb)));
42 });
43
44 it.effect("does not remove an allocation superseded after a deprovision claim", () => {
45 const fakeDb = {
46 delete: (table: unknown) => {
47 expect(table).toBe(relayManagedEndpointAllocations);
48 return {
49 where: () => ({
50 returning: () => Effect.succeed([]),
51 }),
52 };
53 },
54 } as unknown as RelayDb.RelayDb["Service"];
55
56 return Effect.gen(function* () {
57 const allocations = yield* ManagedEndpointAllocations.ManagedEndpointAllocations;
58 expect(
59 yield* allocations.removeClaimed({
60 userId: "user-1",
61 environmentId: "environment-1",
62 updatedAt: "outdated-claim-generation",
63 }),
64 ).toBe(false);
65 }).pipe(Effect.provide(layerWithDb(fakeDb)));
66 });
67
68 it.effect("retains database failures with allocation operation and identity", () => {
69 const cause = new Error("database unavailable");
70 const fakeDb = {
71 select: () => ({
72 from: (table: unknown) => {
73 expect(table).toBe(relayManagedEndpointAllocations);
74 return {
75 where: () => ({
76 limit: () => Effect.fail(cause),
77 }),
78 };
79 },
80 }),
81 } as unknown as RelayDb.RelayDb["Service"];
82
83 return Effect.gen(function* () {
84 const allocations = yield* ManagedEndpointAllocations.ManagedEndpointAllocations;
85 const error = yield* Effect.flip(
86 allocations.get({ userId: "user-1", environmentId: "environment-1" }),
87 );
88
89 expect(error).toMatchObject({
90 _tag: "ManagedEndpointAllocationPersistenceError",
91 operation: "get",
92 stage: "database-request",
93 userId: "user-1",
94 environmentId: "environment-1",
95 });
96 expect(error.cause).toBe(cause);
97 }).pipe(Effect.provide(layerWithDb(fakeDb)));
98 });
99
100 it.effect("reports an unresolved reservation without manufacturing a cause", () => {
101 const fakeDb = {
102 insert: (table: unknown) => {
103 expect(table).toBe(relayManagedEndpointAllocations);
104 return {
105 values: () => ({
106 onConflictDoNothing: () => ({
107 returning: () => Effect.succeed([]),
108 }),
109 }),
110 };
111 },
112 select: () => ({
113 from: (table: unknown) => {
114 expect(table).toBe(relayManagedEndpointAllocations);
115 return {
116 where: () => ({
117 limit: () => Effect.succeed([]),
118 }),
119 };
120 },
121 }),
122 } as unknown as RelayDb.RelayDb["Service"];
123
124 return Effect.gen(function* () {
125 const allocations = yield* ManagedEndpointAllocations.ManagedEndpointAllocations;
126 const error = yield* Effect.flip(
127 allocations.reserve({
128 userId: "user-1",
129 environmentId: "environment-1",
130 hostname: "environment-1.example.test",
131 tunnelName: "environment-1-tunnel",
132 }),
133 );
134
135 expect(error).toMatchObject({
136 _tag: "ManagedEndpointAllocationPersistenceError",
137 operation: "reserve",
138 stage: "resolve-reservation",
139 userId: "user-1",
140 environmentId: "environment-1",
141 hostname: "environment-1.example.test",
142 tunnelName: "environment-1-tunnel",
143 });
144 expect(error.cause).toBeUndefined();
145 expect(error.message).toContain("'resolve-reservation'");
146 }).pipe(Effect.provide(layerWithDb(fakeDb)));
147 });
148});