Commit ee913541 authored by Siddique's avatar Siddique
Browse files

Minor fixes

parent 86d95a9b
...@@ -199,7 +199,7 @@ npm ci ...@@ -199,7 +199,7 @@ npm ci
npm run dev npm run dev
``` ```
The frontend uses `http://localhost:8000` by default. Set `VITE_API_BASE_URL` before starting Vite if the backend uses another address. During local development, the frontend calls `/api`, and Vite proxies those requests to `http://127.0.0.1:8000`. This matches the `/api` path used by Nginx in Docker. If the local backend runs at another address, set `VITE_DEV_API_PROXY_TARGET` before starting Vite. `VITE_API_BASE_URL` can still override the browser-facing API base for a custom deployment.
Open: Open:
......
...@@ -12,7 +12,7 @@ npm ci ...@@ -12,7 +12,7 @@ npm ci
npm run dev npm run dev
``` ```
Set `VITE_API_BASE_URL` when the backend is not available at `http://localhost:8000`. During local development, the frontend uses `/api` and Vite proxies those requests to `http://127.0.0.1:8000`. Set `VITE_DEV_API_PROXY_TARGET` when the local backend uses another address. Docker uses the same `/api` browser path through Nginx. Set `VITE_API_BASE_URL` only when a custom deployment requires another browser-facing API base.
## Checks ## Checks
......
import { afterEach, describe, expect, it, vi } from "vitest"
import { getQuestionAnswers, getRunHistory, getRunStudents } from "./sessionsApi"
function successfulJsonResponse(payload: unknown) {
return {
ok: true,
json: vi.fn().mockResolvedValue(payload)
} as unknown as Response
}
describe("sessions API URLs", () => {
afterEach(() => {
vi.unstubAllGlobals()
})
it("uses the shared /api path for run history", async () => {
const fetchMock = vi.fn().mockResolvedValue(successfulJsonResponse({ items: [] }))
vi.stubGlobal("fetch", fetchMock)
await getRunHistory("grading")
expect(fetchMock).toHaveBeenCalledWith("/api/runs?run_type=grading")
})
it("builds student query parameters on the shared /api path", async () => {
const fetchMock = vi.fn().mockResolvedValue(successfulJsonResponse({ items: [] }))
vi.stubGlobal("fetch", fetchMock)
await getRunStudents("run/with spaces", 2, 50)
expect(fetchMock).toHaveBeenCalledWith(
"/api/runs/run%2Fwith%20spaces/students?page=2&page_size=50"
)
})
it("builds answer filters on the shared /api path", async () => {
const fetchMock = vi.fn().mockResolvedValue(successfulJsonResponse({ items: [] }))
vi.stubGlobal("fetch", fetchMock)
await getQuestionAnswers("run-1", 4, 1, 200, {
scoreScale: "whole",
predictedScores: [1, 2]
})
expect(fetchMock).toHaveBeenCalledWith(
"/api/runs/run-1/questions/4/answers?page=1&page_size=200&score_scale=whole&predicted_score=1&predicted_score=2"
)
})
})
import type { UploadMetadata } from "../types" import type { UploadMetadata } from "../types"
const API_BASE_URL = const configuredApiBaseUrl = import.meta.env.VITE_API_BASE_URL?.trim()
import.meta.env.VITE_API_BASE_URL?.replace(/\/$/, "") ?? const API_BASE_URL = (configuredApiBaseUrl || "/api").replace(/\/$/, "")
"http://localhost:8000"
async function parseError(response: Response): Promise<string> { async function parseError(response: Response): Promise<string> {
try { try {
......
...@@ -2,9 +2,20 @@ import { defineConfig } from 'vitest/config' ...@@ -2,9 +2,20 @@ import { defineConfig } from 'vitest/config'
import react from '@vitejs/plugin-react' import react from '@vitejs/plugin-react'
import tailwindcss from '@tailwindcss/vite' import tailwindcss from '@tailwindcss/vite'
const localBackendUrl = process.env.VITE_DEV_API_PROXY_TARGET || 'http://127.0.0.1:8000'
// https://vite.dev/config/ // https://vite.dev/config/
export default defineConfig({ export default defineConfig({
plugins: [react(), tailwindcss()], plugins: [react(), tailwindcss()],
server: {
proxy: {
'/api': {
target: localBackendUrl,
changeOrigin: true,
rewrite: path => path.replace(/^\/api/, ''),
},
},
},
test: { test: {
environment: 'jsdom', environment: 'jsdom',
setupFiles: './src/test/setup.ts', setupFiles: './src/test/setup.ts',
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment