1+ import { beforeEach , describe , expect , it , vi } from "vitest"
2+
3+ import {
4+ createProjectConfig ,
5+ recomputeProjectConfigAuthority ,
6+ updateProjectConfig ,
7+ } from "@/lib/api"
8+
9+ import { POST } from "./route"
10+
11+ vi . mock ( "@/lib/api" , ( ) => ( {
12+ createProjectConfig : vi . fn ( ) ,
13+ recomputeProjectConfigAuthority : vi . fn ( ) ,
14+ updateProjectConfig : vi . fn ( ) ,
15+ } ) )
16+
17+ describe ( "POST /api/projects/[id]/authority-settings" , ( ) => {
18+ beforeEach ( ( ) => {
19+ vi . clearAllMocks ( )
20+ } )
21+
22+ function buildFormData ( ) {
23+ const formData = new FormData ( )
24+ formData . set ( "draft_schedule_cron" , "" )
25+ formData . set ( "authority_weight_mention" , "0.2" )
26+ formData . set ( "authority_weight_engagement" , "0.15" )
27+ formData . set ( "authority_weight_recency" , "0.15" )
28+ formData . set ( "authority_weight_source_quality" , "0.15" )
29+ formData . set ( "authority_weight_cross_newsletter" , "0.2" )
30+ formData . set ( "authority_weight_feedback" , "0.1" )
31+ formData . set ( "authority_weight_duplicate" , "0.05" )
32+ formData . set ( "upvote_authority_weight" , "0.05" )
33+ formData . set ( "downvote_authority_weight" , "-0.05" )
34+ formData . set ( "authority_decay_rate" , "0.9" )
35+ return formData
36+ }
37+
38+ it ( "creates a config and returns JSON for a save request" , async ( ) => {
39+ vi . mocked ( createProjectConfig ) . mockResolvedValue ( { id : 7 } as never )
40+
41+ const response = await POST (
42+ new Request ( "http://localhost/api/projects/4/authority-settings?mode=json" , {
43+ method : "POST" ,
44+ body : buildFormData ( ) ,
45+ } ) ,
46+ {
47+ params : Promise . resolve ( { id : "4" } ) ,
48+ } ,
49+ )
50+
51+ expect ( createProjectConfig ) . toHaveBeenCalledWith (
52+ 4 ,
53+ expect . objectContaining ( { authority_weight_engagement : 0.15 } ) ,
54+ )
55+ expect ( updateProjectConfig ) . not . toHaveBeenCalled ( )
56+ expect ( response . status ) . toBe ( 200 )
57+ await expect ( response . json ( ) ) . resolves . toEqual ( {
58+ configId : 7 ,
59+ message : "Authority weights saved." ,
60+ } )
61+ } )
62+
63+ it ( "updates and recomputes when requested" , async ( ) => {
64+ vi . mocked ( updateProjectConfig ) . mockResolvedValue ( { id : 9 } as never )
65+ vi . mocked ( recomputeProjectConfigAuthority ) . mockResolvedValue ( {
66+ status : "completed" ,
67+ project_id : 4 ,
68+ config_id : 9 ,
69+ } )
70+
71+ const formData = buildFormData ( )
72+ formData . set ( "configId" , "9" )
73+ formData . set ( "intent" , "save_and_recompute" )
74+
75+ const response = await POST (
76+ new Request ( "http://localhost/api/projects/4/authority-settings?mode=json" , {
77+ method : "POST" ,
78+ body : formData ,
79+ } ) ,
80+ {
81+ params : Promise . resolve ( { id : "4" } ) ,
82+ } ,
83+ )
84+
85+ expect ( updateProjectConfig ) . toHaveBeenCalledWith (
86+ 4 ,
87+ 9 ,
88+ expect . objectContaining ( { authority_weight_source_quality : 0.15 } ) ,
89+ )
90+ expect ( recomputeProjectConfigAuthority ) . toHaveBeenCalledWith ( 4 , 9 )
91+ expect ( response . status ) . toBe ( 200 )
92+ await expect ( response . json ( ) ) . resolves . toEqual ( {
93+ configId : 9 ,
94+ message : "Authority weights saved and recomputed." ,
95+ } )
96+ } )
97+ } )
0 commit comments