/**
* Tests for config loading and path resolution
*/
import { describe, expect, test } from "bun:test";
describe("config", () => {
const CONFIG_FILE = ".doclea/config.json";
describe("config file path", () => {
function getConfigPath(projectPath: string): string {
return `${projectPath}/${CONFIG_FILE}`;
}
test("builds correct path", () => {
expect(getConfigPath("/home/user/project")).toBe(
"/home/user/project/.doclea/config.json",
);
});
test("handles root path", () => {
expect(getConfigPath("/")).toBe("//.doclea/config.json");
});
test("handles relative path", () => {
expect(getConfigPath(".")).toBe("./.doclea/config.json");
});
});
describe("db path resolution", () => {
interface Config {
storage: { dbPath: string };
}
function getDbPath(config: Config, projectPath: string): string {
return `${projectPath}/${config.storage.dbPath}`;
}
test("resolves db path from config", () => {
const config = { storage: { dbPath: ".doclea/local.db" } };
expect(getDbPath(config, "/home/user/project")).toBe(
"/home/user/project/.doclea/local.db",
);
});
test("handles custom db path", () => {
const config = { storage: { dbPath: "data/memories.db" } };
expect(getDbPath(config, "/project")).toBe("/project/data/memories.db");
});
});
describe("default config structure", () => {
interface DefaultConfig {
embedding: { provider: string; endpoint: string };
qdrant: { url: string; collectionName: string };
storage: { dbPath: string };
}
const DEFAULT_CONFIG: DefaultConfig = {
embedding: { provider: "local", endpoint: "http://localhost:8080" },
qdrant: {
url: "http://localhost:6333",
collectionName: "doclea_memories",
},
storage: { dbPath: ".doclea/local.db" },
};
test("uses local provider by default", () => {
expect(DEFAULT_CONFIG.embedding.provider).toBe("local");
});
test("uses default local endpoint", () => {
expect(DEFAULT_CONFIG.embedding.endpoint).toBe("http://localhost:8080");
});
test("uses default qdrant url", () => {
expect(DEFAULT_CONFIG.qdrant.url).toBe("http://localhost:6333");
});
test("uses default collection name", () => {
expect(DEFAULT_CONFIG.qdrant.collectionName).toBe("doclea_memories");
});
test("uses default db path", () => {
expect(DEFAULT_CONFIG.storage.dbPath).toBe(".doclea/local.db");
});
});
describe("config validation", () => {
interface Config {
embedding: { provider: string };
qdrant: { url: string; collectionName: string };
storage: { dbPath: string };
}
function isValidConfig(config: unknown): config is Config {
if (typeof config !== "object" || config === null) return false;
const c = config as Record<string, unknown>;
if (typeof c.embedding !== "object" || c.embedding === null) return false;
if (typeof c.qdrant !== "object" || c.qdrant === null) return false;
if (typeof c.storage !== "object" || c.storage === null) return false;
const embedding = c.embedding as Record<string, unknown>;
const qdrant = c.qdrant as Record<string, unknown>;
const storage = c.storage as Record<string, unknown>;
return (
typeof embedding.provider === "string" &&
typeof qdrant.url === "string" &&
typeof qdrant.collectionName === "string" &&
typeof storage.dbPath === "string"
);
}
test("validates complete config", () => {
const config = {
embedding: { provider: "local", endpoint: "http://localhost:8080" },
qdrant: { url: "http://localhost:6333", collectionName: "memories" },
storage: { dbPath: ".doclea/local.db" },
};
expect(isValidConfig(config)).toBe(true);
});
test("rejects missing embedding", () => {
const config = {
qdrant: { url: "http://localhost:6333", collectionName: "memories" },
storage: { dbPath: ".doclea/local.db" },
};
expect(isValidConfig(config)).toBe(false);
});
test("rejects missing qdrant", () => {
const config = {
embedding: { provider: "local" },
storage: { dbPath: ".doclea/local.db" },
};
expect(isValidConfig(config)).toBe(false);
});
test("rejects missing storage", () => {
const config = {
embedding: { provider: "local" },
qdrant: { url: "http://localhost:6333", collectionName: "memories" },
};
expect(isValidConfig(config)).toBe(false);
});
test("rejects null config", () => {
expect(isValidConfig(null)).toBe(false);
});
});
describe("config error building", () => {
function buildConfigError(path: string, message: string): string {
return `Failed to load config from ${path}: ${message}`;
}
test("builds error message", () => {
const error = buildConfigError(
"/project/.doclea/config.json",
"Invalid JSON",
);
expect(error).toBe(
"Failed to load config from /project/.doclea/config.json: Invalid JSON",
);
});
test("handles parse error", () => {
const error = buildConfigError("/config.json", "Unexpected token");
expect(error).toContain("Unexpected token");
});
});
describe("embedding provider extraction", () => {
type Provider = "local" | "openai" | "nomic" | "voyage" | "ollama";
function getEmbeddingProvider(config: {
embedding: { provider: string };
}): Provider | null {
const p = config.embedding.provider;
if (["local", "openai", "nomic", "voyage", "ollama"].includes(p)) {
return p as Provider;
}
return null;
}
test("extracts local provider", () => {
expect(getEmbeddingProvider({ embedding: { provider: "local" } })).toBe(
"local",
);
});
test("extracts openai provider", () => {
expect(getEmbeddingProvider({ embedding: { provider: "openai" } })).toBe(
"openai",
);
});
test("returns null for invalid provider", () => {
expect(
getEmbeddingProvider({ embedding: { provider: "invalid" } }),
).toBeNull();
});
});
describe("qdrant config extraction", () => {
interface QdrantConfig {
url: string;
apiKey?: string;
collectionName: string;
}
function extractQdrantConfig(config: {
qdrant: Record<string, unknown>;
}): QdrantConfig | null {
const q = config.qdrant;
if (typeof q.url !== "string" || typeof q.collectionName !== "string") {
return null;
}
return {
url: q.url,
apiKey: typeof q.apiKey === "string" ? q.apiKey : undefined,
collectionName: q.collectionName,
};
}
test("extracts complete qdrant config", () => {
const config = {
qdrant: {
url: "http://localhost:6333",
apiKey: "secret",
collectionName: "memories",
},
};
const extracted = extractQdrantConfig(config);
expect(extracted?.url).toBe("http://localhost:6333");
expect(extracted?.apiKey).toBe("secret");
expect(extracted?.collectionName).toBe("memories");
});
test("extracts config without apiKey", () => {
const config = {
qdrant: { url: "http://localhost:6333", collectionName: "memories" },
};
const extracted = extractQdrantConfig(config);
expect(extracted?.apiKey).toBeUndefined();
});
test("returns null for missing url", () => {
const config = { qdrant: { collectionName: "memories" } };
expect(extractQdrantConfig(config)).toBeNull();
});
});
});