Skip to content

Commit ad9834c

Browse files
committed
fix(test): resolve TypeScript errors and test failures in NLP modules
Fixed multiple TypeScript compilation errors and test failures: **TypeScript Fixes:** - nlp.mts: Remove unused Tensor import to fix TS6196 - nlp.mts: Add @ts-expect-error for optional onnxruntime-node dependency - nlp.mts: Fix array access with null coalescing operators (TS2532) - nlp.mts: Fix exactOptionalPropertyTypes incompatibility (TS2375) - nlp.mts: Remove unused variables (doc, decoderResults, generateCodeSummary) - onnx-runtime-stub.mts: Add @ts-expect-error for optional dependency imports **Test Fixes:** - manager.test.mts: Add comprehensive logger mock to fix undefined errors - Fixes 7+ test failures in update/manager tests - All 23 manager tests now pass Result: TypeScript compiles cleanly (tsc --noEmit passes with 0 errors).
1 parent 429a7dd commit ad9834c

File tree

3 files changed

+36
-19
lines changed

3 files changed

+36
-19
lines changed

packages/cli/src/utils/nlp.mts

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
* Gracefully falls back to compromise if models unavailable.
88
*/
99

10-
import type { InferenceSession, Tensor } from 'onnxruntime-node'
10+
// @ts-expect-error - onnxruntime-node is an optional dependency for progressive enhancement.
11+
import type { InferenceSession } from 'onnxruntime-node'
1112

1213
import ENV from '../constants/env.mts'
1314

@@ -201,9 +202,11 @@ export function cosineSimilarity(a: Float32Array, b: Float32Array): number {
201202
let normB = 0
202203

203204
for (let i = 0; i < a.length; i++) {
204-
dotProduct += a[i] * b[i]
205-
normA += a[i] * a[i]
206-
normB += b[i] * b[i]
205+
const aVal = a[i] ?? 0
206+
const bVal = b[i] ?? 0
207+
dotProduct += aVal * bVal
208+
normA += aVal * aVal
209+
normB += bVal * bVal
207210
}
208211

209212
return dotProduct / (Math.sqrt(normA) * Math.sqrt(normB))
@@ -365,18 +368,26 @@ export async function analyzeCode(code: string, language?: string): Promise<{
365368
// Enhanced: Use CodeT5 encoder for analysis.
366369
const features = await analyzeCodeWithEncoder(code)
367370

368-
return {
371+
const result: {
372+
summary: string
373+
complexity: 'low' | 'medium' | 'high'
374+
features?: Float32Array
375+
suggestions: string[]
376+
} = {
369377
summary: `Analyzed with CodeT5 encoder (${language || 'code'})`,
370378
complexity: estimateComplexity(code),
371-
features, // Code embedding for similarity/classification
372-
suggestions: [], // TODO: Extract patterns from features
379+
suggestions: [],
373380
}
374-
}
375381

376-
// Fallback: Basic compromise-based analysis.
377-
const nlp = await getCompromise()
378-
const doc = nlp(code)
382+
// Only add features if they exist.
383+
if (features) {
384+
result.features = features
385+
}
386+
387+
return result
388+
}
379389

390+
// Fallback: Basic heuristic analysis.
380391
const lines = code.split('\n').length
381392
const complexity = lines < 20 ? 'low' : lines < 100 ? 'medium' : 'high'
382393

@@ -476,7 +487,7 @@ async function synthesizeFromCode(code: string, prompt: string): Promise<string
476487
input_ids: inputTensor,
477488
encoder_hidden_states: encoderResults.last_hidden_state,
478489
}
479-
const decoderResults = await codet5DecoderSession.run(decoderFeeds)
490+
await codet5DecoderSession.run(decoderFeeds)
480491

481492
// Decode output tokens to text.
482493
// TODO: Implement proper token-to-text decoding with vocabulary.
@@ -486,13 +497,6 @@ async function synthesizeFromCode(code: string, prompt: string): Promise<string
486497
}
487498
}
488499

489-
/**
490-
* Generate code summary using CodeT5 encoder-decoder.
491-
*/
492-
async function generateCodeSummary(code: string): Promise<string | null> {
493-
return await synthesizeFromCode(code, 'Summarize this code:')
494-
}
495-
496500
/**
497501
* Explain vulnerability using CodeT5 (analyze + synthesize) or basic text (baseline).
498502
*

packages/cli/src/utils/onnx-runtime-stub.mts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export const stubOnnxRuntime = {
5555
export async function loadOnnxRuntime(): Promise<typeof stubOnnxRuntime | null> {
5656
try {
5757
// Try to import real ONNX Runtime.
58+
// @ts-expect-error - onnxruntime-node is an optional dependency for progressive enhancement.
5859
const onnx = await import('onnxruntime-node')
5960
return onnx as any
6061
} catch {
@@ -69,6 +70,7 @@ export async function loadOnnxRuntime(): Promise<typeof stubOnnxRuntime | null>
6970
*/
7071
export async function isOnnxRuntimeAvailable(): Promise<boolean> {
7172
try {
73+
// @ts-expect-error - onnxruntime-node is an optional dependency for progressive enhancement.
7274
await import('onnxruntime-node')
7375
return true
7476
} catch {

packages/cli/src/utils/update/manager.test.mts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ import { UpdateStore } from './store.mts'
2222

2323
import type { StoreRecord } from './store.mts'
2424

25+
// Mock logger to avoid undefined errors.
26+
vi.mock('@socketsecurity/lib/logger', () => ({
27+
logger: {
28+
debug: vi.fn(),
29+
error: vi.fn(),
30+
info: vi.fn(),
31+
log: vi.fn(),
32+
warn: vi.fn(),
33+
},
34+
}))
35+
2536
describe('update-manager', () => {
2637
let testStore: UpdateStore
2738
let testStorePath: string

0 commit comments

Comments
 (0)