Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import { E2BHandle } from '../src/handle'
3import type { Sandbox } from 'e2b'
4
5type ExecResult = { exitCode: number; stdout: string; stderr: string }
6type ExecFn = (command: string) => Promise<ExecResult>
7function lstatCommand(path: string): string {
8 const quoted = `'${path.replace(/'/g, `'\\''`)}'`
9 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`
10}
11function lstatPath(command: string): string {
12 return /^tanstack_lstat_path='([^']*)';/.exec(command)?.[1] ?? ''
13}
14function execSlot(handle: object) {
15 return {
16 set exec(value: ExecFn) {
17 Object.defineProperty(handle, 'exec', { configurable: true, value })
18 },
19 }
20}
21
22function createHandle(): E2BHandle {
23 return new E2BHandle({
24 sandbox: { sandboxId: 'test' } as unknown as Sandbox,
25 workdir: '/workspace',
26 })
27}
28
29describe('E2BHandle.fs.lstat', () => {
30 it('parses file, directory, symlink, and other metadata', async () => {
31 const values = new Map<string, string>([
32 ['file', '81A4:12\n'],
33 ['dir', '41ed:4096\n'],
34 ['link', 'a1ff:4\n'],
35 ['other', 'c1b6:0\n'],
36 ])
37 const handle = createHandle()
38 execSlot(handle).exec = async (command: string) => {
39 const path = lstatPath(command)
40 expect(command).toBe(lstatCommand(path))
41 return {
42 exitCode: 0,
43 stdout: values.get(path.split('/').pop()!) ?? '',
44 stderr: '',
45 }
46 }
47 await expect(handle.fs.lstat!('/workspace/file')).resolves.toEqual({
48 type: 'file',
49 mode: 0x81a4,
50 size: 12,
51 })
52 await expect(handle.fs.lstat!('/workspace/dir')).resolves.toEqual({
53 type: 'dir',
54 mode: 0x41ed,
55 })
56 await expect(handle.fs.lstat!('/workspace/link')).resolves.toEqual({
57 type: 'symlink',
58 mode: 0xa1ff,
59 })
60 await expect(handle.fs.lstat!('/workspace/other')).resolves.toEqual({
61 type: 'other',
62 mode: 0xc1b6,
63 })
64 })
65
66 it('parses a zero-byte regular empty file with size zero', async () => {
67 const handle = createHandle()
68 execSlot(handle).exec = async () => ({
69 exitCode: 0,
70 stdout: '81a4:0\n',
71 stderr: '',
72 })
73 await expect(handle.fs.lstat!('/workspace/empty')).resolves.toEqual({
74 type: 'file',
75 mode: 0x81a4,
76 size: 0,
77 })
78 })
79
80 it.each([
81 ['not-a-number', '81a4'],
82 ['-1', '81a4'],
83 ['', '81a4'],
84 ['5', '81a4junk'],
85 ['5', ''],
86 ['5', '0x81a4'],
87 ['9007199254740992', '81a4'],
88 ])('rejects malformed lstat fields: size=%s mode=%s', async (size, mode) => {
89 const handle = createHandle()
90 execSlot(handle).exec = async () => ({
91 exitCode: 0,
92 stdout: `${mode}:${size}\n`,
93 stderr: '',
94 })
95 await expect(handle.fs.lstat!('/workspace/file')).rejects.toThrow(
96 'invalid lstat output',
97 )
98 })
99
100 it.each([
101 '/workspace/missing',
102 '/workspace/missing-parent/child',
103 '-H/missing',
104 ])('returns undefined for a verified missing path: %s', async (path) => {
105 const handle = createHandle()
106 execSlot(handle).exec = async (command) => {
107 expect(command).toBe(lstatCommand(path))
108 return {
109 exitCode: 0,
110 stdout: '__TANSTACK_LSTAT_MISSING__',
111 stderr: '',
112 }
113 }
114 await expect(handle.fs.lstat!(path)).resolves.toBeUndefined()
115 })
116
117 it('treats a transport-padded missing sentinel as missing', async () => {
118 const handle = createHandle()
119 execSlot(handle).exec = async () => ({
120 exitCode: 0,
121 stdout: '__TANSTACK_LSTAT_MISSING__\n',
122 stderr: '',
123 })
124 await expect(
125 handle.fs.lstat!('/workspace/missing'),
126 ).resolves.toBeUndefined()
127 })
128
129 it('preserves an unverified parent failure', async () => {
130 const handle = createHandle()
131 execSlot(handle).exec = async () => ({
132 exitCode: 1,
133 stdout: '',
134 stderr: 'permission denied',
135 })
136 await expect(handle.fs.lstat!('/workspace/file/child')).rejects.toThrow(
137 'lstat failed: permission denied',
138 )
139 })
140})