53 lines
2.3 KiB
TypeScript
53 lines
2.3 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import { chmodSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import test from "node:test";
|
|
import { DEFAULT_CONFIG, loadConfig } from "../src/config.js";
|
|
import { loadCredentials, NOCOBASE_API_BASE_URL, saveCredentials } from "../src/credentials.js";
|
|
|
|
test("stores only the Key and always loads the fixed NocoBase endpoint", () => {
|
|
const directory = mkdtempSync(join(tmpdir(), "nocobase-mcp-credentials-"));
|
|
const path = join(directory, "private", "credentials.json");
|
|
saveCredentials({ authorizeKey: "secret" }, path);
|
|
assert.deepEqual(loadCredentials(path), {
|
|
apiBaseUrl: NOCOBASE_API_BASE_URL,
|
|
authorizeKey: "secret",
|
|
});
|
|
assert.deepEqual(JSON.parse(readFileSync(path, "utf8")), { authorizeKey: "secret" });
|
|
|
|
writeFileSync(path, JSON.stringify({ apiBaseUrl: "https://legacy.example/api", authorizeKey: "secret" }));
|
|
assert.equal(loadCredentials(path).apiBaseUrl, NOCOBASE_API_BASE_URL);
|
|
if (process.platform !== "win32") {
|
|
chmodSync(path, 0o644);
|
|
assert.throws(() => loadCredentials(path), /0600/);
|
|
}
|
|
});
|
|
|
|
test("loads camelCase and snake_case config aliases without mutating defaults", () => {
|
|
const directory = mkdtempSync(join(tmpdir(), "nocobase-mcp-config-"));
|
|
const path = join(directory, "config.json");
|
|
writeFileSync(
|
|
path,
|
|
JSON.stringify({
|
|
data_source: "secondary",
|
|
timezone: "UTC",
|
|
collections: { customers: { resource: "crm_customers", owner_field: "assignee" } },
|
|
projects: { nameField: "project_title" },
|
|
}),
|
|
);
|
|
const loaded = loadConfig(path);
|
|
assert.equal(loaded.dataSource, "secondary");
|
|
assert.equal(loaded.customer.resource, "crm_customers");
|
|
assert.equal(loaded.customer.ownerField, "assignee");
|
|
assert.equal(loaded.project.nameField, "project_title");
|
|
assert.equal(DEFAULT_CONFIG.customer.resource, "customers");
|
|
});
|
|
|
|
test("rejects malformed config types before any NocoBase request", () => {
|
|
const directory = mkdtempSync(join(tmpdir(), "nocobase-mcp-invalid-config-"));
|
|
const path = join(directory, "config.json");
|
|
writeFileSync(path, JSON.stringify({ collections: { customers: { fields: "id,name" } } }));
|
|
assert.throws(() => loadConfig(path), /array of strings/);
|
|
});
|