Skip to content

Commit bc58b2b

Browse files
committed
Add tests
1 parent 8662b6f commit bc58b2b

File tree

5 files changed

+35
-8
lines changed

5 files changed

+35
-8
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { describe, expect, it } from "vitest";
2+
import { get_file_name_from_path } from "./fileUtils";
3+
4+
describe ("get_file_name_from_path", () => {
5+
it("strips files/contents", async () => {
6+
const path = await get_file_name_from_path('/files/contents/cats');
7+
expect(path).toBe("/cats");
8+
}),
9+
it("works if files/contents is not present", async () => {
10+
const path = await get_file_name_from_path('/dogs');
11+
expect(path).toBe("/dogs");
12+
}),
13+
it("strips a trailing slash", async () => {
14+
const path = await get_file_name_from_path('/files/contents/birds/');
15+
expect(path).toBe("/birds");
16+
})
17+
})
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export async function get_file_name_from_path(path: string): Promise<string> {
2+
path = path.replace('/files/contents', '')
3+
path = path.endsWith('/') ? path.substring(0, path.length - 1) : path
4+
5+
return path
6+
}

apps/sandbox-container/container/index.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import { exec } from 'node:child_process'
77
import * as fs from 'node:fs/promises'
88
import path from 'node:path'
99

10-
import { ExecParams, FileList, FilesWrite } from '../shared/schema.ts'
10+
import type { FileList } from '../shared/schema.ts'
11+
import { ExecParams, FilesWrite } from '../shared/schema.ts'
12+
import { get_file_name_from_path } from './fileUtils.ts'
1113

1214
process.chdir('workdir')
1315

@@ -60,8 +62,7 @@ app.get('/files/ls', async (c) => {
6062
* Get the contents of a file or directory
6163
*/
6264
app.get('/files/contents/*', async (c) => {
63-
let reqPath = c.req.path.replace('/files/contents', '')
64-
reqPath = reqPath.endsWith('/') ? reqPath.substring(0, reqPath.length - 1) : reqPath
65+
const reqPath = await get_file_name_from_path(c.req.path)
6566
try {
6667
const mimeType = mime.getType(reqPath)
6768
const headers = mimeType ? { 'Content-Type': mimeType } : undefined
@@ -105,7 +106,8 @@ app.get('/files/contents/*', async (c) => {
105106
*/
106107
app.post('/files/contents', zValidator('json', FilesWrite), async (c) => {
107108
const file = c.req.valid('json')
108-
const reqPath = file.path.endsWith('/') ? file.path.substring(0, file.path.length - 1) : file.path
109+
const reqPath = await get_file_name_from_path(file.path)
110+
109111
try {
110112
await fs.writeFile(reqPath, file.text)
111113
return c.newResponse(null, 200)
@@ -120,8 +122,8 @@ app.post('/files/contents', zValidator('json', FilesWrite), async (c) => {
120122
* Delete a file or directory
121123
*/
122124
app.delete('/files/contents/*', async (c) => {
123-
let reqPath = c.req.path.replace('/files/contents', '')
124-
reqPath = reqPath.endsWith('/') ? reqPath.substring(0, reqPath.length - 1) : reqPath
125+
const reqPath = await get_file_name_from_path(c.req.path)
126+
125127
try {
126128
await fs.rm(path.join(process.cwd(), reqPath), {recursive: true})
127129
return c.newResponse('ok', 200)

apps/sandbox-container/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"start": "wrangler dev",
1212
"start:container": "tsx container/index.ts",
1313
"postinstall": "mkdir -p workdir",
14+
"test": "vitest",
1415
"types": "wrangler types"
1516
},
1617
"dependencies": {

apps/sandbox-container/server/containerMcp.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
22
import { McpAgent } from 'agents/mcp'
33
import { z } from 'zod'
44

5-
import { Env, Props } from '.'
5+
import type { Env, Props } from '.'
66
import { OPEN_CONTAINER_PORT } from '../shared/consts'
7-
import { ExecParams, FileList, FilePathParam, FilesWrite } from '../shared/schema'
7+
import type { FileList } from '../shared/schema'
8+
import { ExecParams, FilePathParam, FilesWrite } from '../shared/schema'
89
import { MAX_CONTAINERS, proxyFetch, startAndWaitForPort } from './containerHelpers'
910
import { getContainerManager } from './containerManager'
1011
import { BASE_INSTRUCTIONS } from './prompts'

0 commit comments

Comments
 (0)