1 import { describe, expect, it } from "vitest"
2 import {
3 getApiRefIntroSlug,
4 getApiRefOperationSlug,
5 getApiRefPath,
6 getApiRefTagOperationSlugs,
7 getApiRefTagSlug,
8 } from "../api-ref-paths.js"
9
10 describe("getApiRefIntroSlug", () => {
11 it("slugifies an intro heading", () => {
12 expect(getApiRefIntroSlug("Authentication")).toBe("authentication")
13 expect(getApiRefIntroSlug("Select Fields and Relations")).toBe(
14 "select-fields-and-relations"
15 )
16 })
17 })
18
19 describe("getApiRefTagSlug", () => {
20 it("slugifies a tag name", () => {
21 expect(getApiRefTagSlug("Carts")).toBe("carts")
22 expect(getApiRefTagSlug("Gift Cards")).toBe("gift-cards")
23 })
24 })
25
26 describe("getApiRefOperationSlug", () => {
27 it("prefers x-sidebar-summary, then summary, then operationId", () => {
28 expect(
29 getApiRefOperationSlug({
30 operationId: "GetCartsId",
31 summary: "Get a Cart",
32 "x-sidebar-summary": "Get Cart",
33 })
34 ).toBe("get-cart")
35 expect(
36 getApiRefOperationSlug({ operationId: "GetCartsId", summary: "Get a Cart" })
37 ).toBe("get-a-cart")
38 expect(getApiRefOperationSlug({ operationId: "GetCartsId" })).toBe(
39 "getcartsid"
40 )
41 })
42 })
43
44 describe("getApiRefTagOperationSlugs", () => {
45 it("computes unique slugs within a tag", () => {
46 const slugs = getApiRefTagOperationSlugs([
47 { operationId: "GetCartsId", summary: "Get a Cart" },
48 { operationId: "PostCarts", summary: "Create Cart" },
49 { operationId: "PostCartsIdLineItems", "x-sidebar-summary": "Add Line Item" },
50 ])
51
52 expect(slugs.get("GetCartsId")).toBe("get-a-cart")
53 expect(slugs.get("PostCarts")).toBe("create-cart")
54 expect(slugs.get("PostCartsIdLineItems")).toBe("add-line-item")
55 })
56
57 it("deduplicates colliding slugs deterministically", () => {
58 const slugs = getApiRefTagOperationSlugs([
59 { operationId: "A", summary: "Get a Cart" },
60 { operationId: "B", summary: "Get a Cart" },
61 { operationId: "C", summary: "Get a Cart" },
62 ])
63
64 expect(slugs.get("A")).toBe("get-a-cart")
65 expect(slugs.get("B")).toBe("get-a-cart-2")
66 expect(slugs.get("C")).toBe("get-a-cart-3")
67 })
68
69 it("avoids collision with the reserved schema slug", () => {
70 const slugs = getApiRefTagOperationSlugs([
71 { operationId: "A", summary: "Schema" },
72 ])
73
74 expect(slugs.get("A")).toBe("schema-2")
75 })
76 })
77
78 describe("getApiRefPath", () => {
79 it("builds tag and operation paths", () => {
80 expect(getApiRefPath({ area: "store", section: "carts" })).toBe(
81 "/store/carts"
82 )
83 expect(
84 getApiRefPath({
85 area: "store",
86 section: "carts",
87 operationSlug: "get-a-cart",
88 })
89 ).toBe("/store/carts/get-a-cart")
90 expect(
91 getApiRefPath({ area: "store", section: "carts", operationSlug: "schema" })
92 ).toBe("/store/carts/schema")
93 })
94 })