Files
kandev/apps/web/lib/api/domains/org-units-api.test.ts
T
João Salavisa 481359ddb2 feat(auth): organization units, tenancy, roles and scopes for team access (#2919)
Co-authored-by: Kandev Agent <agent@kandev.dev>
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-05 13:40:43 +00:00

41 lines
1.2 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { withDepth, type OrgUnit } from "./org-units-api";
function unit(id: string, path: string, name = id): OrgUnit {
return {
id,
org_id: "org-1",
parent_id: "",
kind: "standard",
name,
path,
created_at: "",
updated_at: "",
};
}
describe("withDepth", () => {
// Depth is what indents the tree. Deriving it from the materialized path
// rather than walking parents keeps the render a single pass, but it means
// an off-by-one here flattens a department into its own parent.
it("derives indentation depth from the materialized path", () => {
const rows = withDepth([
unit("root", "/root/"),
unit("dept", "/root/dept/"),
unit("team", "/root/dept/team/"),
unit("squad", "/root/dept/team/squad/"),
]);
expect(rows.map((r) => r.depth)).toEqual([0, 1, 2, 3]);
});
it("keeps the backend's parent-before-child order", () => {
const rows = withDepth([unit("root", "/root/"), unit("dept", "/root/dept/")]);
expect(rows.map((r) => r.id)).toEqual(["root", "dept"]);
});
it("treats a malformed path as top level rather than throwing", () => {
expect(withDepth([unit("odd", "")])[0]!.depth).toBe(0);
});
});