chore: Fix types in tests 33/N.

This commit is contained in:
cpojer
2026-02-17 15:45:36 +09:00
parent f44b58fd58
commit 49bd9f75f4
21 changed files with 148 additions and 42 deletions

View File

@@ -2,13 +2,15 @@ import path from "node:path";
import { describe, expect, it, vi } from "vitest";
import { POSIX_OPENCLAW_TMP_DIR, resolvePreferredOpenClawTmpDir } from "./tmp-openclaw-dir.js";
type TmpDirOptions = NonNullable<Parameters<typeof resolvePreferredOpenClawTmpDir>[0]>;
function fallbackTmp(uid = 501) {
return path.join("/var/fallback", `openclaw-${uid}`);
}
function resolveWithMocks(params: {
lstatSync: ReturnType<typeof vi.fn>;
accessSync?: ReturnType<typeof vi.fn>;
lstatSync: NonNullable<TmpDirOptions["lstatSync"]>;
accessSync?: NonNullable<TmpDirOptions["accessSync"]>;
uid?: number;
tmpdirPath?: string;
}) {
@@ -28,7 +30,7 @@ function resolveWithMocks(params: {
describe("resolvePreferredOpenClawTmpDir", () => {
it("prefers /tmp/openclaw when it already exists and is writable", () => {
const lstatSync = vi.fn(() => ({
const lstatSync: NonNullable<TmpDirOptions["lstatSync"]> = vi.fn(() => ({
isDirectory: () => true,
isSymbolicLink: () => false,
uid: 501,
@@ -43,26 +45,28 @@ describe("resolvePreferredOpenClawTmpDir", () => {
});
it("prefers /tmp/openclaw when it does not exist but /tmp is writable", () => {
const lstatSync = vi.fn(() => {
const lstatSyncMock = vi.fn<NonNullable<TmpDirOptions["lstatSync"]>>(() => {
const err = new Error("missing") as Error & { code?: string };
err.code = "ENOENT";
throw err;
});
// second lstat call (after mkdir) should succeed
lstatSync.mockImplementationOnce(() => {
lstatSyncMock.mockImplementationOnce(() => {
const err = new Error("missing") as Error & { code?: string };
err.code = "ENOENT";
throw err;
});
lstatSync.mockImplementationOnce(() => ({
lstatSyncMock.mockImplementationOnce(() => ({
isDirectory: () => true,
isSymbolicLink: () => false,
uid: 501,
mode: 0o40700,
}));
const { resolved, accessSync, mkdirSync, tmpdir } = resolveWithMocks({ lstatSync });
const { resolved, accessSync, mkdirSync, tmpdir } = resolveWithMocks({
lstatSync: lstatSyncMock,
});
expect(resolved).toBe(POSIX_OPENCLAW_TMP_DIR);
expect(accessSync).toHaveBeenCalledWith("/tmp", expect.any(Number));
@@ -76,7 +80,7 @@ describe("resolvePreferredOpenClawTmpDir", () => {
isSymbolicLink: () => false,
uid: 501,
mode: 0o100644,
}));
})) as unknown as ReturnType<typeof vi.fn> & NonNullable<TmpDirOptions["lstatSync"]>;
const { resolved, tmpdir } = resolveWithMocks({ lstatSync });
expect(resolved).toBe(fallbackTmp());