Skip to content

Commit c631021

Browse files
committed
test(shell): stabilize metadata preview tests
1 parent 7fd91da commit c631021

2 files changed

Lines changed: 18 additions & 38 deletions

File tree

packages/opencode/src/tool/shell.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ function pathArgs(list: Part[], ps: boolean, cmd = false) {
220220
return out
221221
}
222222

223-
function preview(text: string) {
223+
export function previewMetadata(text: string) {
224224
if (Buffer.byteLength(text, "utf-8") <= MAX_METADATA_LENGTH) return text
225225
return "...\n\n" + tailBytes(text, MAX_METADATA_LENGTH)
226226
}
@@ -497,7 +497,7 @@ export const ShellTool = Tool.define(
497497
cut = true
498498
}
499499

500-
last = preview(last + chunk)
500+
last = previewMetadata(last + chunk)
501501

502502
if (file) {
503503
sink?.write(chunk)
@@ -589,7 +589,7 @@ export const ShellTool = Tool.define(
589589
return {
590590
title: input.description,
591591
metadata: {
592-
output: last || preview(output),
592+
output: last || previewMetadata(output),
593593
exit: code,
594594
description: input.description,
595595
truncated: cut,

packages/opencode/test/tool/shell.test.ts

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { describe, expect } from "bun:test"
1+
import { describe, expect, test } from "bun:test"
22
import { Cause, Effect, Exit, Layer } from "effect"
33
import type * as Scope from "effect/Scope"
44
import os from "os"
55
import path from "path"
66
import { Config } from "@/config/config"
77
import { Shell } from "../../src/shell/shell"
8-
import { ShellTool } from "../../src/tool/shell"
8+
import { ShellTool, previewMetadata } from "../../src/tool/shell"
99
import { Filesystem } from "@/util/filesystem"
1010
import { provideInstance, tmpdirScoped } from "../fixture/fixture"
1111
import type { Permission } from "../../src/permission"
@@ -1205,42 +1205,22 @@ describe("tool.shell truncation", () => {
12051205
),
12061206
)
12071207

1208-
it.live("truncates metadata output by utf8 byte length", () =>
1209-
runIn(
1210-
projectRoot,
1211-
Effect.gen(function* () {
1212-
const result = yield* run({
1213-
command: `${bin} -e ${evalarg("process.stdout.write(String.fromCodePoint(0x4e00).repeat(10001))")}`,
1214-
description: "Generate CJK output exceeding metadata byte limit",
1215-
})
1216-
const output = result.metadata.output
1217-
if (typeof output !== "string") throw new Error("expected metadata output")
1208+
test("truncates metadata output by utf8 byte length", () => {
1209+
const output = previewMetadata(String.fromCodePoint(0x4e00).repeat(10001))
12181210

1219-
expect(output.startsWith("...\n\n")).toBe(true)
1220-
expect(Buffer.byteLength(output.slice("...\n\n".length), "utf-8")).toBeLessThanOrEqual(30_000)
1221-
}),
1222-
),
1223-
)
1211+
expect(output.startsWith("...\n\n")).toBe(true)
1212+
expect(Buffer.byteLength(output.slice("...\n\n".length), "utf-8")).toBeLessThanOrEqual(30_000)
1213+
})
12241214

1225-
it.live("does not split surrogate pairs when truncating metadata output", () =>
1226-
runIn(
1227-
projectRoot,
1228-
Effect.gen(function* () {
1229-
const result = yield* run({
1230-
command: `${bin} -e ${evalarg(
1231-
"process.stdout.write(String.fromCharCode(97)+String.fromCodePoint(0x1f642).repeat(15000)+String.fromCharCode(98))",
1232-
)}`,
1233-
description: "Generate emoji output exceeding metadata byte limit",
1234-
})
1235-
const output = result.metadata.output
1236-
if (typeof output !== "string") throw new Error("expected metadata output")
1215+
test("does not split surrogate pairs when truncating metadata output", () => {
1216+
const output = previewMetadata(
1217+
String.fromCharCode(97) + String.fromCodePoint(0x1f642).repeat(15000) + String.fromCharCode(98),
1218+
)
1219+
const first = output.charCodeAt("...\n\n".length)
12371220

1238-
const first = output.charCodeAt("...\n\n".length)
1239-
expect(output.startsWith("...\n\n")).toBe(true)
1240-
expect(first < 0xdc00 || first > 0xdfff).toBe(true)
1241-
}),
1242-
),
1243-
)
1221+
expect(output.startsWith("...\n\n")).toBe(true)
1222+
expect(first < 0xdc00 || first > 0xdfff).toBe(true)
1223+
})
12441224

12451225
it.live("full output is saved to file when truncated", () =>
12461226
runIn(

0 commit comments

Comments
 (0)