@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 165 lines 6.1 KB
Raw
1 import * as DenoFileSystem from "@effect/platform-deno/DenoFileSystem"
2 import { assert, describe, it } from "@effect/vitest"
3 import * as Effect from "effect/Effect"
4 import * as Fiber from "effect/Fiber"
5 import * as FileSystem from "effect/FileSystem"
6 import * as Option from "effect/Option"
7 import * as Stream from "effect/Stream"
8 import { afterEach } from "vitest"
9 import { testLayer } from "../../../effect/test/FileSystem.test-utils.ts"
10
11 describe("FileSystem", () =>
12 testLayer(DenoFileSystem.layer, {
13 accessOnDirectory: false,
14 tempFileScopedRemovesDirectory: false,
15 noFollow: false
16 }))
17
18 describe("stat", () => {
19 it.effect("returns device and inode metadata as bigint", () =>
20 Effect.gen(function*() {
21 const fs = yield* FileSystem.FileSystem
22 const path = yield* fs.makeTempFileScoped()
23 const native = Deno.statSync(path)
24 const info = yield* fs.stat(path)
25
26 assert.strictEqual(info.dev, BigInt(native.dev))
27 assert.deepStrictEqual(info.ino, Option.map(Option.fromNullishOr(native.ino), BigInt))
28 }).pipe(Effect.provide(DenoFileSystem.layer)))
29 })
30
31 describe("watch", () => {
32 it.effect("prefers the caller WatchBackend over the construction backend", () =>
33 Effect.gen(function*() {
34 const fs = yield* FileSystem.FileSystem
35 const root = yield* fs.makeTempDirectoryScoped()
36 const callerEvent: FileSystem.WatchEvent = { _tag: "Create", path: "from-caller" }
37 const event = yield* fs.watch(root).pipe(
38 Stream.runHead,
39 Effect.provideService(FileSystem.WatchBackend, {
40 register: () => Option.some(Stream.make(callerEvent))
41 })
42 )
43 assert.deepStrictEqual(event, Option.some(callerEvent))
44 }).pipe(
45 Effect.provide(DenoFileSystem.layer),
46 Effect.provideService(FileSystem.WatchBackend, {
47 register: () => Option.some(Stream.make({ _tag: "Create" as const, path: "from-construction" }))
48 })
49 ))
50
51 it.live("uses a WatchBackend provided after the layer is built", () =>
52 Effect.gen(function*() {
53 const fs = yield* FileSystem.FileSystem
54 const root = yield* fs.makeTempDirectoryScoped()
55 const backendEvent: FileSystem.WatchEvent = { _tag: "Create", path: "from-backend" }
56 const event = yield* fs.watch(root).pipe(
57 Stream.runHead,
58 Effect.provideService(FileSystem.WatchBackend, {
59 register: () => Option.some(Stream.make(backendEvent))
60 }),
61 Effect.timeout("1 second")
62 )
63 assert.deepStrictEqual(event, Option.some(backendEvent))
64 }).pipe(Effect.provide(DenoFileSystem.layer)))
65 })
66
67 describe("truncate", () => {
68 it.effect.each([1.5, NaN])(
69 "rejects length %s at the call site without changing the file",
70 (length) =>
71 Effect.gen(function*() {
72 const fs = yield* FileSystem.FileSystem
73 const path = yield* fs.makeTempFileScoped()
74 yield* fs.writeFileString(path, "contents")
75 const file = yield* fs.open(path, { flag: "r+" })
76 yield* file.seek(BigInt(4), "start")
77 let threwAtCall = false
78
79 yield* Effect.exit(Effect.suspend(() => {
80 try {
81 return file.truncate(length)
82 } catch (error) {
83 threwAtCall = error instanceof RangeError
84 return Effect.die(error)
85 }
86 }))
87
88 assert.deepStrictEqual({
89 threwAtCall,
90 content: yield* fs.readFileString(path),
91 position: yield* file.seek(BigInt(0), "current")
92 }, {
93 threwAtCall: true,
94 content: "contents",
95 position: BigInt(4)
96 })
97 }).pipe(Effect.provide(DenoFileSystem.layer))
98 )
99 })
100
101 describe.skipIf(Deno.build.os === "windows")("writeFile", () => {
102 it.effect("applies the mode when creating a file", () =>
103 Effect.gen(function*() {
104 const fs = yield* FileSystem.FileSystem
105 const directory = yield* fs.makeTempDirectoryScoped()
106 const path = `${directory}/created`
107
108 yield* fs.writeFileString(path, "content", { mode: 0o600 })
109
110 assert.strictEqual((yield* fs.stat(path)).mode & 0o777, 0o600)
111 }).pipe(Effect.provide(DenoFileSystem.layer)))
112
113 it.effect("preserves the mode of an existing file", () =>
114 Effect.gen(function*() {
115 const fs = yield* FileSystem.FileSystem
116 const path = yield* fs.makeTempFileScoped()
117 yield* fs.chmod(path, 0o640)
118
119 yield* fs.writeFileString(path, "content", { mode: 0o600 })
120
121 assert.strictEqual((yield* fs.stat(path)).mode & 0o777, 0o640)
122 }).pipe(Effect.provide(DenoFileSystem.layer)))
123 })
124
125 describe("File native I/O under interruption", { concurrent: false }, () => {
126 const { read } = Deno.FsFile.prototype
127 afterEach(() => {
128 Deno.FsFile.prototype.read = read
129 })
130
131 it.effect("skips cancelled queued I/O but waits for an interrupted in-flight read", () =>
132 Effect.gen(function*() {
133 const fs = yield* FileSystem.FileSystem
134 const path = yield* fs.makeTempFileScoped()
135 yield* fs.writeFileString(path, "abcdefghij")
136 const file = yield* fs.open(path, { flag: "r+" })
137 const started = Promise.withResolvers<void>()
138 const released = Promise.withResolvers<void>()
139 let calls = 0
140 Deno.FsFile.prototype.read = async function(p) {
141 if (calls++ === 0) {
142 started.resolve()
143 await released.promise
144 }
145 return read.call(this, p)
146 }
147
148 const first = yield* Effect.forkChild(file.read(new Uint8Array(5), { position: BigInt(0) }))
149 yield* Effect.promise(() => started.promise)
150 const queued = yield* Effect.forkChild(file.write(new TextEncoder().encode("XYZ")), {
151 startImmediately: true
152 })
153 yield* Fiber.interrupt(queued)
154 yield* Fiber.interrupt(first)
155 const next = yield* Effect.forkChild(
156 file.readAlloc(5, { position: BigInt(5) }).pipe(Effect.flatMap(Effect.fromOption)),
157 { startImmediately: true }
158 )
159 const readsBeforeRelease = calls
160 released.resolve()
161 assert.strictEqual(readsBeforeRelease, 1)
162 assert.strictEqual(new TextDecoder().decode(yield* Fiber.join(next)), "fghij")
163 assert.strictEqual(yield* fs.readFileString(path), "abcdefghij")
164 }).pipe(Effect.provide(DenoFileSystem.layer)))
165 })