11import type { CanonicalSpec } from "@patchlogr/types" ;
22import { describe , expect , test } from "vitest" ;
33import { DEFAULT_TAG , partitionByTag } from "../partitionByTag" ;
4+ import type { HashInternalNode } from "../partition" ;
45
56describe ( "partitionByTag" , ( ) => {
67 test ( "should group by first tag" , ( ) => {
@@ -25,13 +26,21 @@ describe("partitionByTag", () => {
2526 } ,
2627 } ;
2728
28- const partitions = partitionByTag ( spec ) . partitions ;
29- expect ( partitions ) . toHaveLength ( 1 ) ;
30- expect ( partitions . get ( "user" ) ) . toHaveLength ( 2 ) ;
31- expect ( partitions . get ( "user" ) ?. [ 0 ] ?. operationKey ) . toBe ( "GET /user" ) ;
32- expect ( partitions . get ( "user" ) ?. [ 1 ] ?. operationKey ) . toBe (
33- "GET /user/{userId}" ,
34- ) ;
29+ const result = partitionByTag ( spec ) ;
30+ expect ( result . root . type ) . toBe ( "node" ) ;
31+ expect ( result . root . key ) . toBe ( "root" ) ;
32+
33+ const root = result . root as HashInternalNode ;
34+ expect ( root . children ) . toHaveLength ( 1 ) ;
35+
36+ const userTagNode = root . children . find (
37+ ( child ) => child . key === "user" ,
38+ ) as HashInternalNode ;
39+
40+ expect ( userTagNode . type ) . toBe ( "node" ) ;
41+ expect ( userTagNode . children ) . toHaveLength ( 2 ) ;
42+ expect ( userTagNode . children [ 0 ] ?. key ) . toBe ( "GET /user" ) ;
43+ expect ( userTagNode . children [ 1 ] ?. key ) . toBe ( "GET /user/{userId}" ) ;
3544 } ) ;
3645
3746 test ( "should group by multiple tags" , ( ) => {
@@ -56,15 +65,24 @@ describe("partitionByTag", () => {
5665 } ,
5766 } ;
5867
59- const partitions = partitionByTag ( spec ) . partitions ;
68+ const result = partitionByTag ( spec ) ;
69+ expect ( result . root . type ) . toBe ( "node" ) ;
70+
71+ const root = result . root as HashInternalNode ;
72+ expect ( root . children ) . toHaveLength ( 2 ) ;
6073
61- expect ( partitions ) . toHaveLength ( 2 ) ;
62- expect ( partitions . get ( "user" ) ) . toHaveLength ( 1 ) ;
63- expect ( partitions . get ( "auth" ) ) . toHaveLength ( 1 ) ;
64- expect ( partitions . get ( "user" ) ?. [ 0 ] ?. operationKey ) . toBe ( "GET /user" ) ;
65- expect ( partitions . get ( "auth" ) ?. [ 0 ] ?. operationKey ) . toBe (
66- "POST /auth/login" ,
67- ) ;
74+ const userTagNode = root . children . find (
75+ ( child ) => child . key === "user" ,
76+ ) as HashInternalNode ;
77+ const authTagNode = root . children . find (
78+ ( child ) => child . key === "auth" ,
79+ ) as HashInternalNode ;
80+
81+ expect ( userTagNode . children ) . toHaveLength ( 1 ) ;
82+ expect ( userTagNode . children [ 0 ] ?. key ) . toBe ( "GET /user" ) ;
83+
84+ expect ( authTagNode . children ) . toHaveLength ( 1 ) ;
85+ expect ( authTagNode . children [ 0 ] ?. key ) . toBe ( "POST /auth/login" ) ;
6886 } ) ;
6987
7088 test ( "should group into default tag if tag not exists" , ( ) => {
@@ -81,12 +99,18 @@ describe("partitionByTag", () => {
8199 } ,
82100 } ;
83101
84- const partitions = partitionByTag ( spec ) . partitions ;
102+ const result = partitionByTag ( spec ) ;
103+ expect ( result . root . type ) . toBe ( "node" ) ;
104+
105+ const root = result . root as HashInternalNode ;
106+ expect ( root . children ) . toHaveLength ( 1 ) ;
107+
108+ const defaultTagNode = root . children . find (
109+ ( child ) => child . key === DEFAULT_TAG ,
110+ ) as HashInternalNode ;
85111
86- expect ( partitions ) . toHaveLength ( 1 ) ;
87- expect ( partitions . get ( DEFAULT_TAG ) ) . toHaveLength ( 1 ) ;
88- expect ( partitions . get ( DEFAULT_TAG ) ?. [ 0 ] ?. operationKey ) . toBe (
89- "GET /user" ,
90- ) ;
112+ expect ( defaultTagNode . type ) . toBe ( "node" ) ;
113+ expect ( defaultTagNode . children ) . toHaveLength ( 1 ) ;
114+ expect ( defaultTagNode . children [ 0 ] ?. key ) . toBe ( "GET /user" ) ;
91115 } ) ;
92116} ) ;
0 commit comments