chore(lint): replace ESLint with Oxlint and clear anti-slop findings

This commit is contained in:
Chris Scott committed 2026-11-13 01:13:34 -05:00
1 parent c6c13335e5
commit b625acfc0c
1083 files changed
+67089 -80136

No files matched your search

+90 -63
View File
@@ -1,22 +1,30 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import type { OcmState } from '../src/state.js'
import {
MockTokenStoreError,
mockStateModule,
mockTokenStoreModule,
unmockTokenStoreModules,
type TokenStoreMockOptions,
} from './helpers/token-store-mocks.js'
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs'
import { dirname } from 'node:path'
import { z } from 'zod'
import { createFileTokenStore } from '../src/token-store-file.js'
import { createCliHome, type CliHome } from './helpers/cli-home.js'
type CliOverrides = TokenStoreMockOptions & {
state?: OcmState | null
const MANAGER = 'https://manager.example.com'
interface LoadOptions {
state?: { managerUrl: string } | null
token?: string | null
envToken?: string | null
documentVersion?: number
}
async function loadCli(overrides: CliOverrides = {}) {
vi.resetModules()
const state = 'state' in overrides ? overrides.state : { managerUrl: 'https://manager.example.com' }
const { clearState } = mockStateModule(state ?? null)
const { getToken, setToken, deleteToken } = mockTokenStoreModule(overrides)
const homes: CliHome[] = []
async function loadCli(options: LoadOptions = {}) {
const home = createCliHome({
state: options.state === undefined ? { managerUrl: MANAGER } : options.state,
token: options.token ?? null,
envToken: options.envToken ?? null,
documentVersion: options.documentVersion,
account: MANAGER,
})
homes.push(home)
const written: string[] = []
const stderr: string[] = []
const stdoutSpy = vi.spyOn(process.stdout, 'write').mockImplementation((chunk) => {
@@ -27,15 +35,14 @@ async function loadCli(overrides: CliOverrides = {}) {
stderr.push(String(chunk))
return true
})
vi.resetModules()
const cli = await import('../bin/ocm')
return {
cli,
written,
stderr,
getToken,
setToken,
deleteToken,
clearState,
home,
storedToken: () => createFileTokenStore(home.credentialsPath).get(MANAGER),
restore: () => {
stdoutSpy.mockRestore()
stderrSpy.mockRestore()
@@ -43,6 +50,16 @@ async function loadCli(overrides: CliOverrides = {}) {
}
}
async function exitCodeOnFailure(run: () => Promise<void>): Promise<number | undefined> {
const exitSpy = vi.spyOn(process, 'exit').mockImplementation(() => {
throw new Error('exit')
})
await expect(run()).rejects.toThrow('exit')
const code = z.number().safeParse(exitSpy.mock.calls[0]?.[0])
exitSpy.mockRestore()
return code.success ? code.data : undefined
}
describe('cli auth commands', () => {
let originalArgv: string[]
@@ -53,15 +70,15 @@ describe('cli auth commands', () => {
afterEach(() => {
process.argv = originalArgv
unmockTokenStoreModules()
for (const home of homes.splice(0)) home.restore()
})
it('ocm status reports the store kind and location', async () => {
const { cli, written, restore } = await loadCli({ token: 'tok', store: { kind: 'file', location: '/tmp/credentials.json' } })
const { cli, written, home, restore } = await loadCli({ token: 'tok' })
await cli.cmdStatus()
const out = written.join('')
expect(out).toContain('token store:')
expect(out).toContain('file (/tmp/credentials.json)')
expect(out).toContain(`file (${home.credentialsPath})`)
restore()
})
@@ -78,7 +95,7 @@ describe('cli auth commands', () => {
})
it('ocm status reports the env store', async () => {
const { cli, written, restore } = await loadCli({ env: 'tok_env' })
const { cli, written, restore } = await loadCli({ envToken: 'tok_env' })
await cli.cmdStatus()
const out = written.join('')
expect(out).toContain('env (OCM_TOKEN)')
@@ -86,17 +103,17 @@ describe('cli auth commands', () => {
})
it('ocm status reports the store in the no-state branch', async () => {
const { cli, written, restore } = await loadCli({ state: null, token: null })
const { cli, written, home, restore } = await loadCli({ state: null, token: null })
await cli.cmdStatus()
const out = written.join('')
expect(out).toContain('token store:')
expect(out).toContain('file (/tmp/credentials.json)')
expect(out).toContain(`file (${home.credentialsPath})`)
expect(out).toMatch(/^token:\s+no/m)
restore()
})
it('ocm status reports a token is present in the no-state branch when OCM_TOKEN is active', async () => {
const { cli, written, restore } = await loadCli({ state: null, env: 'tok_env' })
const { cli, written, restore } = await loadCli({ state: null, envToken: 'tok_env' })
await cli.cmdStatus()
const out = written.join('')
expect(out).toContain('token store:')
@@ -106,38 +123,33 @@ describe('cli auth commands', () => {
})
it('ocm status reports an unavailable token store instead of exiting', async () => {
const { cli, written, restore } = await loadCli({
getTokenImpl: () => Promise.reject(new MockTokenStoreError('keychain locked', 'keychain')),
})
const { cli, written, restore } = await loadCli({ documentVersion: 2 })
await cli.cmdStatus()
const out = written.join('')
expect(out).toMatch(/^token:\s+unavailable \(keychain locked\)/m)
expect(out).toMatch(/^token:\s+unavailable \(token file .*unsupported format version 2/m)
expect(out).toContain('manager url:')
restore()
})
it('ocm login stores the token verbatim', async () => {
const { cli, setToken, restore } = await loadCli()
it('ocm login stores the token for the normalized manager url', async () => {
const { cli, storedToken, restore } = await loadCli()
await cli.cmdLogin(['https://manager.example.com/', 'tok_xyz'])
expect(setToken).toHaveBeenCalledWith('https://manager.example.com', 'tok_xyz')
expect(await storedToken()).toBe('tok_xyz')
restore()
})
it('ocm login names the store on success', async () => {
const { cli, written, restore } = await loadCli({ store: { kind: 'file', location: '/tmp/credentials.json' } })
const { cli, written, home, restore } = await loadCli()
await cli.cmdLogin(['https://manager.example.com', 'tok_xyz'])
expect(written.join('')).toContain('Saved token for https://manager.example.com (file: /tmp/credentials.json).')
expect(written.join('')).toContain(`Saved token for ${MANAGER} (file: ${home.credentialsPath}).`)
restore()
})
it('ocm login reports the persisted store and warns that OCM_TOKEN wins', async () => {
const { cli, written, restore } = await loadCli({
env: 'tok_env',
writeTarget: { kind: 'file', location: '/tmp/credentials.json' },
})
const { cli, written, home, restore } = await loadCli({ envToken: 'tok_env' })
await cli.cmdLogin(['https://manager.example.com', 'tok_xyz'])
const out = written.join('')
expect(out).toContain('Saved token for https://manager.example.com (file: /tmp/credentials.json).')
expect(out).toContain(`Saved token for ${MANAGER} (file: ${home.credentialsPath}).`)
expect(out).toContain('note: OCM_TOKEN is set and takes precedence over the stored token')
restore()
})
@@ -150,25 +162,23 @@ describe('cli auth commands', () => {
})
it('ocm login surfaces a store failure', async () => {
const { cli, stderr, restore } = await loadCli({
setTokenImpl: () => Promise.reject(new MockTokenStoreError('disk full')),
})
const exitSpy = vi.spyOn(process, 'exit').mockImplementation(() => { throw new Error('exit') })
await expect(cli.cmdLogin(['https://manager.example.com', 'tok_xyz'])).rejects.toThrow('exit')
expect(stderr.join('')).toContain('token store error: disk full')
expect(exitSpy).toHaveBeenCalledWith(1)
const { cli, stderr, restore } = await loadCli({ documentVersion: 2 })
const code = await exitCodeOnFailure(() => cli.cmdLogin(['https://manager.example.com', 'tok_xyz']))
expect(code).toBe(1)
expect(stderr.join('')).toContain('token store error: token file ')
expect(stderr.join('')).toContain('unsupported format version 2')
restore()
})
const expectLoginUsageError = async (args: string[]) => {
const { cli, stderr, setToken, restore } = await loadCli()
async function expectLoginUsageError(args: string[]): Promise<void> {
const { cli, stderr, storedToken, restore } = await loadCli()
const exitSpy = vi.spyOn(process, 'exit').mockImplementation(() => {
throw new Error('exit')
})
await expect(cli.cmdLogin(args)).rejects.toThrow('exit')
expect(stderr.join('')).toContain('usage: ocm login <url> [token] [--name <name>]')
expect(exitSpy).toHaveBeenCalledWith(1)
expect(setToken).not.toHaveBeenCalled()
expect(await storedToken()).toBeNull()
restore()
}
@@ -191,46 +201,63 @@ describe('cli auth commands', () => {
it('ocm login accepts the token before or after --name', async () => {
const afterName = await loadCli()
await afterName.cli.cmdLogin(['https://manager.example.com', 'tok_xyz', '--name', 'prod'])
expect(afterName.setToken).toHaveBeenCalledWith('https://manager.example.com', 'tok_xyz')
expect(await afterName.storedToken()).toBe('tok_xyz')
afterName.restore()
const beforeName = await loadCli()
await beforeName.cli.cmdLogin(['https://manager.example.com', '--name', 'prod', 'tok_xyz'])
expect(beforeName.setToken).toHaveBeenCalledWith('https://manager.example.com', 'tok_xyz')
expect(await beforeName.storedToken()).toBe('tok_xyz')
beforeName.restore()
})
it('ocm logout reports removal', async () => {
const { cli, written, restore } = await loadCli({ deleteTokenImpl: () => Promise.resolve(true) })
const { cli, written, restore } = await loadCli({ token: 'tok' })
await cli.cmdLogout()
expect(written.join('')).toContain('Removed stored token for')
restore()
})
it('ocm logout reports nothing stored', async () => {
const { cli, written, restore } = await loadCli({ deleteTokenImpl: () => Promise.resolve(false) })
const { cli, written, restore } = await loadCli({ token: null })
await cli.cmdLogout()
expect(written.join('')).toContain('No stored token found.')
restore()
})
it('ocm logout clears state even when the store delete fails', async () => {
const { cli, written, stderr, clearState, restore } = await loadCli({
deleteTokenImpl: () => Promise.reject(new MockTokenStoreError('keychain locked', 'keychain')),
})
const exitSpy = vi.spyOn(process, 'exit').mockImplementation(() => { throw new Error('exit') })
await expect(cli.cmdLogout()).rejects.toThrow('exit')
expect(clearState).toHaveBeenCalledOnce()
const { cli, written, stderr, home, restore } = await loadCli({ documentVersion: 2 })
const code = await exitCodeOnFailure(() => cli.cmdLogout())
expect(code).toBe(1)
expect(written.join('')).toContain('State cleared.')
expect(stderr.join('')).toContain('token store error: keychain locked')
expect(exitSpy).toHaveBeenCalledWith(1)
expect(stderr.join('')).toContain('token store error: token file ')
expect(stderr.join('')).toContain('unsupported format version 2')
vi.resetModules()
const { readState } = await import('../src/state.js')
expect(readState()).toBeNull()
expect(readFileSync(home.statePath, 'utf-8')).toBe('{}')
restore()
})
it('ocm logout warns that OCM_TOKEN still authenticates', async () => {
const { cli, written, restore } = await loadCli({ env: 'tok_env' })
const { cli, written, restore } = await loadCli({ token: 'tok', envToken: 'tok_env' })
await cli.cmdLogout()
expect(written.join('')).toContain('note: OCM_TOKEN is set and takes precedence over the stored token')
restore()
})
})
describe('cli home fixture', () => {
it('writes state and credentials under the temporary home', async () => {
const home = createCliHome({ state: { managerUrl: MANAGER, lastRepoId: 7 }, token: 'tok' })
homes.push(home)
mkdirSync(dirname(home.statePath), { recursive: true })
writeFileSync(home.credentialsPath, JSON.stringify({ version: 1, tokens: { [MANAGER]: 'tok_from_disk' } }))
vi.resetModules()
const { readState } = await import('../src/state.js')
expect(readState()?.lastRepoId).toBe(7)
expect(await createFileTokenStore(home.credentialsPath).get(MANAGER)).toBe('tok_from_disk')
})
})