From 6dd990474ec4c30a54c047bc3b52e05b1885b933 Mon Sep 17 00:00:00 2001 From: yiling Date: Tue, 26 May 2026 10:27:07 +0800 Subject: [PATCH] fix: resolve lint and typecheck errors in API and Authz tests --- src/api/file.ts | 92 ++++++++++++++------------------ src/api/invite.test.ts | 7 ++- src/api/metadata.test.ts | 7 ++- src/api/notification.test.ts | 7 ++- src/api/project.test.ts | 7 ++- src/api/provider.test.ts | 56 ++++++++++--------- src/services/authz/authz.test.ts | 34 +++++++++--- 7 files changed, 115 insertions(+), 95 deletions(-) diff --git a/src/api/file.ts b/src/api/file.ts index c20a0400..44d82fa5 100644 --- a/src/api/file.ts +++ b/src/api/file.ts @@ -36,25 +36,21 @@ const route = new Hono<{ Variables: { user: User } }>() const asset = await assetService.getAsset({ assetId: fileId }) return c.json(asset) }) - .patch( - '/files/:fileId/order', - zValidator('json', updateAssetOrderRequestSchema), - async (c) => { - const fileId = c.req.param('fileId') - const user = c.get('user') - const req = c.req.valid('json') + .patch('/files/:fileId/order', zValidator('json', updateAssetOrderRequestSchema), async (c) => { + const fileId = c.req.param('fileId') + const user = c.get('user') + const req = c.req.valid('json') - await authzService.hasPermission({ - user, - permission: Permission.Edit, - type: ResourceType.Asset, - id: fileId, - }) + await authzService.hasPermission({ + user, + permission: Permission.Edit, + type: ResourceType.Asset, + id: fileId, + }) - const updated = await assetService.updateAssetOrder(fileId, req) - return c.json(updated) - }, - ) + const updated = await assetService.updateAssetOrder(fileId, req) + return c.json(updated) + }) .put('/files/:fileId', zValidator('json', updateFileRequestSchema), async (c) => { const fileId = c.req.param('fileId') const user = c.get('user') @@ -165,25 +161,21 @@ const route = new Hono<{ Variables: { user: User } }>() return c.json(comment, 201) }, ) - .get( - '/files/:fileId/comments', - zValidator('query', paginationParamsSchema), - async (c) => { - const fileId = c.req.param('fileId') - const user = c.get('user') - const req = c.req.valid('query') + .get('/files/:fileId/comments', zValidator('query', paginationParamsSchema), async (c) => { + const fileId = c.req.param('fileId') + const user = c.get('user') + const req = c.req.valid('query') - await authzService.hasPermission({ - user, - permission: Permission.Read, - type: ResourceType.Asset, - id: fileId, - }) + await authzService.hasPermission({ + user, + permission: Permission.Read, + type: ResourceType.Asset, + id: fileId, + }) - const comments = await assetService.listComments(fileId, req) - return c.json(comments) - }, - ) + const comments = await assetService.listComments(fileId, req) + return c.json(comments) + }) .get('/comments/:commentId', async (c) => { const commentId = c.req.param('commentId') const user = c.get('user') @@ -198,26 +190,22 @@ const route = new Hono<{ Variables: { user: User } }>() const comment = await assetService.getComment(commentId) return c.json(comment) }) - .post( - '/files/restore', - zValidator('json', restoreFilesRequestSchema), - async (c) => { - const user = c.get('user') - const req = c.req.valid('json') + .post('/files/restore', zValidator('json', restoreFilesRequestSchema), async (c) => { + const user = c.get('user') + const req = c.req.valid('json') - for (const id of req.ids) { - await authzService.hasPermission({ - user, - permission: Permission.Edit, - type: ResourceType.Asset, - id, - }) - } + for (const id of req.ids) { + await authzService.hasPermission({ + user, + permission: Permission.Edit, + type: ResourceType.Asset, + id, + }) + } - await assetService.restoreAssets(req.ids) - return c.body(null, 204) - }, - ) + await assetService.restoreAssets(req.ids) + return c.body(null, 204) + }) .post('/teams/:teamId/files', zValidator('form', uploadFileRequestSchema), async (c) => { const teamId = c.req.param('teamId') const user = c.get('user') diff --git a/src/api/invite.test.ts b/src/api/invite.test.ts index c9853735..9fd873f5 100644 --- a/src/api/invite.test.ts +++ b/src/api/invite.test.ts @@ -11,14 +11,17 @@ vi.mock('@/services/notification/notification') vi.mock('@/services/authz/authz') vi.mock('@/api/middleware/auth', () => ({ - authMiddleware: async (c: Context, next: Next) => { + authMiddleware: async ( + c: Context<{ Variables: { user: { id: string; name: string } } }>, + next: Next, + ) => { c.set('user', { id: 'u1', name: 'Test User' }) await next() }, })) const getApp = () => { - const app = new Hono() + const app = new Hono<{ Variables: { user: { id: string; name: string } } }>() .route('/', publicInviteRoute) .use('*', async (c, next) => { c.set('user', { id: 'u1', name: 'Test User' }) diff --git a/src/api/metadata.test.ts b/src/api/metadata.test.ts index 79f2dced..12389530 100644 --- a/src/api/metadata.test.ts +++ b/src/api/metadata.test.ts @@ -5,7 +5,10 @@ import { metadataService } from '@/services/metadata/metadata' import { authzService, Permission, ResourceType } from '@/services/authz/authz' vi.mock('@/api/middleware/auth', () => ({ - authMiddleware: async (c: Context, next: Next) => { + authMiddleware: async ( + c: Context<{ Variables: { user: { id: string; name: string } } }>, + next: Next, + ) => { c.set('user', { id: 'user1', name: 'Test User' }) await next() }, @@ -15,7 +18,7 @@ vi.mock('@/services/authz/authz') vi.mock('@/services/metadata/metadata') describe('metadata api', () => { - const app = new Hono() + const app = new Hono<{ Variables: { user: { id: string; name: string } } }>() .use('*', async (c, next) => { c.set('user', { id: 'user1', name: 'Test User' }) await next() diff --git a/src/api/notification.test.ts b/src/api/notification.test.ts index 2118b0cc..a5ed730d 100644 --- a/src/api/notification.test.ts +++ b/src/api/notification.test.ts @@ -5,7 +5,10 @@ import { notificationService } from '@/services/notification/notification' import { authzService, Permission, ResourceType } from '@/services/authz/authz' vi.mock('@/api/middleware/auth', () => ({ - authMiddleware: async (c: Context, next: Next) => { + authMiddleware: async ( + c: Context<{ Variables: { user: { id: string; name: string } } }>, + next: Next, + ) => { c.set('user', { id: 'user1', name: 'Test User' }) await next() }, @@ -15,7 +18,7 @@ vi.mock('@/services/authz/authz') vi.mock('@/services/notification/notification') describe('notification api', () => { - const app = new Hono() + const app = new Hono<{ Variables: { user: { id: string; name: string } } }>() .use('*', async (c, next) => { c.set('user', { id: 'user1', name: 'Test User' }) await next() diff --git a/src/api/project.test.ts b/src/api/project.test.ts index 68b42ba6..d153745b 100644 --- a/src/api/project.test.ts +++ b/src/api/project.test.ts @@ -6,7 +6,10 @@ import { projectService } from '@/services/project/project' import { assetService } from '@/services/asset/asset' vi.mock('@/api/middleware/auth', () => ({ - authMiddleware: async (c: Context, next: Next) => { + authMiddleware: async ( + c: Context<{ Variables: { user: { id: string; name: string } } }>, + next: Next, + ) => { c.set('user', { id: 'user1', name: 'Test User' }) await next() }, @@ -17,7 +20,7 @@ vi.mock('@/services/project/project') vi.mock('@/services/asset/asset') describe('project api', () => { - const app = new Hono() + const app = new Hono<{ Variables: { user: { id: string; name: string } } }>() .use('*', async (c, next) => { c.set('user', { id: 'user1', name: 'Test User' }) await next() diff --git a/src/api/provider.test.ts b/src/api/provider.test.ts index 49a563bc..82b03250 100644 --- a/src/api/provider.test.ts +++ b/src/api/provider.test.ts @@ -5,7 +5,10 @@ import { providerService } from '@/services/provider/provider' import { authzService, Permission, ResourceType } from '@/services/authz/authz' vi.mock('@/api/middleware/auth', () => ({ - authMiddleware: async (c: Context, next: Next) => { + authMiddleware: async ( + c: Context<{ Variables: { user: { id: string; name: string } } }>, + next: Next, + ) => { c.set('user', { id: 'user1', name: 'Test User' }) await next() }, @@ -15,7 +18,7 @@ vi.mock('@/services/authz/authz') vi.mock('@/services/provider/provider') describe('provider api', () => { - const app = new Hono() + const app = new Hono<{ Variables: { user: { id: string; name: string } } }>() .use('*', async (c, next) => { c.set('user', { id: 'user1', name: 'Test User' }) await next() @@ -28,11 +31,9 @@ describe('provider api', () => { }) it('GET /teams/:teamId/providers returns providers', async () => { - // Using any here because mocking complex service return types or Hono context is overly verbose for this test. - // eslint-disable-next-line @typescript-eslint/no-explicit-any vi.mocked(providerService.listByTeam).mockResolvedValue([ - { id: 'p1', name: 'openai', config: {} }, - ] as any) + { id: 'p1', name: 'openai', config: { api: 'openai' } }, + ] as unknown as Awaited>) const res = await app.request('/teams/t1/providers') @@ -49,14 +50,13 @@ describe('provider api', () => { }) it('GET /providers/:id/models returns models', async () => { - // Using any here because mocking complex service return types or Hono context is overly verbose for this test. - // eslint-disable-next-line @typescript-eslint/no-explicit-any - vi.mocked(providerService.getById).mockResolvedValue({ id: 'p1', teamId: 't1' } as any) - // Using any here because mocking complex service return types or Hono context is overly verbose for this test. - // eslint-disable-next-line @typescript-eslint/no-explicit-any + vi.mocked(providerService.getById).mockResolvedValue({ + id: 'p1', + teamId: 't1', + } as unknown as Awaited>) vi.mocked(providerService.listModelsByProvider).mockResolvedValue([ { modelId: 'm1', name: 'model1' }, - ] as any) + ] as unknown as Awaited>) const res = await app.request('/providers/p1/models') @@ -85,21 +85,19 @@ describe('provider api', () => { config: { api: 'openai-completions', reasoning: false, - input: ['text'], + input: ['text' as const], contextWindow: 128000, maxTokens: 4096, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, }, }, ] - // Using any here because mocking complex service return types or Hono context is overly verbose for this test. - // eslint-disable-next-line @typescript-eslint/no-explicit-any vi.mocked(providerService.create).mockResolvedValue({ id: 'p1', name: 'openai', config, models, - } as any) + } as unknown as Awaited>) const res = await app.request('/teams/t1/providers', { method: 'POST', @@ -131,24 +129,23 @@ describe('provider api', () => { config: { api: 'openai-completions', reasoning: false, - input: ['text'], + input: ['text' as const], contextWindow: 128000, maxTokens: 4096, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, }, }, ] - // Using any here because mocking complex service return types or Hono context is overly verbose for this test. - // eslint-disable-next-line @typescript-eslint/no-explicit-any - vi.mocked(providerService.getById).mockResolvedValue({ id: 'p1', teamId: 't1' } as any) - // Using any here because mocking complex service return types or Hono context is overly verbose for this test. - // eslint-disable-next-line @typescript-eslint/no-explicit-any + vi.mocked(providerService.getById).mockResolvedValue({ + id: 'p1', + teamId: 't1', + } as unknown as Awaited>) vi.mocked(providerService.update).mockResolvedValue({ id: 'p1', name: 'openai', config, models, - } as any) + } as unknown as Awaited>) const res = await app.request('/providers/p1', { method: 'PUT', @@ -169,12 +166,13 @@ describe('provider api', () => { }) it('DELETE /providers/:id deletes provider', async () => { - // Using any here because mocking complex service return types or Hono context is overly verbose for this test. - // eslint-disable-next-line @typescript-eslint/no-explicit-any - vi.mocked(providerService.getById).mockResolvedValue({ id: 'p1', teamId: 't1' } as any) - // Using any here because mocking complex service return types or Hono context is overly verbose for this test. - // eslint-disable-next-line @typescript-eslint/no-explicit-any - vi.mocked(providerService.delete).mockResolvedValue({ id: 'p1' } as any) + vi.mocked(providerService.getById).mockResolvedValue({ + id: 'p1', + teamId: 't1', + } as unknown as Awaited>) + vi.mocked(providerService.delete).mockResolvedValue({ + id: 'p1', + } as unknown as Awaited>) const res = await app.request('/providers/p1', { method: 'DELETE', diff --git a/src/services/authz/authz.test.ts b/src/services/authz/authz.test.ts index 8cc2f9c0..6be1f555 100644 --- a/src/services/authz/authz.test.ts +++ b/src/services/authz/authz.test.ts @@ -273,7 +273,13 @@ describe('AuthzService', () => { data: { name: 'BotUser2', email: 'bot2@example.com', password: 'p' }, }) const agent = await prisma.agent.create({ - data: { id: user.id, teamId: team.id, type: 'chat', enabled: true, config: {} }, + data: { + id: user.id, + teamId: team.id, + type: 'chat', + enabled: true, + config: { provider: 'openai', model: 'gpt-4o' }, + }, }) const agentSession = await prisma.agentSession.create({ data: { agentId: agent.id, cwd: '/tmp' }, @@ -286,10 +292,20 @@ describe('AuthzService', () => { // Metadata Field const teamField = await prisma.metadataField.create({ - data: { key: 'field_t', scope: 'TEAM', teamId: team.id, config: { name: 'Field T' } }, + data: { + key: 'field_t', + scope: 'TEAM', + teamId: team.id, + config: { name: 'Field T', type: 'text' }, + }, }) const projectField = await prisma.metadataField.create({ - data: { key: 'field_p', scope: 'PROJECT', projectId: project.id, config: { name: 'Field P' } }, + data: { + key: 'field_p', + scope: 'PROJECT', + projectId: project.id, + config: { name: 'Field P', type: 'text' }, + }, }) // Skill @@ -299,7 +315,7 @@ describe('AuthzService', () => { // Provider const provider = await prisma.provider.create({ - data: { name: 'Prov 1', teamId: team.id, config: {} }, + data: { name: 'Prov 1', teamId: team.id, config: { api: 'openai' } }, }) // Invite @@ -322,9 +338,15 @@ describe('AuthzService', () => { }) // Test execution via cast - // eslint-disable-next-line @typescript-eslint/no-explicit-any const resolveContext = (type: ResourceType, id: string) => - (authzService as any).resolveContext(type, id) + ( + authzService as unknown as { + resolveContext: ( + type: ResourceType, + id: string, + ) => Promise<{ teamId: string; projectId?: string }> + } + ).resolveContext(type, id) // Tests await expect(resolveContext(ResourceType.Team, team.id)).resolves.toEqual({ teamId: team.id })