refactor(fs): use mkdirSafe recursive and align test mocks

Replace try/access/mkdir pattern with single mkdirSafe call in ssh-key-manager; drop unused mkdir mocks from stt/tts/repo tests and mock fs-safe's mkdirSafe instead; add zod peer dep extension to pnpm workspace; note EACCES-tolerant startup in troubleshooting doc.
This commit is contained in:
Chris Scott committed 2026-07-23 22:59:05 -04:00
1 parent f20d8ffb71
commit 376bd67f1b
7 files changed
+49 -22

No files matched your search

+2 -10
View File
@@ -9,11 +9,7 @@ import { mkdirSafe } from './fs-safe'
const SSH_KEYS_DIR = join(getWorkspacePath(), '.ssh-keys')
async function ensureSSHKeysDir(): Promise<void> {
try {
await fs.access(SSH_KEYS_DIR)
} catch {
await mkdirSafe(SSH_KEYS_DIR, { mode: 0o700 })
}
await mkdirSafe(SSH_KEYS_DIR, { mode: 0o700 })
}
async function validateSSHKey(keyPath: string): Promise<boolean> {
@@ -167,11 +163,7 @@ export function generateSSHConfig(entries: SSHConfigEntry[]): string {
export async function writeSSHConfig(configPath: string, configContent: string): Promise<void> {
const dir = join(getWorkspacePath(), 'config')
try {
await fs.access(dir)
} catch {
await mkdirSafe(dir, { mode: 0o700 })
}
await mkdirSafe(dir, { mode: 0o700 })
await fs.writeFile(configPath, configContent, { mode: 0o600 })
}
-5
View File
@@ -2,7 +2,6 @@ import { describe, it, expect, vi, beforeEach } from 'vitest'
import * as fs from 'fs/promises'
vi.mock('fs/promises', () => ({
mkdir: vi.fn(),
readFile: vi.fn(),
writeFile: vi.fn(),
stat: vi.fn(),
@@ -45,7 +44,6 @@ vi.mock('@opencode-manager/shared/config/env', () => ({
},
}))
const mockMkdir = fs.mkdir as any
const mockReadFile = fs.readFile as any
const mockWriteFile = fs.writeFile as any
const mockStat = fs.stat as any
@@ -432,7 +430,6 @@ describe('STT Routes', () => {
mtimeMs: Date.now() - 2 * 60 * 60 * 1000,
})
mockUnlink.mockResolvedValue(undefined)
mockMkdir.mockResolvedValue(undefined)
mockWriteFile.mockResolvedValue(undefined)
const mockFetch = vi.fn().mockResolvedValue({
@@ -461,7 +458,6 @@ describe('STT Routes', () => {
it('should return default model when API fetch fails', async () => {
const originalFetch = globalThis.fetch
mockStat.mockRejectedValue(new Error('File not found'))
mockMkdir.mockResolvedValue(undefined)
mockWriteFile.mockResolvedValue(undefined)
const mockFetch = vi.fn().mockRejectedValue(new Error('Network error'))
@@ -479,7 +475,6 @@ describe('STT Routes', () => {
it('should force refresh when refresh=true', async () => {
const originalFetch = globalThis.fetch
mockMkdir.mockResolvedValue(undefined)
mockWriteFile.mockResolvedValue(undefined)
const mockFetch = vi.fn().mockResolvedValue({
-1
View File
@@ -2,7 +2,6 @@ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import * as fs from 'fs/promises'
vi.mock('fs/promises', () => ({
mkdir: vi.fn(),
readFile: vi.fn(),
writeFile: vi.fn(),
readdir: vi.fn(),
+7 -3
View File
@@ -16,21 +16,25 @@ const deleteRepo = vi.fn()
const lstat = vi.fn()
const stat = vi.fn()
const readdir = vi.fn()
const mkdir = vi.fn()
const symlink = vi.fn()
const readlink = vi.fn()
const mkdirSafe = vi.fn()
vi.mock('fs/promises', () => ({
default: {
lstat,
stat,
readdir,
mkdir,
symlink,
readlink,
},
}))
vi.mock('../../src/utils/fs-safe', () => ({
mkdirSafe,
mkdirSyncSafe: vi.fn(),
}))
vi.mock('../../src/utils/process', () => ({
executeCommand,
}))
@@ -84,7 +88,7 @@ describe('repo service', () => {
beforeEach(() => {
vi.clearAllMocks()
ensureDirectoryExists.mockResolvedValue(undefined)
mkdir.mockResolvedValue(undefined)
mkdirSafe.mockResolvedValue(undefined)
symlink.mockResolvedValue(undefined)
readlink.mockResolvedValue('')
readdir.mockResolvedValue([])
+2
View File
@@ -48,6 +48,8 @@ ports:
**Symptoms:** Container can't write to volumes
Startup tolerates workspace directories that already exist with wrong ownership; permission errors then appear on the first write into the directory rather than at startup.
**Solutions:**
1. Fix ownership:
+30 -3
View File
@@ -4,6 +4,8 @@ settings:
autoInstallPeers: true
excludeLinksFromLockfile: false
packageExtensionsChecksum: sha256-MiHZ3mgtOUb60KfpkAkui5ad/9eLmcQda0MiZKaTRqA=
importers:
.:
@@ -111,7 +113,7 @@ importers:
version: 3.2.2(react@19.2.3)
'@hookform/resolvers':
specifier: ^5.2.2
version: 5.2.2(react-hook-form@7.69.0(react@19.2.3))
version: 5.2.2(react-hook-form@7.69.0(react@19.2.3))(zod@4.3.2)
'@opencode-manager/shared':
specifier: workspace:*
version: link:../shared
@@ -899,6 +901,10 @@ packages:
resolution: {integrity: sha512-A/IxlMLShx3KjV/HeTcTfaMxdwy690+L/ZADoeaTltLx+CVuzkeVIPuybK3jrRfw7YZnmdKsVVHAlEPIAEUNlA==}
peerDependencies:
react-hook-form: ^7.55.0
zod: '*'
peerDependenciesMeta:
zod:
optional: true
'@humanfs/core@0.19.1':
resolution: {integrity: sha512-5DyQ4+1JEUzejeK1JGICcideyfUbGixgS9jNgex5nqkW+cY7WZhxBigmieN5Qnw9ZosSNVC9KQKyb+GUaGyKUA==}
@@ -1623,56 +1629,67 @@ packages:
resolution: {integrity: sha512-EHMUcDwhtdRGlXZsGSIuXSYwD5kOT9NVnx9sqzYiwAc91wfYOE1g1djOEDseZJKKqtHAHGwnGPQu3kytmfaXLQ==}
cpu: [arm]
os: [linux]
libc: [glibc]
'@rollup/rollup-linux-arm-musleabihf@4.54.0':
resolution: {integrity: sha512-+pBrqEjaakN2ySv5RVrj/qLytYhPKEUwk+e3SFU5jTLHIcAtqh2rLrd/OkbNuHJpsBgxsD8ccJt5ga/SeG0JmA==}
cpu: [arm]
os: [linux]
libc: [musl]
'@rollup/rollup-linux-arm64-gnu@4.54.0':
resolution: {integrity: sha512-NSqc7rE9wuUaRBsBp5ckQ5CVz5aIRKCwsoa6WMF7G01sX3/qHUw/z4pv+D+ahL1EIKy6Enpcnz1RY8pf7bjwng==}
cpu: [arm64]
os: [linux]
libc: [glibc]
'@rollup/rollup-linux-arm64-musl@4.54.0':
resolution: {integrity: sha512-gr5vDbg3Bakga5kbdpqx81m2n9IX8M6gIMlQQIXiLTNeQW6CucvuInJ91EuCJ/JYvc+rcLLsDFcfAD1K7fMofg==}
cpu: [arm64]
os: [linux]
libc: [musl]
'@rollup/rollup-linux-loong64-gnu@4.54.0':
resolution: {integrity: sha512-gsrtB1NA3ZYj2vq0Rzkylo9ylCtW/PhpLEivlgWe0bpgtX5+9j9EZa0wtZiCjgu6zmSeZWyI/e2YRX1URozpIw==}
cpu: [loong64]
os: [linux]
libc: [glibc]
'@rollup/rollup-linux-ppc64-gnu@4.54.0':
resolution: {integrity: sha512-y3qNOfTBStmFNq+t4s7Tmc9hW2ENtPg8FeUD/VShI7rKxNW7O4fFeaYbMsd3tpFlIg1Q8IapFgy7Q9i2BqeBvA==}
cpu: [ppc64]
os: [linux]
libc: [glibc]
'@rollup/rollup-linux-riscv64-gnu@4.54.0':
resolution: {integrity: sha512-89sepv7h2lIVPsFma8iwmccN7Yjjtgz0Rj/Ou6fEqg3HDhpCa+Et+YSufy27i6b0Wav69Qv4WBNl3Rs6pwhebQ==}
cpu: [riscv64]
os: [linux]
libc: [glibc]
'@rollup/rollup-linux-riscv64-musl@4.54.0':
resolution: {integrity: sha512-ZcU77ieh0M2Q8Ur7D5X7KvK+UxbXeDHwiOt/CPSBTI1fBmeDMivW0dPkdqkT4rOgDjrDDBUed9x4EgraIKoR2A==}
cpu: [riscv64]
os: [linux]
libc: [musl]
'@rollup/rollup-linux-s390x-gnu@4.54.0':
resolution: {integrity: sha512-2AdWy5RdDF5+4YfG/YesGDDtbyJlC9LHmL6rZw6FurBJ5n4vFGupsOBGfwMRjBYH7qRQowT8D/U4LoSvVwOhSQ==}
cpu: [s390x]
os: [linux]
libc: [glibc]
'@rollup/rollup-linux-x64-gnu@4.54.0':
resolution: {integrity: sha512-WGt5J8Ij/rvyqpFexxk3ffKqqbLf9AqrTBbWDk7ApGUzaIs6V+s2s84kAxklFwmMF/vBNGrVdYgbblCOFFezMQ==}
cpu: [x64]
os: [linux]
libc: [glibc]
'@rollup/rollup-linux-x64-musl@4.54.0':
resolution: {integrity: sha512-JzQmb38ATzHjxlPHuTH6tE7ojnMKM2kYNzt44LO/jJi8BpceEC8QuXYA908n8r3CNuG/B3BV8VR3Hi1rYtmPiw==}
cpu: [x64]
os: [linux]
libc: [musl]
'@rollup/rollup-openharmony-arm64@4.54.0':
resolution: {integrity: sha512-huT3fd0iC7jigGh7n3q/+lfPcXxBi+om/Rs3yiFxjvSxbSB6aohDFXbWvlspaqjeOh+hx7DDHS+5Es5qRkWkZg==}
@@ -1750,24 +1767,28 @@ packages:
engines: {node: '>= 10'}
cpu: [arm64]
os: [linux]
libc: [glibc]
'@tailwindcss/oxide-linux-arm64-musl@4.1.18':
resolution: {integrity: sha512-1px92582HkPQlaaCkdRcio71p8bc8i/ap5807tPRDK/uw953cauQBT8c5tVGkOwrHMfc2Yh6UuxaH4vtTjGvHg==}
engines: {node: '>= 10'}
cpu: [arm64]
os: [linux]
libc: [musl]
'@tailwindcss/oxide-linux-x64-gnu@4.1.18':
resolution: {integrity: sha512-v3gyT0ivkfBLoZGF9LyHmts0Isc8jHZyVcbzio6Wpzifg/+5ZJpDiRiUhDLkcr7f/r38SWNe7ucxmGW3j3Kb/g==}
engines: {node: '>= 10'}
cpu: [x64]
os: [linux]
libc: [glibc]
'@tailwindcss/oxide-linux-x64-musl@4.1.18':
resolution: {integrity: sha512-bhJ2y2OQNlcRwwgOAGMY0xTFStt4/wyU6pvI6LSuZpRgKQwxTec0/3Scu91O8ir7qCR3AuepQKLU/kX99FouqQ==}
engines: {node: '>= 10'}
cpu: [x64]
os: [linux]
libc: [musl]
'@tailwindcss/oxide-wasm32-wasi@4.1.18':
resolution: {integrity: sha512-LffYTvPjODiP6PT16oNeUQJzNVyJl1cjIebq/rWWBF+3eDst5JGEFSc5cWxyRCJ0Mxl+KyIkqRxk1XPEs9x8TA==}
@@ -3418,24 +3439,28 @@ packages:
engines: {node: '>= 12.0.0'}
cpu: [arm64]
os: [linux]
libc: [glibc]
lightningcss-linux-arm64-musl@1.30.2:
resolution: {integrity: sha512-5Vh9dGeblpTxWHpOx8iauV02popZDsCYMPIgiuw97OJ5uaDsL86cnqSFs5LZkG3ghHoX5isLgWzMs+eD1YzrnA==}
engines: {node: '>= 12.0.0'}
cpu: [arm64]
os: [linux]
libc: [musl]
lightningcss-linux-x64-gnu@1.30.2:
resolution: {integrity: sha512-Cfd46gdmj1vQ+lR6VRTTadNHu6ALuw2pKR9lYq4FnhvgBc4zWY1EtZcAc6EffShbb1MFrIPfLDXD6Xprbnni4w==}
engines: {node: '>= 12.0.0'}
cpu: [x64]
os: [linux]
libc: [glibc]
lightningcss-linux-x64-musl@1.30.2:
resolution: {integrity: sha512-XJaLUUFXb6/QG2lGIW6aIk6jKdtjtcffUT0NKvIqhSBY3hh9Ch+1LCeH80dR9q9LBjG3ewbDjnumefsLsP6aiA==}
engines: {node: '>= 12.0.0'}
cpu: [x64]
os: [linux]
libc: [musl]
lightningcss-win32-arm64-msvc@1.30.2:
resolution: {integrity: sha512-FZn+vaj7zLv//D/192WFFVA0RgHawIcHqLX9xuWiQt7P0PtdFEVaxgF9rjM/IRYHQXNnk61/H/gb2Ei+kUQ4xQ==}
@@ -5335,10 +5360,12 @@ snapshots:
dependencies:
hono: 4.11.7
'@hookform/resolvers@5.2.2(react-hook-form@7.69.0(react@19.2.3))':
'@hookform/resolvers@5.2.2(react-hook-form@7.69.0(react@19.2.3))(zod@4.3.2)':
dependencies:
'@standard-schema/utils': 0.3.0
react-hook-form: 7.69.0(react@19.2.3)
optionalDependencies:
zod: 4.3.2
'@humanfs/core@0.19.1': {}
@@ -6358,7 +6385,7 @@ snapshots:
'@testing-library/dom@10.4.1':
dependencies:
'@babel/code-frame': 7.29.0
'@babel/code-frame': 7.29.7
'@babel/runtime': 7.28.4
'@types/aria-query': 5.0.4
aria-query: 5.3.0
+8
View File
@@ -4,3 +4,11 @@ packages:
- 'frontend'
- 'ocm-cli'
- '!workspace/**'
packageExtensions:
'@hookform/resolvers':
peerDependencies:
zod: '*'
peerDependenciesMeta:
zod:
optional: true