Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

lstat.test.ts

216 lines7.5 KB

1import { describe, expect, it } from 'vitest'
2import Docker from 'dockerode'
3import { DockerHandle } from '../src/handle'
4​
5type ExecResult = { exitCode: number; stdout: string; stderr: string }
6function lstatCommand(path: string): string {
7 const quoted = `'${path.replace(/'/g, `'\\''`)}'`
8 return `tanstack_lstat_path=${quoted}; tanstack_lstat_output=$(stat -c '%f:%s' -- "$tanstack_lstat_path" 2>&1); tanstack_lstat_status=$?; if [ "$tanstack_lstat_status" -eq 0 ]; then printf '%s\n' "$tanstack_lstat_output"; else tanstack_lstat_missing() { tanstack_missing_path=$1; case "$tanstack_missing_path" in /|.) return 1 ;; */*) tanstack_parent=${'$'}{tanstack_missing_path%/*}; tanstack_name=${'$'}{tanstack_missing_path##*/}; [ -n "$tanstack_parent" ] || tanstack_parent=/ ;; *) tanstack_parent=.; tanstack_name=$tanstack_missing_path ;; esac; tanstack_parent_mode=$(stat -L -c '%f' -- "$tanstack_parent" 2>/dev/null); tanstack_parent_status=$?; if [ "$tanstack_parent_status" -ne 0 ]; then tanstack_lstat_missing "$tanstack_parent"; else case "$tanstack_parent_mode" in 4[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]) case "$tanstack_parent" in /*) tanstack_find_parent=$tanstack_parent ;; *) tanstack_find_parent=./$tanstack_parent ;; esac; tanstack_match=$(find -H "$tanstack_find_parent" -mindepth 1 -maxdepth 1 -exec sh -c 'tanstack_target=$1; shift; for tanstack_candidate do [ "${'$'}{tanstack_candidate##*/}" = "$tanstack_target" ] && { printf 1; exit 0; }; done; exit 0' sh "$tanstack_name" '{}' + 2>/dev/null); tanstack_find_status=$?; [ "$tanstack_find_status" -eq 0 ] && [ -z "$tanstack_match" ] ;; *) return 1 ;; esac; fi; }; if tanstack_lstat_missing "$tanstack_lstat_path"; then printf '%s' '__TANSTACK_LSTAT_MISSING__'; else printf '%s\n' "$tanstack_lstat_output" >&2; exit "$tanstack_lstat_status"; fi; fi`
9}
10function lstatPath(command: string): string {
11 return /^tanstack_lstat_path='([^']*)';/.exec(command)?.[1] ?? ''
13​
14function setExec(
15 handle: object,
16 exec: (command: string) => Promise<ExecResult>,
17) {
18 Object.defineProperty(handle, 'exec', { configurable: true, value: exec })
20​
21function createHandle(): DockerHandle {
22 const docker = new Docker()
23 let handle: DockerHandle
24 handle = new DockerHandle({
25 docker,
26 container: docker.getContainer('test'),
27 workdir: '/workspace',
28 forkFactory: () => Promise.resolve(handle),
29 removeOnDestroy: false,
30 })
31 return handle
33​
34describe('DockerHandle.fs.lstat', () => {
35 it.each([
36 '/workspace/missing',
37 '/workspace/missing-parent/child',
38 '-H/missing',
39 '-delete/missing',
40 ])('returns undefined for a verified missing path: %s', async (path) => {
41 const handle = createHandle()
42 setExec(handle, async (command) => {
43 expect(command).toBe(lstatCommand(path))
44 return {
45 exitCode: 0,
46 stdout: '__TANSTACK_LSTAT_MISSING__',
47 stderr: '',
48 }
49 })
50 await expect(handle.fs.lstat!(path)).resolves.toBeUndefined()
51 })
52​
53 it('treats a transport-padded missing sentinel as missing', async () => {
54 const handle = createHandle()
55 setExec(handle, async () => ({
56 exitCode: 0,
57 stdout: '__TANSTACK_LSTAT_MISSING__\n',
58 stderr: '',
59 }))
60 await expect(
61 handle.fs.lstat!('/workspace/missing'),
62 ).resolves.toBeUndefined()
63 })
64​
65 it.each([
66 '/workspace/file/child',
67 '/workspace/loop/child',
68 '/workspace/denied-link/child',
69 ])('preserves an unverified parent failure: %s', async (path) => {
70 const handle = createHandle()
71 setExec(handle, async (command) => {
72 expect(command).toBe(lstatCommand(path))
73 return { exitCode: 1, stdout: '', stderr: 'permission denied' }
74 })
75 await expect(handle.fs.lstat!(path)).rejects.toThrow('permission denied')
76 })
77​
78 it('parses file, directory, symlink, and other metadata', async () => {
79 const values = new Map<string, string>([
80 ['file', '81A4:12\n'],
81 ['dir', '41ed:4096\n'],
82 ['link', 'a1ff:4\n'],
83 ['other', 'c1b6:0\n'],
84 ['char', '21b6:0\n'],
85 ['block', '61b6:0\n'],
86 ['fifo', '11b6:0\n'],
87 ['unknown', '71b6:7\n'],
88 ])
89 const handle = createHandle()
90 setExec(handle, async (command: string) => {
91 const path = lstatPath(command)
92 expect(command).toBe(lstatCommand(path))
93 return {
94 exitCode: 0,
95 stdout: values.get(lstatPath(command).split('/').pop() ?? '') ?? '',
96 stderr: '',
97 }
98 })
99 await expect(handle.fs.lstat!('/workspace/file')).resolves.toEqual({
100 type: 'file',
101 mode: 0x81a4,
102 size: 12,
103 })
104 await expect(handle.fs.lstat!('/workspace/dir')).resolves.toEqual({
105 type: 'dir',
106 mode: 0x41ed,
107 })
108 await expect(handle.fs.lstat!('/workspace/link')).resolves.toEqual({
109 type: 'symlink',
110 mode: 0xa1ff,
111 })
112 await expect(handle.fs.lstat!('/workspace/other')).resolves.toEqual({
113 type: 'other',
114 mode: 0xc1b6,
115 })
116 })
117 it('parses a character special file as other without size', async () => {
118 const handle = createHandle()
119 setExec(handle, async () => ({
120 exitCode: 0,
121 stdout: '21b6:0\n',
122 stderr: '',
123 }))
124 await expect(handle.fs.lstat!('/workspace/char')).resolves.toEqual({
125 type: 'other',
126 mode: 0x21b6,
127 })
128 })
129 it('parses a zero-byte regular empty file with size zero', async () => {
130 const handle = createHandle()
131 setExec(handle, async () => ({
132 exitCode: 0,
133 stdout: '81a4:0\n',
134 stderr: '',
135 }))
136 await expect(handle.fs.lstat!('/workspace/empty')).resolves.toEqual({
137 type: 'file',
138 mode: 0x81a4,
139 size: 0,
140 })
141 })
142 it.each([
143 ['not-a-number', '81a4'],
144 ['Infinity', '81a4'],
145 ['-1', '81a4'],
146 ['', '81a4'],
147 ['5', '81a4junk'],
148 ['5', '-81a4'],
149 [' 5', '81a4'],
150 ['5 ', '81a4'],
151 ['5\n6', '81a4'],
152 ['NaN', '81a4'],
153 ['5', ''],
154 ['5', '0x81a4'],
155 ['5', '81a'],
156 ['5', '81a45'],
157 ['5', '81a4 '],
158 ['9007199254740992', '81a4'],
159 ])('rejects malformed lstat fields', async (size, mode) => {
160 const handle = createHandle()
161 setExec(handle, async () => ({
162 exitCode: 0,
163 stdout: `${mode}:${size}\n`,
164 stderr: '',
165 }))
166 await expect(handle.fs.lstat!('/workspace/file')).rejects.toThrow(
167 'invalid lstat output',
168 )
169 })
170 it('parses a block special file as other without size', async () => {
171 const handle = createHandle()
172 setExec(handle, async () => ({
173 exitCode: 0,
174 stdout: '61b6:0\n',
175 stderr: '',
176 }))
177 await expect(handle.fs.lstat!('/workspace/block')).resolves.toEqual({
178 type: 'other',
179 mode: 0x61b6,
180 })
181 })
182 it('parses a fifo as other without size', async () => {
183 const handle = createHandle()
184 setExec(handle, async () => ({
185 exitCode: 0,
186 stdout: '11b6:0\n',
187 stderr: '',
188 }))
189 await expect(handle.fs.lstat!('/workspace/fifo')).resolves.toEqual({
190 type: 'other',
191 mode: 0x11b6,
192 })
193 })
194 it('parses an unknown file type as other without size', async () => {
195 const handle = createHandle()
196 setExec(handle, async () => ({
197 exitCode: 0,
198 stdout: '71b6:7\n',
199 stderr: '',
200 }))
201 await expect(handle.fs.lstat!('/workspace/unknown')).resolves.toEqual({
202 type: 'other',
203 mode: 0x71b6,
204 })
205 })
206 it.each([' 81a4:12\n', '81a4:12\nextra\n'])(
207 'rejects non-protocol whitespace/payload: %j',
208 async (stdout) => {
209 const handle = createHandle()
210 setExec(handle, async () => ({ exitCode: 0, stdout, stderr: '' }))
211 await expect(handle.fs.lstat!('/workspace/file')).rejects.toThrow(
212 'invalid lstat output',
213 )
214 },
215 )
216})