fix(tools): handle trailing slashes and whitespace in normalizeBaseUrl and addConversation

This commit is contained in:
Aditya kumar singh 2026-09-17 17:05:29 +05:30 • committed by Mahesh Sanikommu
parent a93d85adee
commit 85ddb64bfd
2 changed files with 29 additions and 2 deletions

View file

@ -8,8 +8,8 @@ import { Supermemory } from "supermemory"
*/
export const normalizeBaseUrl = (url?: string): string => {
const defaultUrl = "https://api.supermemory.ai"
if (!url) return defaultUrl
return url.endsWith("/") ? url.slice(0, -1) : url
const trimmed = url?.trim().replace(/\/+$/, "")
return trimmed || defaultUrl
}
/**

View file

@ -1,5 +1,6 @@
import { describe, expect, it } from "vitest"
import { makeTurnKey } from "./shared/cache"
import { normalizeBaseUrl } from "./shared/context"
import {
DEFAULT_VALUES,
SEARCH_LIMIT_BOUNDS,
@ -157,3 +158,29 @@ describe("makeTurnKey", () => {
expect(keyA).not.toBe(keyB)
})
})
describe("normalizeBaseUrl", () => {
it("returns default URL when input is missing or empty", () => {
expect(normalizeBaseUrl()).toBe("https://api.supermemory.ai")
expect(normalizeBaseUrl("")).toBe("https://api.supermemory.ai")
expect(normalizeBaseUrl(" ")).toBe("https://api.supermemory.ai")
})
it("collapses single and multiple trailing slashes", () => {
expect(normalizeBaseUrl("http://localhost:6768/")).toBe(
"http://localhost:6768",
)
expect(normalizeBaseUrl("http://localhost:6768///")).toBe(
"http://localhost:6768",
)
expect(normalizeBaseUrl("https://api.supermemory.ai/")).toBe(
"https://api.supermemory.ai",
)
})
it("trims whitespace around URLs", () => {
expect(normalizeBaseUrl(" http://localhost:6768/ ")).toBe(
"http://localhost:6768",
)
})
})