test.rules/rules/eslint/scoped-resource-release.eslint.rule.test.ts

test.rules/rules/eslint/scoped-resource-release.eslint.rule.test.ts is a file in Codebase Testing. 48 lines of code and 0 definitions.

import { RESOURCE_MARK, SCOPED_RESOURCES } from "@ssot/govlab/shared/manifests/invariant.manifest.ts";
import { describe, expect, it } from "vitest";
import { Linter } from "eslint";
import { absolutePath } from "@ssot/paths";
import rule from "@ssot/govlab/rules/eslint/scoped-resource-release.eslint.rule.ts";
import { sep } from "node:path";

const linter = new Linter();
const config = [
    {
        files: ["**/*.ts"],
        languageOptions: {
            ecmaVersion: 2025 as const,
            parser: await import("@typescript-eslint/parser"),
            sourceType: "module" as const,
        },
        plugins: { t: { rules: { r: rule } } },
        rules: { "t/r": "error" as const },
    },
];

const SRC = `${absolutePath("govlab.patterns").split(sep).join("/")}/core/factories/probe.factory.ts`;

const idsFor = function idsFor(code: string): string[] {
    return linter
        .verify(code, config, SRC)
        .map((message) => message.messageId)
        .filter((id): id is string => typeof id === "string");
};

const [entry] = [...SCOPED_RESOURCES];
const [key, release] = entry ?? ["", ""];
const source = key.slice(0, key.indexOf(RESOURCE_MARK));
const acquirer = key.slice(key.indexOf(RESOURCE_MARK) + RESOURCE_MARK.length);

describe("scoped-resource-release", () => {
    it("reports a registered acquirer imported where its release is neither imported nor declared", () => {
        expect(idsFor(`import { ${acquirer} } from "${source}";\nexport const open = ${acquirer};`)).toStrictEqual([
            "unreleased",
        ]);
    });

    it("accepts the acquirer beside a declared release function", () => {
        const code = `import { ${acquirer} } from "${source}";\nexport const ${release} = async function ${release}(): Promise<void> {};`;
        expect(idsFor(code)).toStrictEqual([]);
    });

    it("accepts the acquirer beside an imported release function", () => {
        const code = `import { ${acquirer} } from "${source}";\nimport { ${release} } from "./owner.factory.ts";`;
        expect(idsFor(code)).toStrictEqual([]);
    });

    it("ignores the same name taken from a module the registry does not classify", () => {
        expect(idsFor(`import { ${acquirer} } from "unregistered";`)).toStrictEqual([]);
    });
});