-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmount-materialization.test.ts
More file actions
258 lines (230 loc) · 10.5 KB
/
Copy pathmount-materialization.test.ts
File metadata and controls
258 lines (230 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import assert from "node:assert/strict"
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"
import { tmpdir } from "node:os"
import { dirname, join } from "node:path"
import { materializePlaygroundStagedInputs } from "../packages/runtime-playground/src/mount-materialization.js"
const source = await mkdtemp(join(tmpdir(), "wp-codebox-mount-materialization-"))
const writes: Record<string, string> = {}
try {
await mkdir(join(source, "src"), { recursive: true })
await mkdir(join(source, "node_modules", "large-package"), { recursive: true })
await writeFile(join(source, "src", "example.php"), "<?php echo 'ok';")
await writeFile(join(source, "node_modules", "large-package", "ignored.php"), "<?php echo 'ignored';")
const result = await materializePlaygroundStagedInputs({
playground: {
async run({ code }: { code: string }) {
return code.includes("wp-codebox/host-mount-verification/v1")
? { text: JSON.stringify({ schema: "wp-codebox/host-mount-verification/v1", repaired: 0, skipped: 0 }) }
: { text: JSON.stringify({ schema: "wp-codebox/host-mount-directory-materialization/v1", created: 1, skipped: 0 }) }
},
async writeFile(target: string, contents: string) { writes[target] = contents },
},
} as never, [{
type: "directory",
source,
target: "/wordpress/project",
mode: "readwrite",
}])
assert.equal(result.materialized, 1)
assert.deepEqual(Object.keys(writes), ["/wordpress/project/src/example.php"])
assert.equal(writes["/wordpress/project/src/example.php"], "<?php echo 'ok';")
} finally {
await rm(source, { recursive: true, force: true })
}
const directorySource = await mkdtemp(join(tmpdir(), "wp-codebox-directory-materialization-"))
const readableDirectories = new Set<string>()
const directoryWrites: Record<string, string> = {}
try {
await mkdir(join(directorySource, "bin", "tests", "i18n-tools", "fixtures", "empty"), { recursive: true })
await writeFile(join(directorySource, "bin", "tests", "i18n-tools", "phpunit.xml"), "<phpunit />")
const result = await materializePlaygroundStagedInputs({
playground: {
async run({ code }: { code: string }) {
const payload = materializationPayload(code)
if (code.includes("wp-codebox/host-mount-verification/v1")) {
return { text: JSON.stringify({ schema: "wp-codebox/host-mount-verification/v1", repaired: 0, skipped: 0 }) }
}
for (const directory of payload.directories ?? []) {
readableDirectories.add(directory)
}
return { text: JSON.stringify({ schema: "wp-codebox/host-mount-directory-materialization/v1", created: payload.directories?.length ?? 0, skipped: 0 }) }
},
async writeFile(target: string, contents: string) {
if (!readableDirectories.has(dirname(target))) {
throw new Error(`sandbox directory was not materialized before write: ${dirname(target)}`)
}
directoryWrites[target] = contents
},
},
} as never, [{
type: "directory",
source: directorySource,
target: "/home/example/public_html",
mode: "readonly",
}])
assert.equal(result.materialized, 1)
assert.equal(result.phaseResult.status, "completed")
assert.equal(readableDirectories.has("/home/example/public_html"), true, "mount target is created")
assert.equal(readableDirectories.has("/home/example/public_html/bin/tests/i18n-tools"), true, "nested cwd target is created")
assert.equal(readableDirectories.has("/home/example/public_html/bin/tests/i18n-tools/fixtures/empty"), true, "empty subdirectories are created")
assert.equal(directoryWrites["/home/example/public_html/bin/tests/i18n-tools/phpunit.xml"], "<phpunit />")
} finally {
await rm(directorySource, { recursive: true, force: true })
}
const fallbackSource = await mkdtemp(join(tmpdir(), "wp-codebox-batched-materialization-"))
const fallbackFileBatches: number[] = []
try {
await mkdir(join(fallbackSource, "files"), { recursive: true })
for (let index = 0; index < 205; index++) {
await writeFile(join(fallbackSource, "files", `file-${index}.txt`), `file ${index}`)
}
const result = await materializePlaygroundStagedInputs({
playground: {
async run({ code }: { code: string }) {
const payload = materializationPayload(code)
if (code.includes("wp-codebox/host-mount-materialization/v1")) {
fallbackFileBatches.push(payload.files?.length ?? 0)
return { text: JSON.stringify({ schema: "wp-codebox/host-mount-materialization/v1", materialized: payload.files?.length ?? 0, skipped: 0 }) }
}
return { text: JSON.stringify({ schema: "wp-codebox/host-mount-directory-materialization/v1", created: payload.directories?.length ?? 0, skipped: 0 }) }
},
},
} as never, [{
type: "directory",
source: fallbackSource,
target: "/workspace/large-tree",
mode: "readwrite",
}])
assert.equal(result.materialized, 205)
assert.equal(fallbackFileBatches.length, 3, "large fallback writes are split into bounded batches")
assert.deepEqual(fallbackFileBatches, [100, 100, 5])
} finally {
await rm(fallbackSource, { recursive: true, force: true })
}
const silentlyDroppedSource = await mkdtemp(join(tmpdir(), "wp-codebox-silently-dropped-materialization-"))
const verifiedFiles: Record<string, string> = {}
try {
await mkdir(join(silentlyDroppedSource, "composer"), { recursive: true })
await writeFile(join(silentlyDroppedSource, "composer", "autoload_real.php"), "<?php require __DIR__ . '/autoload_static.php';")
await writeFile(join(silentlyDroppedSource, "composer", "autoload_static.php"), "<?php class ComposerStaticInitFixture {}")
const result = await materializePlaygroundStagedInputs({
playground: {
async run({ code }: { code: string }) {
const payload = materializationPayload(code)
if (code.includes("wp-codebox/host-mount-verification/v1")) {
let repaired = 0
for (const file of payload.files ?? []) {
const contents = Buffer.from(file.contentsBase64, "base64").toString("utf8")
if (verifiedFiles[file.target] !== contents) {
verifiedFiles[file.target] = contents
repaired++
}
}
return { text: JSON.stringify({ schema: "wp-codebox/host-mount-verification/v1", repaired, skipped: 0 }) }
}
return { text: JSON.stringify({ schema: "wp-codebox/host-mount-directory-materialization/v1", created: payload.directories?.length ?? 0, skipped: 0 }) }
},
async writeFile(target: string, contents: string) {
if (!target.endsWith("autoload_static.php")) {
verifiedFiles[target] = contents
}
},
},
} as never, [{
type: "directory",
source: silentlyDroppedSource,
target: "/wp-codebox-vendor",
mode: "readonly",
}])
assert.equal(result.materialized, 2)
assert.equal(verifiedFiles["/wp-codebox-vendor/composer/autoload_static.php"], "<?php class ComposerStaticInitFixture {}", "verification repairs a direct writer that silently omits a generated companion file")
} finally {
await rm(silentlyDroppedSource, { recursive: true, force: true })
}
const unreadableTargetSource = await mkdtemp(join(tmpdir(), "wp-codebox-unreadable-target-"))
try {
await mkdir(join(unreadableTargetSource, "bin", "tests", "i18n-tools"), { recursive: true })
await writeFile(join(unreadableTargetSource, "bin", "tests", "i18n-tools", "phpunit.xml"), "<phpunit />")
await assert.rejects(
materializePlaygroundStagedInputs({
playground: {
async run({ code }: { code: string }) {
const payload = materializationPayload(code)
return {
text: JSON.stringify({
schema: "wp-codebox/host-mount-directory-materialization/v1",
created: payload.directories?.length ?? 0,
skipped: 0,
missing: ["/home/example/public_html/bin/tests/i18n-tools"],
}),
}
},
async writeFile() {
throw new Error("files should not be written when directory verification fails")
},
},
} as never, [{
type: "directory",
source: unreadableTargetSource,
target: "/home/example/public_html",
mode: "readwrite",
}]),
/Staged input mount target directories are not readable in the sandbox after materialization: \/home\/example\/public_html\/bin\/tests\/i18n-tools \(missing\)/,
)
} finally {
await rm(unreadableTargetSource, { recursive: true, force: true })
}
const failedVerificationSource = await mkdtemp(join(tmpdir(), "wp-codebox-failed-directory-verification-"))
try {
await mkdir(join(failedVerificationSource, "bin", "tests", "i18n-tools"), { recursive: true })
await assert.rejects(
materializePlaygroundStagedInputs({
playground: {
async run() {
return { exitCode: 1, errors: "mkdir failed", text: "" }
},
async writeFile() {
throw new Error("files should not be written when directory verification exits non-zero")
},
},
} as never, [{
type: "directory",
source: failedVerificationSource,
target: "/home/example/public_html",
mode: "readwrite",
}]),
/playground-staged-input-mkdir failed with exit code 1/,
)
} finally {
await rm(failedVerificationSource, { recursive: true, force: true })
}
const malformedVerificationSource = await mkdtemp(join(tmpdir(), "wp-codebox-malformed-directory-verification-"))
try {
await mkdir(join(malformedVerificationSource, "bin", "tests", "i18n-tools"), { recursive: true })
await assert.rejects(
materializePlaygroundStagedInputs({
playground: {
async run() {
return { text: "" }
},
async writeFile() {
throw new Error("files should not be written when directory verification omits its schema")
},
},
} as never, [{
type: "directory",
source: malformedVerificationSource,
target: "/home/example/public_html",
mode: "readwrite",
}]),
/playground-staged-input-mkdir did not return wp-codebox\/host-mount-directory-materialization\/v1/,
)
} finally {
await rm(malformedVerificationSource, { recursive: true, force: true })
}
function materializationPayload(code: string): { directories?: string[]; files?: Array<{ target: string; contentsBase64: string }> } {
const match = code.match(/\$payload = json_decode\((.*), true\);/)
assert.ok(match, "materialization PHP includes a JSON payload")
return JSON.parse(JSON.parse(match[1]))
}
console.log("mount materialization ok")