Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(frontend): authenticate correlation requests

ShiroKSH92e07677e25c
diff --git a/frontend/src/lib/__tests__/api.test.ts b/frontend/src/lib/__tests__/api.test.ts
index faa168536..3ae729133 100644
--- a/frontend/src/lib/__tests__/api.test.ts
+++ b/frontend/src/lib/__tests__/api.test.ts
@@ -39,4 +39,32 @@ describe("api request helper", () => {
message: expect.stringContaining("Expected JSON from /channels/status, got text/html"),
} satisfies Partial<ApiError>);
});
+
+ it("sends the stored API key when fetching a correlation matrix", async () => {
+ vi.stubGlobal("localStorage", {
+ getItem: vi.fn(() => "remote-test-key"),
+ setItem: vi.fn(),
+ removeItem: vi.fn(),
+ });
+ const fetchMock = vi.fn().mockResolvedValue(
+ new Response(JSON.stringify({ labels: ["A", "B"], matrix: [[1, 0], [0, 1]] }), {
+ status: 200,
+ headers: { "content-type": "application/json" },
+ }),
+ );
+ vi.stubGlobal("fetch", fetchMock);
+
+ const { api } = await loadApiModule();
+
+ await expect(api.getCorrelation("A,B", 90, "pearson")).resolves.toEqual({
+ labels: ["A", "B"],
+ matrix: [[1, 0], [0, 1]],
+ });
+ expect(fetchMock).toHaveBeenCalledWith(
+ "/correlation?codes=A%2CB&days=90&method=pearson",
+ expect.objectContaining({
+ headers: expect.objectContaining({ Authorization: "Bearer remote-test-key" }),
+ }),
+ );
+ });
});
diff --git a/frontend/src/lib/api.ts b/frontend/src/lib/api.ts
index 13ea4aab7..4f276db07 100644
--- a/frontend/src/lib/api.ts
+++ b/frontend/src/lib/api.ts
@@ -19,6 +19,11 @@ export function isAuthRequiredError(error: unknown): boolean {
return error instanceof ApiError && (error.status === 401 || error.status === 403);
}
+export interface CorrelationResponse {
+ labels: string[];
+ matrix: number[][];
+}
+
async function errorFromResponse(res: Response): Promise<ApiError> {
let detail = `HTTP ${res.status}`;
try {
@@ -84,6 +89,10 @@ function appendQueryParam(url: string, key: string, value: string): string {
export const api = {
uploadFile,
+ getCorrelation: (codes: string, days: number, method: "pearson" | "spearman") =>
+ request<CorrelationResponse>(
+ `/correlation?codes=${encodeURIComponent(codes)}&days=${encodeURIComponent(String(days))}&method=${encodeURIComponent(method)}`,
+ ),
listRuns: (limit?: number) => request<RunListItem[]>(`/runs${limit ? `?limit=${encodeURIComponent(String(limit))}` : ""}`),
getRun: (id: string, params: RunDetailParams = {}) => {
const q = new URLSearchParams();
diff --git a/frontend/src/pages/Correlation.tsx b/frontend/src/pages/Correlation.tsx
index aa3dc5124..47deaf99f 100644
--- a/frontend/src/pages/Correlation.tsx
+++ b/frontend/src/pages/Correlation.tsx
@@ -2,6 +2,7 @@ import i18n from '@/i18n';
import { useState } from "react";
import { BarChart3 } from "lucide-react";
import { CorrelationMatrix } from "@/components/charts/CorrelationMatrix";
+import { api } from "@/lib/api";
const WINDOWS = [30, 60, 90, 180, 365] as const;
@@ -19,9 +20,7 @@ export function Correlation() {
setError(null);
setLoading(true);
try {
- const result = await request<{ labels: string[]; matrix: number[][] }>(
- `/correlation?codes=${encodeURIComponent(codes)}&days=${days}&method=${method}`
- );
+ const result = await api.getCorrelation(codes, days, method);
setLabels(result.labels);
setMatrix(result.matrix);
} catch (e) {
@@ -116,22 +115,3 @@ export function Correlation() {
</div>
);
}
-
-// Minimal request helper (avoids importing the full api client which may have path issues)
-async function request<T>(path: string, options?: RequestInit): Promise<T> {
- const BASE = "";
- const res = await fetch(`${BASE}${path}`, {
- headers: { "Content-Type": "application/json", ...options?.headers },
- ...options,
- });
- if (!res.ok) {
- let detail = `HTTP ${res.status}`;
- try {
- const body = await res.json();
- detail = body.detail || body.message || detail;
- } catch { /* ignore */ }
- throw new Error(detail);
- }
- const text = await res.text();
- return text ? JSON.parse(text) : ({} as T);
-}
\ No newline at end of file