mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
chore(lint): replace ESLint with Oxlint and clear anti-slop findings
This commit is contained in:
1 parent
c6c13335e5
commit
b625acfc0c
1083 files changed
+67089
-80136
No files matched your search
@@ -1,9 +1,12 @@
|
||||
import { describe, it, expect, vi, afterEach } from 'vitest'
|
||||
import { JsonObjectSchema, type JsonValue } from '@opencode-manager/shared/utils'
|
||||
import { ManagerApi, ManagerApiError, isManagerRouteMissing, MANAGER_FEATURE_MISSING } from '../src/manager-api.js'
|
||||
import type { MultiRun } from '@opencode-manager/shared/schemas'
|
||||
|
||||
const BASE_URL = 'http://localhost:5003'
|
||||
|
||||
type FetchImplementation = (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>
|
||||
|
||||
const run: MultiRun = {
|
||||
id: 3,
|
||||
repoId: 1,
|
||||
@@ -18,20 +21,35 @@ const run: MultiRun = {
|
||||
fusions: [],
|
||||
}
|
||||
|
||||
function okResponse(body: unknown) {
|
||||
return { ok: true, status: 200, json: () => Promise.resolve(body), text: () => Promise.resolve('') }
|
||||
function okResponse(body: JsonValue): Response {
|
||||
return Response.json(body)
|
||||
}
|
||||
|
||||
function errorResponse(status: number, text: string) {
|
||||
return { ok: false, status, json: () => Promise.reject(new Error('no json')), text: () => Promise.resolve(text) }
|
||||
function errorResponse(status: number, text: string): Response {
|
||||
return new Response(text, { status })
|
||||
}
|
||||
|
||||
function stubFetch(response: unknown) {
|
||||
const fetchMock = vi.fn().mockResolvedValue(response)
|
||||
function stubFetch(respond: FetchImplementation) {
|
||||
const fetchMock = vi.fn<FetchImplementation>(respond)
|
||||
vi.stubGlobal('fetch', fetchMock)
|
||||
return fetchMock
|
||||
}
|
||||
|
||||
function stubSingleResponse(response: Response) {
|
||||
return stubFetch(async () => response)
|
||||
}
|
||||
|
||||
async function expectApiError(request: Promise<unknown>): Promise<ManagerApiError> {
|
||||
const failure = await request.then(() => null, (cause) => cause)
|
||||
expect(failure).toBeInstanceOf(ManagerApiError)
|
||||
if (!(failure instanceof ManagerApiError)) throw new Error('expected a ManagerApiError')
|
||||
return failure
|
||||
}
|
||||
|
||||
function errorBody(code: string | null, text: string): JsonValue {
|
||||
return code === null ? { error: text } : { error: text, code }
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals()
|
||||
})
|
||||
@@ -40,7 +58,7 @@ describe('ManagerApi multi-runs', () => {
|
||||
const api = new ManagerApi(BASE_URL, 'tok')
|
||||
|
||||
it('lists runs for a repo', async () => {
|
||||
const fetchMock = stubFetch(okResponse({ runs: [run] }))
|
||||
const fetchMock = stubSingleResponse(okResponse({ runs: [run] }))
|
||||
|
||||
await expect(api.listMultiRuns(1)).resolves.toEqual([run])
|
||||
expect(fetchMock).toHaveBeenCalledWith(`${BASE_URL}/api/internal/multi-runs?repoId=1`, {
|
||||
@@ -49,7 +67,7 @@ describe('ManagerApi multi-runs', () => {
|
||||
})
|
||||
|
||||
it('launches a run', async () => {
|
||||
const fetchMock = stubFetch(okResponse({ run }))
|
||||
const fetchMock = stubSingleResponse(okResponse({ run }))
|
||||
const request = { repoId: 1, name: 'sweep', prompt: 'hello', models: ['a/b'], isolate: true }
|
||||
|
||||
await expect(api.launchMultiRun(request)).resolves.toEqual(run)
|
||||
@@ -61,7 +79,7 @@ describe('ManagerApi multi-runs', () => {
|
||||
})
|
||||
|
||||
it('fuses selected entries', async () => {
|
||||
const fetchMock = stubFetch(okResponse({ run }))
|
||||
const fetchMock = stubSingleResponse(okResponse({ run }))
|
||||
const request = { requestId: '11111111-1111-4111-8111-111111111111', entryIds: [1, 2], model: 'a/b' }
|
||||
|
||||
await expect(api.fuseMultiRun(3, request)).resolves.toEqual(run)
|
||||
@@ -73,7 +91,7 @@ describe('ManagerApi multi-runs', () => {
|
||||
})
|
||||
|
||||
it('discards an entry', async () => {
|
||||
const fetchMock = stubFetch(okResponse({ run }))
|
||||
const fetchMock = stubSingleResponse(okResponse({ run }))
|
||||
|
||||
await expect(api.discardMultiRunEntry(3, 4)).resolves.toEqual(run)
|
||||
expect(fetchMock).toHaveBeenCalledWith(`${BASE_URL}/api/internal/multi-runs/3/entries/4/discard`, {
|
||||
@@ -85,44 +103,44 @@ describe('ManagerApi multi-runs', () => {
|
||||
|
||||
describe('ManagerApiError', () => {
|
||||
const api = new ManagerApi(BASE_URL, 'tok')
|
||||
const fusion = { requestId: '11111111-1111-4111-8111-111111111111', entryIds: [1, 2], model: 'a/b' }
|
||||
|
||||
it('reads the machine code from the JSON code field, not the human error text', async () => {
|
||||
const details = { unavailableSources: [{ entryId: 1, model: 'a/b', reason: 'running', message: 'still running' }] }
|
||||
stubFetch(errorResponse(409, JSON.stringify({ error: 'Fusion unavailable', code: 'FUSION_UNAVAILABLE', details })))
|
||||
stubSingleResponse(errorResponse(409, JSON.stringify({ error: 'Fusion unavailable', code: 'FUSION_UNAVAILABLE', details })))
|
||||
|
||||
const error = await api.fuseMultiRun(3, { requestId: '11111111-1111-4111-8111-111111111111', entryIds: [1, 2], model: 'a/b' }).catch((err) => err)
|
||||
const error = await expectApiError(api.fuseMultiRun(3, fusion))
|
||||
|
||||
expect(error).toBeInstanceOf(ManagerApiError)
|
||||
expect((error as ManagerApiError).status).toBe(409)
|
||||
expect((error as ManagerApiError).code).toBe('FUSION_UNAVAILABLE')
|
||||
expect((error as ManagerApiError).message).toContain('Fusion unavailable')
|
||||
expect((error as ManagerApiError).details).toEqual(details)
|
||||
expect((error as ManagerApiError).jsonBody).toBe(true)
|
||||
expect(error.status).toBe(409)
|
||||
expect(error.code).toBe('FUSION_UNAVAILABLE')
|
||||
expect(error.message).toContain('Fusion unavailable')
|
||||
expect(error.details).toEqual(details)
|
||||
expect(error.jsonBody).toBe(true)
|
||||
})
|
||||
|
||||
it('leaves the code null when the body only carries human error text', async () => {
|
||||
stubFetch(errorResponse(409, JSON.stringify({ error: 'Fusion unavailable' })))
|
||||
stubSingleResponse(errorResponse(409, JSON.stringify(errorBody(null, 'Fusion unavailable'))))
|
||||
|
||||
const error = await api.fuseMultiRun(3, { requestId: '11111111-1111-4111-8111-111111111111', entryIds: [1, 2], model: 'a/b' }).catch((err) => err)
|
||||
const error = await expectApiError(api.fuseMultiRun(3, fusion))
|
||||
|
||||
expect((error as ManagerApiError).code).toBeNull()
|
||||
expect(error.code).toBeNull()
|
||||
})
|
||||
|
||||
it('leaves details null when the error body has none', async () => {
|
||||
stubFetch(errorResponse(400, JSON.stringify({ error: 'Invalid request' })))
|
||||
stubSingleResponse(errorResponse(400, JSON.stringify(errorBody(null, 'Invalid request'))))
|
||||
|
||||
const error = await api.listMultiRuns(1).catch((err) => err)
|
||||
const error = await expectApiError(api.listMultiRuns(1))
|
||||
|
||||
expect((error as ManagerApiError).details).toBeNull()
|
||||
expect(error.details).toBeNull()
|
||||
})
|
||||
|
||||
it('marks a plain-text error body as non-JSON', async () => {
|
||||
stubFetch(errorResponse(500, 'boom'))
|
||||
stubSingleResponse(errorResponse(500, 'boom'))
|
||||
|
||||
const error = await api.listMultiRuns(1).catch((err) => err)
|
||||
const error = await expectApiError(api.listMultiRuns(1))
|
||||
|
||||
expect((error as ManagerApiError).jsonBody).toBe(false)
|
||||
expect((error as ManagerApiError).code).toBeNull()
|
||||
expect(error.jsonBody).toBe(false)
|
||||
expect(error.code).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -130,16 +148,16 @@ describe('ManagerApi feature support', () => {
|
||||
const api = new ManagerApi(BASE_URL, 'tok')
|
||||
|
||||
it('maps a 401 to MANAGER_FEATURE_MISSING when the token probe succeeds', async () => {
|
||||
const fetchMock = vi.fn()
|
||||
.mockResolvedValueOnce(errorResponse(401, JSON.stringify({ error: 'Unauthorized' })))
|
||||
.mockResolvedValueOnce(okResponse({ workspaces: [] }))
|
||||
vi.stubGlobal('fetch', fetchMock)
|
||||
const fetchMock = stubFetch(async (input) =>
|
||||
String(input).endsWith('/api/internal/opencode-workspaces')
|
||||
? okResponse({ workspaces: [] })
|
||||
: errorResponse(401, JSON.stringify(errorBody(null, 'Unauthorized'))),
|
||||
)
|
||||
|
||||
const error = await api.listMultiRuns(1).catch((err) => err)
|
||||
const error = await expectApiError(api.listMultiRuns(1))
|
||||
|
||||
expect(error).toBeInstanceOf(ManagerApiError)
|
||||
expect((error as ManagerApiError).status).toBe(401)
|
||||
expect((error as ManagerApiError).code).toBe(MANAGER_FEATURE_MISSING)
|
||||
expect(error.status).toBe(401)
|
||||
expect(error.code).toBe(MANAGER_FEATURE_MISSING)
|
||||
expect(isManagerRouteMissing(error)).toBe(true)
|
||||
expect(fetchMock).toHaveBeenNthCalledWith(2, `${BASE_URL}/api/internal/opencode-workspaces`, {
|
||||
headers: { Authorization: 'Bearer tok' },
|
||||
@@ -147,31 +165,27 @@ describe('ManagerApi feature support', () => {
|
||||
})
|
||||
|
||||
it('keeps the plain 401 when the token probe is also rejected', async () => {
|
||||
const fetchMock = vi.fn()
|
||||
.mockResolvedValueOnce(errorResponse(401, JSON.stringify({ error: 'Unauthorized' })))
|
||||
.mockResolvedValueOnce(errorResponse(401, JSON.stringify({ error: 'Unauthorized' })))
|
||||
vi.stubGlobal('fetch', fetchMock)
|
||||
stubFetch(async () => errorResponse(401, JSON.stringify(errorBody(null, 'Unauthorized'))))
|
||||
|
||||
const error = await api.listMultiRuns(1).catch((err) => err)
|
||||
const error = await expectApiError(api.listMultiRuns(1))
|
||||
|
||||
expect(error).toBeInstanceOf(ManagerApiError)
|
||||
expect((error as ManagerApiError).status).toBe(401)
|
||||
expect((error as ManagerApiError).code).toBeNull()
|
||||
expect(error.status).toBe(401)
|
||||
expect(error.code).toBeNull()
|
||||
expect(isManagerRouteMissing(error)).toBe(false)
|
||||
})
|
||||
|
||||
it('keeps the plain 401 when the token probe throws', async () => {
|
||||
const fetchMock = vi.fn()
|
||||
.mockResolvedValueOnce(errorResponse(401, JSON.stringify({ error: 'Unauthorized' })))
|
||||
.mockRejectedValueOnce(new Error('network down'))
|
||||
vi.stubGlobal('fetch', fetchMock)
|
||||
const fetchMock = stubFetch(async (input) => {
|
||||
if (String(input).endsWith('/api/internal/opencode-workspaces')) throw new Error('network down')
|
||||
return errorResponse(401, JSON.stringify(errorBody(null, 'Unauthorized')))
|
||||
})
|
||||
|
||||
const error = await api.listMultiRuns(1).catch((err) => err)
|
||||
const error = await expectApiError(api.listMultiRuns(1))
|
||||
|
||||
expect(error).toBeInstanceOf(ManagerApiError)
|
||||
expect((error as ManagerApiError).status).toBe(401)
|
||||
expect((error as ManagerApiError).code).toBeNull()
|
||||
expect(error.status).toBe(401)
|
||||
expect(error.code).toBeNull()
|
||||
expect(isManagerRouteMissing(error)).toBe(false)
|
||||
expect(fetchMock).toHaveBeenCalledTimes(2)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -179,16 +193,16 @@ describe('ManagerApi mirror routes', () => {
|
||||
const api = new ManagerApi(BASE_URL, 'tok')
|
||||
|
||||
it('maps a 401 from mirrorCheckouts to MANAGER_FEATURE_MISSING with the move message', async () => {
|
||||
const fetchMock = vi.fn()
|
||||
.mockResolvedValueOnce(errorResponse(401, JSON.stringify({ error: 'Unauthorized' })))
|
||||
.mockResolvedValueOnce(okResponse({ workspaces: [] }))
|
||||
vi.stubGlobal('fetch', fetchMock)
|
||||
const fetchMock = stubFetch(async (input) =>
|
||||
String(input).endsWith('/api/internal/opencode-workspaces')
|
||||
? okResponse({ workspaces: [] })
|
||||
: errorResponse(401, JSON.stringify(errorBody(null, 'Unauthorized'))),
|
||||
)
|
||||
|
||||
const error = await api.mirrorCheckouts(1, 'feature').catch((err) => err)
|
||||
const error = await expectApiError(api.mirrorCheckouts(1, 'feature'))
|
||||
|
||||
expect(error).toBeInstanceOf(ManagerApiError)
|
||||
expect((error as ManagerApiError).code).toBe(MANAGER_FEATURE_MISSING)
|
||||
expect((error as ManagerApiError).message).toContain('too old for /ocm-move worktrees')
|
||||
expect(error.code).toBe(MANAGER_FEATURE_MISSING)
|
||||
expect(error.message).toContain('too old for /ocm-move worktrees')
|
||||
expect(isManagerRouteMissing(error)).toBe(true)
|
||||
expect(fetchMock).toHaveBeenNthCalledWith(2, `${BASE_URL}/api/internal/opencode-workspaces`, {
|
||||
headers: { Authorization: 'Bearer tok' },
|
||||
@@ -196,20 +210,22 @@ describe('ManagerApi mirror routes', () => {
|
||||
})
|
||||
|
||||
it('keeps the multi-run feature message unchanged', async () => {
|
||||
const fetchMock = vi.fn()
|
||||
.mockResolvedValueOnce(errorResponse(401, JSON.stringify({ error: 'Unauthorized' })))
|
||||
.mockResolvedValueOnce(okResponse({ workspaces: [] }))
|
||||
vi.stubGlobal('fetch', fetchMock)
|
||||
stubFetch(async (input) =>
|
||||
String(input).endsWith('/api/internal/opencode-workspaces')
|
||||
? okResponse({ workspaces: [] })
|
||||
: errorResponse(401, JSON.stringify(errorBody(null, 'Unauthorized'))),
|
||||
)
|
||||
|
||||
const error = await api.listMultiRuns(1).catch((err) => err)
|
||||
const error = await expectApiError(api.listMultiRuns(1))
|
||||
|
||||
expect((error as ManagerApiError).code).toBe(MANAGER_FEATURE_MISSING)
|
||||
expect((error as ManagerApiError).message).toContain('too old for ocm multi-runs')
|
||||
expect(error.code).toBe(MANAGER_FEATURE_MISSING)
|
||||
expect(error.message).toContain('too old for ocm multi-runs')
|
||||
})
|
||||
|
||||
it('sends the directory query on mirrorHead and mirrorContains', async () => {
|
||||
const fetchMock = vi.fn().mockResolvedValue(okResponse({}))
|
||||
vi.stubGlobal('fetch', fetchMock)
|
||||
const fetchMock = stubFetch(async () =>
|
||||
okResponse({ repoId: 1, branch: null, head: null, dirty: false, contained: false }),
|
||||
)
|
||||
|
||||
await api.mirrorHead(1, '/repos/work trees/repo')
|
||||
await api.mirrorContains(1, 'abc1234', '/repos/work trees/repo')
|
||||
@@ -217,23 +233,39 @@ describe('ManagerApi mirror routes', () => {
|
||||
expect(new URL(String(fetchMock.mock.calls[0]![0])).searchParams.get('directory')).toBe('/repos/work trees/repo')
|
||||
expect(new URL(String(fetchMock.mock.calls[1]![0])).searchParams.get('directory')).toBe('/repos/work trees/repo')
|
||||
})
|
||||
|
||||
it('parses a checkouts response of typed checkout states', async () => {
|
||||
const checkouts = {
|
||||
main: { directory: '/repos/repo', branch: 'main', head: 'abc', dirty: false },
|
||||
worktrees: [{ directory: '/repos/repo-feature', branch: 'feature', head: 'def', dirty: false }],
|
||||
branchHead: 'def',
|
||||
branchCheckedOut: true,
|
||||
suffixedWorktreeBranch: 'feature-ocm',
|
||||
}
|
||||
stubSingleResponse(okResponse(checkouts))
|
||||
|
||||
const parsed = await api.mirrorCheckouts(1, 'feature')
|
||||
|
||||
expect(parsed).toEqual(checkouts)
|
||||
expect(JsonObjectSchema.safeParse(checkouts).success).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('isManagerRouteMissing', () => {
|
||||
const api = new ManagerApi(BASE_URL, 'tok')
|
||||
|
||||
it('is true for a 404 with a non-JSON body', async () => {
|
||||
stubFetch(errorResponse(404, '404 Not Found'))
|
||||
stubSingleResponse(errorResponse(404, '404 Not Found'))
|
||||
|
||||
const error = await api.listMultiRuns(1).catch((err) => err)
|
||||
const error = await expectApiError(api.listMultiRuns(1))
|
||||
|
||||
expect(isManagerRouteMissing(error)).toBe(true)
|
||||
})
|
||||
|
||||
it('is false for a 404 with a JSON error body', async () => {
|
||||
stubFetch(errorResponse(404, JSON.stringify({ error: 'Multi-run not found' })))
|
||||
stubSingleResponse(errorResponse(404, JSON.stringify(errorBody(null, 'Multi-run not found'))))
|
||||
|
||||
const error = await api.listMultiRuns(1).catch((err) => err)
|
||||
const error = await expectApiError(api.listMultiRuns(1))
|
||||
|
||||
expect(isManagerRouteMissing(error)).toBe(false)
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user