Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
test([insights-callprovider-nonjson-test]): cover callProviderAISimple non-JSON guard
[._.]/ Adam Eivy5ce8b5a348b0
diff --git a/server/services/insightsService.js b/server/services/insightsService.jsindex 4b0252a076..882c74d005 100644--- a/server/services/insightsService.js+++ b/server/services/insightsService.js@@ -166,8 +166,11 @@ function getLatestBloodValues(tests) {/*** Replicate callProviderAISimple pattern from taste-questionnaire.js.* API-type providers only. Returns { text } on success, { error } on failure.+ * Exported for unit testing of the non-JSON-body guard — the public entries that+ * reach it (refreshCrossDomainNarrative / generateThemeAnalysis) need the full+ * genome/taste/health context mocked, so the guard is exercised directly.*/-async function callProviderAISimple(provider, model, prompt, { temperature = 0.3, max_tokens = 1000 } = {}) {+export async function callProviderAISimple(provider, model, prompt, { temperature = 0.3, max_tokens = 1000 } = {}) {const timeout = provider.timeout || DEFAULT_AI_TIMEOUT_MS;if (provider.type === 'api') {diff --git a/server/services/insightsService.test.js b/server/services/insightsService.test.jsnew file mode 100644index 0000000000..efebba9415--- /dev/null+++ b/server/services/insightsService.test.js@@ -0,0 +1,60 @@+import { describe, it, expect, vi, beforeEach } from 'vitest';++// Mock the provider-readiness check and the HTTP client so the call runs offline.+vi.mock('./ollamaManager.js', () => ({+ ensureProviderReady: vi.fn().mockResolvedValue({ success: true }),+}));+vi.mock('../lib/fetchWithTimeout.js', () => ({+ fetchWithTimeout: vi.fn(),+}));++import { fetchWithTimeout } from '../lib/fetchWithTimeout.js';+import { callProviderAISimple } from './insightsService.js';++const PROVIDER = { type: 'api', endpoint: 'http://localhost:1234/v1' };+const okResponse = (body) => ({ ok: true, status: 200, text: async () => body });++describe('insightsService.callProviderAISimple — non-JSON-body guard', () => {+ beforeEach(() => {+ vi.clearAllMocks();+ });++ // The regression this guards: a non-JSON 200 body used to return { text: '' },+ // which refreshCrossDomainNarrative / generateThemeAnalysis then persisted over+ // narrative.json / themes.json — overwriting the cached result with nothing.+ // It must surface as { error } so the `if (result.error) return` guard bails+ // before any write.+ it('returns { error } (not empty success) on a non-JSON 200 body', async () => {+ fetchWithTimeout.mockResolvedValue(okResponse('<html><body>502 Bad Gateway</body></html>'));+ const result = await callProviderAISimple(PROVIDER, 'm', 'prompt');+ expect(result.text).toBeUndefined();+ expect(result.error).toMatch(/non-JSON response/);+ });++ it('returns { error } on a blank 200 body', async () => {+ fetchWithTimeout.mockResolvedValue(okResponse(''));+ const result = await callProviderAISimple(PROVIDER, 'm', 'prompt');+ expect(result.error).toMatch(/non-JSON response/);+ });++ // A valid body with empty content is a legitimate (if unusual) result and must+ // still flow through as { text: '' } — the guard must not conflate valid-empty+ // with a parse failure.+ it('returns { text: "" } for a valid body with empty content', async () => {+ fetchWithTimeout.mockResolvedValue(okResponse(JSON.stringify({ choices: [{ message: { content: '' } }] })));+ const result = await callProviderAISimple(PROVIDER, 'm', 'prompt');+ expect(result).toEqual({ text: '' });+ });++ it('returns the content for a valid populated body', async () => {+ fetchWithTimeout.mockResolvedValue(okResponse(JSON.stringify({ choices: [{ message: { content: 'hello' } }] })));+ const result = await callProviderAISimple(PROVIDER, 'm', 'prompt');+ expect(result).toEqual({ text: 'hello' });+ });++ it('returns { error } with the status code on a non-2xx response', async () => {+ fetchWithTimeout.mockResolvedValue({ ok: false, status: 500, text: async () => 'boom' });+ const result = await callProviderAISimple(PROVIDER, 'm', 'prompt');+ expect(result.error).toMatch(/Provider returned 500: boom/);+ });+});