mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 06:20:43 +00:00
test: speed up small unit fast cases
This commit is contained in:
@@ -1,12 +1,27 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { createSafeStreamWriter } from "./stream-writer.js";
|
||||
|
||||
function createSpy<Args extends unknown[], ReturnValue>(
|
||||
implementation?: (...args: Args) => ReturnValue,
|
||||
) {
|
||||
const calls: Args[] = [];
|
||||
const spy = (...args: Args) => {
|
||||
calls.push(args);
|
||||
return implementation?.(...args) as ReturnValue;
|
||||
};
|
||||
spy.calls = calls;
|
||||
spy.clear = () => {
|
||||
calls.length = 0;
|
||||
};
|
||||
return spy;
|
||||
}
|
||||
|
||||
describe("createSafeStreamWriter", () => {
|
||||
it("signals broken pipes and closes the writer", () => {
|
||||
const onBrokenPipe = vi.fn();
|
||||
const onBrokenPipe = createSpy<[], void>();
|
||||
const writer = createSafeStreamWriter({ onBrokenPipe });
|
||||
const stream = {
|
||||
write: vi.fn(() => {
|
||||
write: createSpy<[string], boolean>(() => {
|
||||
const err = new Error("EPIPE") as NodeJS.ErrnoException;
|
||||
err.code = "EPIPE";
|
||||
throw err;
|
||||
@@ -15,15 +30,15 @@ describe("createSafeStreamWriter", () => {
|
||||
|
||||
expect(writer.writeLine(stream, "hello")).toBe(false);
|
||||
expect(writer.isClosed()).toBe(true);
|
||||
expect(onBrokenPipe).toHaveBeenCalledTimes(1);
|
||||
expect(onBrokenPipe.calls).toHaveLength(1);
|
||||
|
||||
onBrokenPipe.mockClear();
|
||||
onBrokenPipe.clear();
|
||||
expect(writer.writeLine(stream, "again")).toBe(false);
|
||||
expect(onBrokenPipe).toHaveBeenCalledTimes(0);
|
||||
expect(onBrokenPipe.calls).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("treats broken pipes from beforeWrite as closed", () => {
|
||||
const onBrokenPipe = vi.fn();
|
||||
const onBrokenPipe = createSpy<[], void>();
|
||||
const writer = createSafeStreamWriter({
|
||||
onBrokenPipe,
|
||||
beforeWrite: () => {
|
||||
@@ -32,10 +47,12 @@ describe("createSafeStreamWriter", () => {
|
||||
throw err;
|
||||
},
|
||||
});
|
||||
const stream = { write: vi.fn(() => true) } as unknown as NodeJS.WriteStream;
|
||||
const stream = {
|
||||
write: createSpy<[string], boolean>(() => true),
|
||||
} as unknown as NodeJS.WriteStream;
|
||||
|
||||
expect(writer.write(stream, "hi")).toBe(false);
|
||||
expect(writer.isClosed()).toBe(true);
|
||||
expect(onBrokenPipe).toHaveBeenCalledTimes(1);
|
||||
expect(onBrokenPipe.calls).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -54,6 +54,7 @@ describe("unit-fast vitest lane", () => {
|
||||
expect(config.test?.include).toContain("src/video-generation/provider-registry.test.ts");
|
||||
expect(config.test?.include).toContain("src/plugin-sdk/provider-entry.test.ts");
|
||||
expect(config.test?.include).toContain("src/security/dangerous-config-flags.test.ts");
|
||||
expect(config.test?.include).toContain("src/security/context-visibility.test.ts");
|
||||
expect(config.test?.include).toContain("src/security/safe-regex.test.ts");
|
||||
});
|
||||
|
||||
|
||||
@@ -160,6 +160,7 @@ export const forcedUnitFastTestFiles = [
|
||||
"src/security/scan-paths.test.ts",
|
||||
"src/security/skill-scanner.test.ts",
|
||||
"src/security/audit-config-include-perms.test.ts",
|
||||
"src/security/context-visibility.test.ts",
|
||||
"src/realtime-transcription/websocket-session.test.ts",
|
||||
"src/realtime-voice/agent-consult-tool.test.ts",
|
||||
"src/routing/resolve-route.test.ts",
|
||||
|
||||
Reference in New Issue
Block a user