jscpd-rs 0.1.6

50x+ faster duplicate-code detector for CI/CD; jscpd-compatible CLI, SARIF, JSON, HTML reports
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
use std::collections::HashSet;
use std::fmt::Write as _;
use std::net::{SocketAddr, ToSocketAddrs};
use std::path::{Path, PathBuf};
use std::sync::{Arc, RwLock};

use anyhow::{Context, Result, bail};
use axum::body::Bytes;
use axum::extract::DefaultBodyLimit;
use axum::extract::State;
use axum::http::header::CONTENT_TYPE;
use axum::http::{HeaderMap, Method, StatusCode, Uri};
use axum::response::{IntoResponse, Response};
use axum::routing::{get, post};
use axum::{Json, Router};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use time::OffsetDateTime;
use time::format_description::well_known::Rfc3339;

use crate::cli::{Options, store_warning};
use crate::detector::{DetectionResult, Fragment, Statistics};
use crate::detector::{PreparedSourceDraft, detect_prepared_drafts, prepare_source_drafts};
use crate::files::{self, SourceFile};

mod mcp;

#[derive(Clone)]
pub struct ServerService {
    state: Arc<RwLock<ServiceState>>,
}

#[derive(Clone)]
struct ServiceState {
    working_directory: PathBuf,
    options: Options,
    project_drafts: Arc<[PreparedSourceDraft]>,
    statistics: Option<Statistics>,
    last_scan_time: Option<String>,
    is_scanning: bool,
    snippet_counter: u64,
    mcp_sessions: HashSet<String>,
}

impl ServerService {
    pub fn new(working_directory: PathBuf, options: Options) -> Self {
        Self {
            state: Arc::new(RwLock::new(ServiceState {
                working_directory,
                options,
                project_drafts: Arc::from(Vec::<PreparedSourceDraft>::new()),
                statistics: None,
                last_scan_time: None,
                is_scanning: false,
                snippet_counter: 0,
                mcp_sessions: HashSet::new(),
            })),
        }
    }

    pub fn initialize(&self) -> Result<()> {
        self.recheck()
    }

    pub fn recheck(&self) -> Result<()> {
        let options = {
            let mut state = self.state.write().expect("server state lock poisoned");
            if state.is_scanning {
                bail!(SCAN_IN_PROGRESS);
            }
            state.is_scanning = true;
            service_detection_options(&state)
        };

        let result = scan_project(&options);
        let mut state = self.state.write().expect("server state lock poisoned");
        state.is_scanning = false;

        let (project_drafts, detection_result) = result?;
        state.project_drafts = project_drafts;
        state.statistics = Some(detection_result.statistics);
        state.last_scan_time = Some(now_rfc3339());
        Ok(())
    }

    pub fn check_snippet(&self, request: CheckSnippetRequest) -> Result<CheckSnippetResponse> {
        if request.code.trim().is_empty() {
            bail!(FIELD_CODE_EMPTY);
        }

        let (options, project_drafts, snippet_id, working_directory) = {
            let mut state = self.state.write().expect("server state lock poisoned");
            if state.is_scanning {
                bail!(SCAN_IN_PROGRESS);
            }
            if state.statistics.is_none() {
                bail!(NOT_INITIALIZED);
            }
            let snippet_id = format!("<snippet>/snippet_{:08x}", state.snippet_counter);
            state.snippet_counter += 1;
            (
                service_detection_options(&state),
                Arc::clone(&state.project_drafts),
                snippet_id,
                state.working_directory.clone(),
            )
        };

        let total_lines = request.code.split('\n').count();
        let snippet_drafts = prepare_source_drafts(
            vec![SourceFile {
                source_id: snippet_id.clone(),
                format: request.format,
                content: request.code,
            }],
            &options,
        );
        let mut prepared_drafts = Vec::with_capacity(project_drafts.len() + snippet_drafts.len());
        prepared_drafts.extend(project_drafts.iter().cloned());
        prepared_drafts.extend(snippet_drafts);
        let result = detect_prepared_drafts(prepared_drafts, &options);
        let duplications = result
            .clones
            .iter()
            .filter_map(|clone| {
                let snippet_is_a = clone.duplication_a.source_id == snippet_id;
                let snippet_is_b = clone.duplication_b.source_id == snippet_id;
                if snippet_is_a == snippet_is_b {
                    return None;
                }
                let (snippet, codebase) = if snippet_is_a {
                    (&clone.duplication_a, &clone.duplication_b)
                } else {
                    (&clone.duplication_b, &clone.duplication_a)
                };
                Some(SnippetDuplication {
                    snippet_location: SnippetLocation::from_fragment(snippet),
                    codebase_location: DuplicationLocation::from_fragment(
                        codebase,
                        &working_directory,
                        &result,
                    ),
                    lines_count: fragment_line_count(snippet),
                })
            })
            .collect::<Vec<_>>();
        let statistics = duplication_statistics(&duplications, total_lines);

        Ok(CheckSnippetResponse {
            duplications,
            statistics,
        })
    }

    pub fn statistics(&self) -> StatsResponse {
        let state = self.state.read().expect("server state lock poisoned");
        StatsResponse {
            statistics: state.statistics.clone(),
            timestamp: state.last_scan_time.clone().unwrap_or_else(now_rfc3339),
        }
    }

    pub fn health(&self) -> HealthResponse {
        let state = self.state.read().expect("server state lock poisoned");
        HealthResponse {
            status: if state.is_scanning {
                "initializing"
            } else {
                "ready"
            },
            working_directory: state.working_directory.display().to_string(),
            last_scan_time: state.last_scan_time.clone(),
        }
    }

    pub(crate) fn create_mcp_session(&self) -> String {
        let mut state = self.state.write().expect("server state lock poisoned");
        let session_id = new_mcp_session_id();
        state.mcp_sessions.insert(session_id.clone());
        session_id
    }

    pub(crate) fn has_mcp_session(&self, session_id: &str) -> bool {
        let state = self.state.read().expect("server state lock poisoned");
        state.mcp_sessions.contains(session_id)
    }
}

fn detection_options(options: &Options) -> Options {
    let mut options = options.clone();
    options.reporters = vec!["json".to_string()];
    options.silent = true;
    options.no_tips = true;
    options
}

fn service_detection_options(state: &ServiceState) -> Options {
    let mut options = detection_options(&state.options);
    options.paths = vec![state.working_directory.clone()];
    options
}

fn scan_project(options: &Options) -> Result<(Arc<[PreparedSourceDraft]>, DetectionResult)> {
    let files = files::discover(options)?;
    let project_drafts = prepare_source_drafts(files, options);
    let result = detect_prepared_drafts(project_drafts.clone(), options);
    Ok((Arc::from(project_drafts), result))
}

pub fn create_router(service: ServerService) -> Router {
    Router::new()
        .route("/", get(api_info))
        .route("/api/check", post(check_snippet).fallback(not_found))
        .route("/api/recheck", post(recheck).fallback(not_found))
        .route("/api/stats", get(stats).fallback(not_found))
        .route("/api/health", get(health).fallback(not_found))
        .route(
            "/mcp",
            post(mcp::post_mcp)
                .get(mcp::method_not_allowed)
                .fallback(not_found),
        )
        .fallback(not_found)
        .layer(DefaultBodyLimit::max(10 * 1024 * 1024))
        .with_state(service)
}

pub async fn serve(options: Options, host: &str, port: u16) -> Result<()> {
    let working_directory = server_working_directory(&options);
    serve_with_working_directory(options, working_directory, host, port).await
}

pub async fn serve_with_working_directory(
    options: Options,
    working_directory: PathBuf,
    host: &str,
    port: u16,
) -> Result<()> {
    if let Some(warning) = store_warning(&options) {
        eprintln!("{warning}");
    }
    let service = ServerService::new(working_directory, options);
    service.initialize()?;
    let app = create_router(service);
    let address = server_bind_address(host, port)?;
    let listener = tokio::net::TcpListener::bind(address)
        .await
        .with_context(|| format!("failed to bind server address {address}"))?;
    println!("JSCPD server running on {}", server_display_url(host, port));
    axum::serve(listener, app).await.context("server failed")
}

fn server_bind_address(host: &str, port: u16) -> Result<SocketAddr> {
    let bind_host = if host == "true" { "0.0.0.0" } else { host };
    (bind_host, port)
        .to_socket_addrs()
        .with_context(|| format!("failed to resolve server address {host}:{port}"))?
        .next()
        .with_context(|| format!("failed to resolve server address {host}:{port}"))
}

fn server_display_url(host: &str, port: u16) -> String {
    format!("http://{host}:{port}")
}

pub fn server_working_directory(options: &Options) -> PathBuf {
    options
        .paths
        .first()
        .cloned()
        .unwrap_or_else(|| std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")))
}

async fn api_info() -> Json<ApiInfoResponse> {
    Json(ApiInfoResponse {
        name: "jscpd-server",
        version: env!("CARGO_PKG_VERSION"),
        endpoints: [
            ("POST /api/check", "Check code snippet for duplications"),
            ("GET /api/stats", "Get overall project statistics"),
            ("GET /api/health", "Server health check"),
            ("POST /api/recheck", "Trigger recheck of the directory"),
            ("POST /mcp", "MCP Protocol endpoint"),
        ]
        .into_iter()
        .map(|(key, value)| (key.to_string(), value.to_string()))
        .collect(),
        documentation: "https://github.com/kucherenko/jscpd",
    })
}

async fn check_snippet(
    State(service): State<ServerService>,
    headers: HeaderMap,
    body: Bytes,
) -> Response {
    let request = match parse_check_payload(&headers, &body) {
        Ok(request) => request,
        Err(CheckPayloadError::Validation(message)) => {
            return error_response("ValidationError", message, 400);
        }
        Err(CheckPayloadError::Syntax(message)) => {
            return error_response("SyntaxError", message, 400);
        }
    };
    match service.check_snippet(request) {
        Ok(response) => Json(response).into_response(),
        Err(error) => error_response("Error", error.to_string(), 400),
    }
}

async fn recheck(State(service): State<ServerService>) -> Response {
    match service.recheck() {
        Ok(()) => Json(RecheckResponse {
            message: "Recheck started",
        })
        .into_response(),
        Err(error) => error_response("Error", error.to_string(), 400),
    }
}

async fn stats(State(service): State<ServerService>) -> Response {
    let response = service.statistics();
    if response.statistics.is_none() {
        return error_response(
            "NotReady",
            "Statistics not available yet. Server is still initializing.",
            503,
        );
    }
    Json(response).into_response()
}

async fn health(State(service): State<ServerService>) -> Json<HealthResponse> {
    Json(service.health())
}

async fn not_found(method: Method, uri: Uri) -> Response {
    error_response(
        "NotFound",
        format!("Route {method} {} not found", uri.path()),
        404,
    )
}

fn error_response(error: &str, message: impl Into<String>, status_code: u16) -> Response {
    let status = StatusCode::from_u16(status_code).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR);
    (
        status,
        Json(ErrorResponse {
            error: error.to_string(),
            message: message.into(),
            status_code,
        }),
    )
        .into_response()
}

fn parse_check_payload(
    headers: &HeaderMap,
    body: &[u8],
) -> std::result::Result<CheckSnippetRequest, CheckPayloadError> {
    let content_type = headers
        .get(CONTENT_TYPE)
        .and_then(|value| value.to_str().ok())
        .unwrap_or_default()
        .to_ascii_lowercase();
    if content_type.starts_with("application/x-www-form-urlencoded") {
        return parse_check_form(body).map_err(CheckPayloadError::Validation);
    }
    let payload = serde_json::from_slice(body)
        .map_err(|error| CheckPayloadError::Syntax(json_syntax_error_message(body, &error)))?;
    parse_check_request(payload).map_err(CheckPayloadError::Validation)
}

fn parse_check_form(body: &[u8]) -> std::result::Result<CheckSnippetRequest, String> {
    let fields = form_urlencoded::parse(body)
        .into_owned()
        .collect::<Vec<_>>();
    let code = required_form_field(&fields, "code")?;
    if code.trim().is_empty() {
        return Err(FIELD_CODE_EMPTY.to_string());
    }
    let format = required_form_field(&fields, "format")?;
    if format.trim().is_empty() {
        return Err(FIELD_FORMAT_EMPTY.to_string());
    }
    Ok(CheckSnippetRequest { code, format })
}

fn parse_check_request(payload: Value) -> std::result::Result<CheckSnippetRequest, String> {
    let Some(object) = payload.as_object() else {
        return Err("Request body must be an object".to_string());
    };
    let code = required_string_field(object, "code")?;
    if code.trim().is_empty() {
        return Err(FIELD_CODE_EMPTY.to_string());
    }
    let format = required_string_field(object, "format")?;
    if format.trim().is_empty() {
        return Err(FIELD_FORMAT_EMPTY.to_string());
    }
    Ok(CheckSnippetRequest { code, format })
}

fn required_string_field(
    object: &serde_json::Map<String, Value>,
    field: &str,
) -> std::result::Result<String, String> {
    let Some(value) = object.get(field) else {
        return Err(format!("Missing required field: {field}"));
    };
    let Some(value) = value.as_str() else {
        return Err(format!("Field \"{field}\" must be a string"));
    };
    Ok(value.to_string())
}

fn required_form_field(
    fields: &[(String, String)],
    field: &str,
) -> std::result::Result<String, String> {
    fields
        .iter()
        .find_map(|(name, value)| (name == field).then(|| value.clone()))
        .ok_or_else(|| format!("Missing required field: {field}"))
}

fn json_syntax_error_message(body: &[u8], error: &serde_json::Error) -> String {
    let body = String::from_utf8_lossy(body);
    let trimmed = body.trim_start();
    if let Some(first) = trimmed.chars().next()
        && !matches!(first, '{' | '[' | '"' | '-' | '0'..='9' | 't' | 'f' | 'n')
    {
        let preview = if trimmed.chars().count() > 20 {
            format!("{}...", trimmed.chars().take(17).collect::<String>())
        } else {
            trimmed.to_string()
        };
        return format!("Unexpected token '{first}', \"{preview}\" is not valid JSON");
    }
    error.to_string()
}

fn duplication_statistics(
    duplications: &[SnippetDuplication],
    total_lines: usize,
) -> DuplicationStatistics {
    let mut duplicated = Vec::<usize>::new();
    for duplication in duplications {
        duplicated.extend(
            duplication.snippet_location.start_line..=duplication.snippet_location.end_line,
        );
    }
    duplicated.sort_unstable();
    duplicated.dedup();
    let duplicated_lines = duplicated.len();
    DuplicationStatistics {
        total_duplications: duplications.len(),
        duplicated_lines,
        total_lines,
        percentage_duplicated: percentage(total_lines, duplicated_lines),
    }
}

fn percentage(total: usize, duplicated: usize) -> f64 {
    if total == 0 {
        0.0
    } else {
        ((duplicated as f64 * 10000.0) / total as f64).round() / 100.0
    }
}

fn relative_source_id(path: &str, working_directory: &Path) -> String {
    let path_ref = Path::new(path);
    path_ref
        .strip_prefix(working_directory)
        .ok()
        .and_then(|relative| relative.to_str())
        .map(str::to_string)
        .unwrap_or_else(|| path.to_string())
}

fn slice_fragment(result: &DetectionResult, fragment: &Fragment) -> Option<String> {
    result
        .source_contents
        .get(&fragment.source_id)
        .and_then(|content| content.get(fragment.range[0]..fragment.range[1]))
        .map(str::to_string)
}

fn fragment_line_count(fragment: &Fragment) -> usize {
    fragment.end.line.saturating_sub(fragment.start.line) + 1
}

fn now_rfc3339() -> String {
    OffsetDateTime::now_utc()
        .format(&Rfc3339)
        .unwrap_or_else(|_| "1970-01-01T00:00:00Z".to_string())
}

fn new_mcp_session_id() -> String {
    let mut bytes = [0u8; 16];
    getrandom::fill(&mut bytes).expect("OS random unavailable for MCP session id");
    bytes[6] = (bytes[6] & 0x0f) | 0x40;
    bytes[8] = (bytes[8] & 0x3f) | 0x80;
    let mut session_id = String::with_capacity(36);
    for (index, byte) in bytes.iter().enumerate() {
        if matches!(index, 4 | 6 | 8 | 10) {
            session_id.push('-');
        }
        write!(&mut session_id, "{byte:02x}").expect("write to string");
    }
    session_id
}

const SCAN_IN_PROGRESS: &str = "Please wait for initial scan to complete";
const NOT_INITIALIZED: &str = "Server not initialized. Please wait for initial scan to complete.";
const FIELD_CODE_EMPTY: &str = "Field \"code\" cannot be empty";
const FIELD_FORMAT_EMPTY: &str = "Field \"format\" cannot be empty";

enum CheckPayloadError {
    Validation(String),
    Syntax(String),
}

#[derive(Clone, Debug, Deserialize)]
pub struct CheckSnippetRequest {
    pub code: String,
    pub format: String,
}

#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CheckSnippetResponse {
    pub duplications: Vec<SnippetDuplication>,
    pub statistics: DuplicationStatistics,
}

#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SnippetDuplication {
    pub snippet_location: SnippetLocation,
    pub codebase_location: DuplicationLocation,
    pub lines_count: usize,
}

#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SnippetLocation {
    pub start_line: usize,
    pub end_line: usize,
    pub start_column: usize,
    pub end_column: usize,
}

impl SnippetLocation {
    fn from_fragment(fragment: &Fragment) -> Self {
        Self {
            start_line: fragment.start.line,
            end_line: fragment.end.line,
            start_column: fragment.start.column,
            end_column: fragment.end.column,
        }
    }
}

#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DuplicationLocation {
    pub file: String,
    pub start_line: usize,
    pub end_line: usize,
    pub start_column: usize,
    pub end_column: usize,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fragment: Option<String>,
}

impl DuplicationLocation {
    fn from_fragment(
        fragment: &Fragment,
        working_directory: &Path,
        result: &DetectionResult,
    ) -> Self {
        Self {
            file: relative_source_id(&fragment.source_id, working_directory),
            start_line: fragment.start.line,
            end_line: fragment.end.line,
            start_column: fragment.start.column,
            end_column: fragment.end.column,
            fragment: slice_fragment(result, fragment),
        }
    }
}

#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DuplicationStatistics {
    pub total_duplications: usize,
    pub duplicated_lines: usize,
    pub total_lines: usize,
    pub percentage_duplicated: f64,
}

#[derive(Clone, Debug, Serialize)]
pub struct StatsResponse {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub statistics: Option<Statistics>,
    pub timestamp: String,
}

#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct HealthResponse {
    pub status: &'static str,
    pub working_directory: String,
    pub last_scan_time: Option<String>,
}

#[derive(Clone, Debug, Serialize)]
pub struct ApiInfoResponse {
    pub name: &'static str,
    pub version: &'static str,
    pub endpoints: std::collections::BTreeMap<String, String>,
    pub documentation: &'static str,
}

#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ErrorResponse {
    pub error: String,
    pub message: String,
    pub status_code: u16,
}

#[derive(Clone, Debug, Serialize)]
pub struct RecheckResponse {
    pub message: &'static str,
}

#[cfg(test)]
mod tests {
    use std::fs;
    use std::sync::atomic::{AtomicU64, Ordering};
    use std::time::{SystemTime, UNIX_EPOCH};

    use axum::body::{Body, to_bytes};
    use axum::http::header::CONTENT_TYPE;
    use axum::http::{Request, StatusCode};
    use serde_json::Value;
    use tower::ServiceExt;

    use crate::cli::Options;

    use super::*;

    static TEMP_PROJECT_COUNTER: AtomicU64 = AtomicU64::new(0);

    fn fixture_project() -> PathBuf {
        let mut path = std::env::temp_dir();
        let stamp = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("time")
            .as_nanos();
        let counter = TEMP_PROJECT_COUNTER.fetch_add(1, Ordering::Relaxed);
        path.push(format!(
            "jscpd-rs-server-{}-{stamp}-{counter}",
            std::process::id()
        ));
        fs::create_dir_all(&path).expect("create temp project");
        let content = "const alpha = 1;\nconst beta = 2;\nconst gamma = alpha + beta;\n";
        fs::write(path.join("a.js"), content).expect("write a.js");
        fs::write(path.join("b.js"), content).expect("write b.js");
        path
    }

    fn service_for(path: &Path) -> ServerService {
        let options = Options {
            paths: vec![path.to_path_buf()],
            min_tokens: 5,
            min_lines: 2,
            max_size_bytes: 1024 * 1024,
            ..Options::default()
        };
        ServerService::new(path.to_path_buf(), options)
    }

    #[test]
    fn server_initialization_populates_stats_and_health() {
        let path = fixture_project();
        let service = service_for(&path);

        service.initialize().expect("initialize");

        let stats = service.statistics();
        assert!(stats.statistics.is_some());
        assert!(stats.timestamp.contains('T'));
        let health = service.health();
        assert_eq!(health.status, "ready");
        assert_eq!(health.working_directory, path.display().to_string());
        assert!(health.last_scan_time.is_some());
        fs::remove_dir_all(path).ok();
    }

    #[test]
    fn server_host_binding_preserves_upstream_display_host() {
        let true_addr = server_bind_address("true", 3000).expect("true host bind");
        assert_eq!(true_addr.ip().to_string(), "0.0.0.0");
        assert_eq!(true_addr.port(), 3000);
        assert_eq!(server_display_url("true", 3000), "http://true:3000");
        assert_eq!(
            server_display_url("localhost", 3001),
            "http://localhost:3001"
        );
    }

    #[test]
    fn server_check_snippet_reports_codebase_duplications() {
        let path = fixture_project();
        let service = service_for(&path);
        service.initialize().expect("initialize");

        let response = service
            .check_snippet(CheckSnippetRequest {
                code: "const alpha = 1;\nconst beta = 2;\nconst gamma = alpha + beta;\n"
                    .to_string(),
                format: "javascript".to_string(),
            })
            .expect("check snippet");

        assert!(!response.duplications.is_empty());
        assert_eq!(
            response.statistics.total_duplications,
            response.duplications.len()
        );
        assert!(response.statistics.duplicated_lines > 0);
        assert!(
            response
                .duplications
                .iter()
                .all(|duplication| !duplication.codebase_location.file.starts_with("<snippet>"))
        );
        fs::remove_dir_all(path).ok();
    }

    #[test]
    fn server_check_snippet_rejects_empty_code() {
        let path = fixture_project();
        let service = service_for(&path);
        service.initialize().expect("initialize");

        let error = service
            .check_snippet(CheckSnippetRequest {
                code: "   ".to_string(),
                format: "javascript".to_string(),
            })
            .expect_err("empty code should fail");

        assert_eq!(error.to_string(), FIELD_CODE_EMPTY);
        fs::remove_dir_all(path).ok();
    }

    #[test]
    fn server_recheck_refreshes_statistics() {
        let path = fixture_project();
        let service = service_for(&path);
        service.initialize().expect("initialize");
        let before = service
            .statistics()
            .statistics
            .expect("stats before")
            .total
            .sources;
        fs::write(path.join("c.js"), "const unique = 1;\n").expect("write c.js");

        service.recheck().expect("recheck");

        let after = service
            .statistics()
            .statistics
            .expect("stats after")
            .total
            .sources;
        assert!(after > before);
        fs::remove_dir_all(path).ok();
    }

    #[test]
    fn server_scan_uses_working_directory_over_config_paths_like_upstream() {
        let working = fixture_project();
        let configured = fixture_project();
        fs::write(configured.join("c.js"), "const configured = 1;\n").expect("write c.js");
        let options = Options {
            paths: vec![configured.clone()],
            min_tokens: 5,
            min_lines: 2,
            max_size_bytes: 1024 * 1024,
            ..Options::default()
        };
        let service = ServerService::new(working.clone(), options);

        service.initialize().expect("initialize");

        let stats = service.statistics().statistics.expect("statistics");
        assert_eq!(stats.total.sources, 2);
        fs::remove_dir_all(working).ok();
        fs::remove_dir_all(configured).ok();
    }

    #[tokio::test]
    async fn server_check_snippet_accepts_form_urlencoded_body() {
        let path = fixture_project();
        let service = service_for(&path);
        service.initialize().expect("initialize");
        let app = create_router(service);
        let body = form_urlencoded::Serializer::new(String::new())
            .append_pair(
                "code",
                "const alpha = 1;\nconst beta = 2;\nconst gamma = alpha + beta;\n",
            )
            .append_pair("format", "javascript")
            .finish();

        let response = app
            .oneshot(
                Request::builder()
                    .method("POST")
                    .uri("/api/check")
                    .header(CONTENT_TYPE, "application/x-www-form-urlencoded")
                    .body(Body::from(body))
                    .expect("request"),
            )
            .await
            .expect("response");

        assert_eq!(response.status(), StatusCode::OK);
        let body = to_bytes(response.into_body(), usize::MAX)
            .await
            .expect("body");
        let body: Value = serde_json::from_slice(&body).expect("json body");
        assert!(body["duplications"].is_array());
        assert_eq!(
            body["statistics"]["totalDuplications"].as_u64(),
            body["duplications"]
                .as_array()
                .map(|items| items.len() as u64)
        );
        fs::remove_dir_all(path).ok();
    }

    #[tokio::test]
    async fn server_check_snippet_invalid_json_matches_upstream_error() {
        let path = fixture_project();
        let service = service_for(&path);
        service.initialize().expect("initialize");
        let app = create_router(service);

        let response = app
            .oneshot(
                Request::builder()
                    .method("POST")
                    .uri("/api/check")
                    .header(CONTENT_TYPE, "application/json")
                    .body(Body::from("invalid-json"))
                    .expect("request"),
            )
            .await
            .expect("response");

        assert_eq!(response.status(), StatusCode::BAD_REQUEST);
        let body = to_bytes(response.into_body(), usize::MAX)
            .await
            .expect("body");
        let body: Value = serde_json::from_slice(&body).expect("json body");
        assert_eq!(body["error"], "SyntaxError");
        assert_eq!(
            body["message"],
            "Unexpected token 'i', \"invalid-json\" is not valid JSON"
        );
        assert_eq!(body["statusCode"], 400);
        fs::remove_dir_all(path).ok();
    }

    #[tokio::test]
    async fn server_check_snippet_rejects_non_string_format_like_upstream() {
        let path = fixture_project();
        let service = service_for(&path);
        service.initialize().expect("initialize");
        let app = create_router(service);

        let response = app
            .oneshot(
                Request::builder()
                    .method("POST")
                    .uri("/api/check")
                    .header(CONTENT_TYPE, "application/json")
                    .body(Body::from(r#"{"code":"console.log(1);","format":123}"#))
                    .expect("request"),
            )
            .await
            .expect("response");

        assert_eq!(response.status(), StatusCode::BAD_REQUEST);
        let body = to_bytes(response.into_body(), usize::MAX)
            .await
            .expect("body");
        let body: Value = serde_json::from_slice(&body).expect("json body");
        assert_eq!(body["error"], "ValidationError");
        assert_eq!(body["message"], "Field \"format\" must be a string");
        assert_eq!(body["statusCode"], 400);
        fs::remove_dir_all(path).ok();
    }

    #[tokio::test]
    async fn server_uninitialized_api_matches_upstream_error_shapes() {
        let path = fixture_project();
        let service = service_for(&path);
        let app = create_router(service);

        let check_response = app
            .clone()
            .oneshot(
                Request::builder()
                    .method("POST")
                    .uri("/api/check")
                    .header(CONTENT_TYPE, "application/json")
                    .body(Body::from(
                        r#"{"code":"console.log(\"test\");","format":"javascript"}"#,
                    ))
                    .expect("request"),
            )
            .await
            .expect("response");

        assert_eq!(check_response.status(), StatusCode::BAD_REQUEST);
        let body = to_bytes(check_response.into_body(), usize::MAX)
            .await
            .expect("body");
        let body: Value = serde_json::from_slice(&body).expect("json body");
        assert_eq!(body["error"], "Error");
        assert_eq!(body["message"], NOT_INITIALIZED);
        assert_eq!(body["statusCode"], 400);

        let stats_response = app
            .clone()
            .oneshot(
                Request::builder()
                    .method("GET")
                    .uri("/api/stats")
                    .body(Body::empty())
                    .expect("request"),
            )
            .await
            .expect("response");

        assert_eq!(stats_response.status(), StatusCode::SERVICE_UNAVAILABLE);
        let body = to_bytes(stats_response.into_body(), usize::MAX)
            .await
            .expect("body");
        let body: Value = serde_json::from_slice(&body).expect("json body");
        assert_eq!(body["error"], "NotReady");
        assert_eq!(
            body["message"],
            "Statistics not available yet. Server is still initializing."
        );
        assert_eq!(body["statusCode"], 503);

        let health_response = app
            .oneshot(
                Request::builder()
                    .method("GET")
                    .uri("/api/health")
                    .body(Body::empty())
                    .expect("request"),
            )
            .await
            .expect("response");

        assert_eq!(health_response.status(), StatusCode::OK);
        let body = to_bytes(health_response.into_body(), usize::MAX)
            .await
            .expect("body");
        let body: Value = serde_json::from_slice(&body).expect("json body");
        assert!(matches!(
            body["status"].as_str(),
            Some("ready" | "initializing")
        ));
        assert_eq!(body["workingDirectory"], path.display().to_string());
        assert_eq!(body["lastScanTime"], Value::Null);
        fs::remove_dir_all(path).ok();
    }

    #[tokio::test]
    async fn server_unknown_routes_return_upstream_style_json_error() {
        let path = fixture_project();
        let service = service_for(&path);
        let app = create_router(service);

        let response = app
            .oneshot(
                Request::builder()
                    .method("GET")
                    .uri("/api/unknown?ignored=true")
                    .body(Body::empty())
                    .expect("request"),
            )
            .await
            .expect("response");

        assert_eq!(response.status(), StatusCode::NOT_FOUND);
        let body = to_bytes(response.into_body(), usize::MAX)
            .await
            .expect("body");
        let body: Value = serde_json::from_slice(&body).expect("json body");
        assert_eq!(body["error"], "NotFound");
        assert_eq!(body["message"], "Route GET /api/unknown not found");
        assert_eq!(body["statusCode"], 404);
        fs::remove_dir_all(path).ok();
    }

    #[tokio::test]
    async fn server_wrong_api_methods_return_upstream_style_not_found() {
        let path = fixture_project();
        let service = service_for(&path);
        let app = create_router(service);

        for (method, uri) in [
            ("GET", "/api/check"),
            ("GET", "/api/recheck"),
            ("POST", "/api/stats"),
            ("POST", "/api/health"),
            ("PUT", "/api/check"),
            ("DELETE", "/api/stats"),
        ] {
            let response = app
                .clone()
                .oneshot(
                    Request::builder()
                        .method(method)
                        .uri(uri)
                        .body(Body::empty())
                        .expect("request"),
                )
                .await
                .expect("response");

            assert_eq!(response.status(), StatusCode::NOT_FOUND);
            let body = to_bytes(response.into_body(), usize::MAX)
                .await
                .expect("body");
            let body: Value = serde_json::from_slice(&body).expect("json body");
            assert_eq!(body["error"], "NotFound");
            assert_eq!(body["message"], format!("Route {method} {uri} not found"));
            assert_eq!(body["statusCode"], 404);
        }
        fs::remove_dir_all(path).ok();
    }

    #[tokio::test]
    async fn server_unsupported_mcp_methods_return_upstream_style_not_found() {
        let path = fixture_project();
        let service = service_for(&path);
        let app = create_router(service);

        for method in ["DELETE", "OPTIONS"] {
            let response = app
                .clone()
                .oneshot(
                    Request::builder()
                        .method(method)
                        .uri("/mcp")
                        .body(Body::empty())
                        .expect("request"),
                )
                .await
                .expect("response");

            assert_eq!(response.status(), StatusCode::NOT_FOUND);
            let body = to_bytes(response.into_body(), usize::MAX)
                .await
                .expect("body");
            let body: Value = serde_json::from_slice(&body).expect("json body");
            assert_eq!(body["error"], "NotFound");
            assert_eq!(body["message"], format!("Route {method} /mcp not found"));
            assert_eq!(body["statusCode"], 404);
        }
        fs::remove_dir_all(path).ok();
    }
}