gram/packages/language-server/tests/project-config.test.ts
abiwab 43396c4db0
All checks were successful
CI / Build & Test (pull_request) Successful in 5m51s
Merge remote-tracking branch 'origin/main' into chore/merge-main-into-dev-1.6-2026-11-26-8
# Conflicts:
#	packages/language-server/src/server.ts
#	packages/language-server/src/utils/project-config.ts
2026-11-26 23:21:07 +01:00

166 lines
5.3 KiB
TypeScript

import { afterEach, describe, expect, it } from "bun:test";
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { readProjectConfigSync } from "../src/utils/project-config";
const dirs: string[] = [];
afterEach(async () => {
while (dirs.length > 0)
await rm(dirs.pop()!, { recursive: true, force: true });
});
/** A project, with `.gram/config.yaml` holding `configYaml` when given. */
async function project(configYaml?: string): Promise<string> {
const dir = await mkdtemp(join(tmpdir(), "gram-projectcfg-"));
dirs.push(dir);
if (configYaml !== undefined) {
await mkdir(join(dir, ".gram"));
await writeFile(join(dir, ".gram", "config.yaml"), configYaml, "utf-8");
}
return dir;
}
const read = async (configYaml?: string) =>
readProjectConfigSync(await project(configYaml));
describe("readProjectConfigSync", () => {
it("is empty without a config file, or with one that does not parse", async () => {
expect(await read()).toEqual({});
expect(await read("{{ not yaml")).toEqual({});
expect(await read("paths: [this is not a map\n")).toEqual({});
expect(await read("just a string\n")).toEqual({});
});
it("reads every field from one file", async () => {
expect(
await read(
[
"paths:",
" bases: ./shared/bases",
"timezone: Europe/Paris",
"availability: 08:00-22:00",
'mealTimes: ["12:00", "19:30"]',
"language: fr",
].join("\n"),
),
).toEqual({
paths: { bases: "./shared/bases" },
timeZone: "Europe/Paris",
availability: { daily: [{ start: "08:00", end: "22:00" }] },
mealTimes: ["12:00", "19:30"],
});
});
it("leaves out a field that does not read, without hiding the others", async () => {
expect(
await read(
"timezone: Mars/Olympus\navailability: 8-22\nmealTimes: [noon]\npaths:\n a: ./a\n",
),
).toEqual({ paths: { a: "./a" } });
});
describe("paths", () => {
it("drops the aliases that are not strings", async () => {
expect(
(await read("paths:\n bases: ./shared/bases\n bad: 42\n")).paths,
).toEqual({ bases: "./shared/bases" });
});
it("is absent without a paths: key", async () => {
expect((await read("language: fr\n")).paths).toBeUndefined();
});
});
describe("database", () => {
it("reads the path of the ingredient database, as the CLI does", async () => {
expect((await read("database: data/ingredients.yaml\n")).database).toBe(
"data/ingredients.yaml",
);
});
it("is absent without it, or with a value that is not a path", async () => {
expect((await read("timezone: Europe/Paris\n")).database).toBeUndefined();
expect((await read('database: ""\n')).database).toBeUndefined();
expect((await read("database: 42\n")).database).toBeUndefined();
});
});
describe("timezone", () => {
it("is absent without one, or with a zone that does not exist", async () => {
expect((await read("paths:\n a: ./a\n")).timeZone).toBeUndefined();
expect((await read("timezone: Mars/Olympus\n")).timeZone).toBeUndefined();
});
});
describe("availability", () => {
it("reads one entry or a list of them, in the syntax of --available", async () => {
expect(
(await read("availability:\n - 09:00-21:00\n - sun=none\n"))
.availability,
).toEqual({
daily: [{ start: "09:00", end: "21:00" }],
weekdays: { sun: [] },
});
});
it("is absent without the setting, or with one that does not parse", async () => {
expect(
(await read("timezone: Europe/Paris\n")).availability,
).toBeUndefined();
expect((await read("availability: 8-22\n")).availability).toBeUndefined();
expect((await read("availability: 8\n")).availability).toBeUndefined();
expect((await read('availability: ""\n')).availability).toBeUndefined();
});
});
describe("clock", () => {
it("reads 12h, 24h or auto", async () => {
expect((await read("clock: 12h\n")).clock).toBe("12h");
expect((await read("clock: 24h\n")).clock).toBe("24h");
});
it("is absent without it, or with a value it does not know", async () => {
expect((await read("timezone: Europe/Paris\n")).clock).toBeUndefined();
expect((await read("clock: 12\n")).clock).toBeUndefined();
expect((await read("clock: AM/PM\n")).clock).toBeUndefined();
});
});
describe("mealTimes", () => {
it("reads them as the CLI does, padding the hour", async () => {
expect((await read('mealTimes: ["9:30", "19:30"]\n')).mealTimes).toEqual([
"09:30",
"19:30",
]);
});
it("is absent without them, or with a time it cannot read", async () => {
expect((await read('mealTimes: ["noon"]\n')).mealTimes).toBeUndefined();
expect((await read("mealTimes: 12:00\n")).mealTimes).toBeUndefined();
expect((await read("mealTimes: []\n")).mealTimes).toBeUndefined();
});
});
describe("lint", () => {
it("reads the settings of lint.rules", async () => {
expect(
(
await read(
"lint:\n rules:\n UNKNOWN_INGREDIENT: off\n UNUSED_INTERMEDIATE: hint\n",
)
).lint,
).toEqual({
rules: { UNKNOWN_INGREDIENT: "off", UNUSED_INTERMEDIATE: "hint" },
});
});
it("is absent without it, or when no setting reads", async () => {
expect((await read("clock: 24h\n")).lint).toBeUndefined();
expect(
(await read("lint:\n rules:\n MISSING_TITLE: error\n")).lint,
).toBeUndefined();
});
});
});