|
| 1 | +import * as NodeServices from "@effect/platform-node/NodeServices"; |
1 | 2 | import { assert, describe, it } from "@effect/vitest"; |
| 3 | +import { EnvironmentId, ProjectId } from "@t3tools/contracts"; |
2 | 4 | import * as Effect from "effect/Effect"; |
| 5 | +import * as FileSystem from "effect/FileSystem"; |
| 6 | +import * as Layer from "effect/Layer"; |
3 | 7 | import * as Option from "effect/Option"; |
4 | 8 |
|
5 | 9 | import * as HyprlandPlacement from "./HyprlandPlacement.ts"; |
6 | 10 | import type { WindowId } from "./WindowId.fork.ts"; |
7 | 11 | import { windowClaimTitle } from "./WindowPlacement.fork.ts"; |
| 12 | +import { |
| 13 | + type WindowProjectManifest, |
| 14 | + windowProjectManifestPath, |
| 15 | +} from "./WindowProjectManifest.fork.ts"; |
8 | 16 |
|
9 | 17 | const PID = 4242; |
10 | 18 | const first = "00000000-0000-4000-8000-000000000001" as WindowId; |
@@ -60,8 +68,9 @@ describe("HyprlandPlacement", () => { |
60 | 68 | }); |
61 | 69 | const knownAddresses = new Set<string>(); |
62 | 70 |
|
63 | | - yield* Effect.all( |
64 | | - mapped.map(({ windowId, title }) => placement.claim(windowId, title, { knownAddresses })), |
| 71 | + yield* Effect.forEach( |
| 72 | + mapped, |
| 73 | + ({ windowId, title }) => placement.claim(windowId, title, { knownAddresses }), |
65 | 74 | { concurrency: "unbounded" }, |
66 | 75 | ); |
67 | 76 |
|
@@ -148,4 +157,104 @@ describe("HyprlandPlacement", () => { |
148 | 157 | assert.isTrue(Option.isNone(yield* placement.workspaceOf(first))); |
149 | 158 | }), |
150 | 159 | ); |
| 160 | + |
| 161 | + it.effect("writes each window's address and published scope, and drops it on forget", () => |
| 162 | + Effect.gen(function* () { |
| 163 | + const writes: WindowProjectManifest[] = []; |
| 164 | + const placement = yield* HyprlandPlacement.make({ |
| 165 | + ...options, |
| 166 | + manifestPath: "/manifest.json", |
| 167 | + writeManifest: (path, manifest) => |
| 168 | + Effect.sync(() => { |
| 169 | + assert.strictEqual(path, "/manifest.json"); |
| 170 | + writes.push(manifest); |
| 171 | + }), |
| 172 | + requestHyprland: listClients(() => [client("0xaaa", "T3 Code")]), |
| 173 | + }); |
| 174 | + const scope = { |
| 175 | + kind: "projects", |
| 176 | + projects: [ |
| 177 | + { |
| 178 | + environmentId: EnvironmentId.make("env"), |
| 179 | + projectId: ProjectId.make("web"), |
| 180 | + workspaceRoot: "/src/web", |
| 181 | + }, |
| 182 | + ], |
| 183 | + } as const; |
| 184 | + |
| 185 | + yield* placement.claim(first, "T3 Code"); |
| 186 | + yield* placement.publishScope(first, scope); |
| 187 | + yield* placement.publishScope(second, { kind: "all" }); |
| 188 | + yield* placement.forget(first); |
| 189 | + |
| 190 | + assert.deepEqual( |
| 191 | + writes.map((manifest) => manifest.windows), |
| 192 | + [ |
| 193 | + [{ windowId: first, address: "0xaaa", scope: null }], |
| 194 | + [{ windowId: first, address: "0xaaa", scope }], |
| 195 | + [ |
| 196 | + { windowId: first, address: "0xaaa", scope }, |
| 197 | + { windowId: second, address: null, scope: { kind: "all" } }, |
| 198 | + ], |
| 199 | + [{ windowId: second, address: null, scope: { kind: "all" } }], |
| 200 | + ], |
| 201 | + ); |
| 202 | + assert.isTrue(writes.every((manifest) => manifest.pid === PID && manifest.version === 1)); |
| 203 | + }), |
| 204 | + ); |
| 205 | + |
| 206 | + it.effect("writes no manifest off Hyprland or without a path", () => |
| 207 | + Effect.gen(function* () { |
| 208 | + const writes: unknown[] = []; |
| 209 | + const record = (_path: string, manifest: WindowProjectManifest) => |
| 210 | + Effect.sync(() => void writes.push(manifest)); |
| 211 | + const offHyprland = yield* HyprlandPlacement.make({ |
| 212 | + ...options, |
| 213 | + environment: { instanceSignature: undefined, runtimeDirectory: "/run/user/1000" }, |
| 214 | + manifestPath: "/manifest.json", |
| 215 | + writeManifest: record, |
| 216 | + }); |
| 217 | + const pathless = yield* HyprlandPlacement.make({ ...options, writeManifest: record }); |
| 218 | + |
| 219 | + yield* offHyprland.publishScope(first, { kind: "all" }); |
| 220 | + yield* pathless.publishScope(first, { kind: "all" }); |
| 221 | + |
| 222 | + assert.deepEqual(writes, []); |
| 223 | + }), |
| 224 | + ); |
| 225 | + |
| 226 | + it.effect("removes its manifest on close and writes none after", () => |
| 227 | + Effect.gen(function* () { |
| 228 | + const fileSystem = yield* FileSystem.FileSystem; |
| 229 | + const root = yield* fileSystem.makeTempDirectoryScoped({ prefix: "placement-layer-" }); |
| 230 | + const keys = ["HYPRLAND_INSTANCE_SIGNATURE", "XDG_RUNTIME_DIR"] as const; |
| 231 | + yield* Effect.acquireRelease( |
| 232 | + Effect.sync(() => { |
| 233 | + const saved = keys.map((key) => [key, process.env[key]] as const); |
| 234 | + process.env.HYPRLAND_INSTANCE_SIGNATURE = "test"; |
| 235 | + process.env.XDG_RUNTIME_DIR = root; |
| 236 | + return saved; |
| 237 | + }), |
| 238 | + (saved) => |
| 239 | + Effect.sync(() => { |
| 240 | + for (const [key, value] of saved) { |
| 241 | + if (value === undefined) delete process.env[key]; |
| 242 | + else process.env[key] = value; |
| 243 | + } |
| 244 | + }), |
| 245 | + ); |
| 246 | + const path = windowProjectManifestPath(root, process.pid)!; |
| 247 | + |
| 248 | + const placement = yield* Effect.gen(function* () { |
| 249 | + const open = yield* HyprlandPlacement.HyprlandPlacement; |
| 250 | + yield* open.publishScope(first, { kind: "all" }); |
| 251 | + assert.isTrue(yield* fileSystem.exists(path)); |
| 252 | + return open; |
| 253 | + }).pipe(Effect.provide(HyprlandPlacement.layer.pipe(Layer.provide(NodeServices.layer)))); |
| 254 | + |
| 255 | + assert.isFalse(yield* fileSystem.exists(path)); |
| 256 | + yield* placement.publishScope(second, { kind: "all" }); |
| 257 | + assert.isFalse(yield* fileSystem.exists(path)); |
| 258 | + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), |
| 259 | + ); |
151 | 260 | }); |
0 commit comments