pmat 3.11.0

PMAT - Zero-config AI context generation and code quality toolkit (CLI, MCP, HTTP)
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
#[cfg(test)]
mod tests {
    use super::*;

    fn create_test_commit(hash: &str, subject: &str) -> CommitInfo {
        CommitInfo {
            hash: hash.to_string(),
            message_subject: subject.to_string(),
            message_body: None,
            author_name: "Test Author".to_string(),
            author_email: "test@example.com".to_string(),
            timestamp: 1700000000,
            is_merge: false,
            is_fix: subject.to_lowercase().contains("fix"),
            is_feat: subject.to_lowercase().contains("feat"),
            issue_refs: vec![],
            files: vec![FileChange {
                path: "src/main.rs".to_string(),
                change_type: ChangeType::Modified,
                lines_added: 10,
                lines_deleted: 5,
            }],
        }
    }

    #[test]
    fn test_create_index_in_memory() {
        let index = GitHistoryIndex::in_memory();
        assert!(index.is_ok());
    }

    #[test]
    fn test_insert_and_count_commits() {
        let mut index = GitHistoryIndex::in_memory().unwrap();

        let commits = vec![
            create_test_commit(&"a".repeat(40), "Fix bug in parser"),
            create_test_commit(&"b".repeat(40), "Add new feature"),
            create_test_commit(&"c".repeat(40), "Refactor module"),
        ];

        let inserted = index.insert_commits(&commits).unwrap();
        assert_eq!(inserted, 3);

        let count = index.commit_count().unwrap();
        assert_eq!(count, 3);
    }

    #[test]
    fn test_insert_skips_non_indexable() {
        let mut index = GitHistoryIndex::in_memory().unwrap();

        let commits = vec![
            create_test_commit(&"a".repeat(40), "Fix bug in parser"), // indexable
            CommitInfo {
                hash: "b".repeat(40),
                message_subject: "wip".to_string(), // too short - not indexable
                message_body: None,
                author_name: "Test".to_string(),
                author_email: "test@example.com".to_string(),
                timestamp: 1700000000,
                is_merge: false,
                is_fix: false,
                is_feat: false,
                issue_refs: vec![],
                files: vec![],
            },
        ];

        let inserted = index.insert_commits(&commits).unwrap();
        assert_eq!(inserted, 1); // Only 1 indexable
    }

    #[test]
    fn test_get_commits_for_file() {
        let mut index = GitHistoryIndex::in_memory().unwrap();

        let commits = vec![
            create_test_commit(&"a".repeat(40), "Fix bug in parser"),
            create_test_commit(&"b".repeat(40), "Add feature to parser"),
        ];

        index.insert_commits(&commits).unwrap();

        let file_commits = index.get_commits_for_file("src/main.rs", 10).unwrap();
        assert_eq!(file_commits.len(), 2);
    }

    #[test]
    fn test_last_indexed_commit() {
        let index = GitHistoryIndex::in_memory().unwrap();

        // Initially none
        assert!(index.get_last_indexed_commit().unwrap().is_none());

        // Set and retrieve
        index.set_last_indexed_commit("abc123").unwrap();
        assert_eq!(
            index.get_last_indexed_commit().unwrap(),
            Some("abc123".to_string())
        );
    }

    #[test]
    fn test_update_embedding() {
        let index = GitHistoryIndex::in_memory().unwrap();

        let commit = create_test_commit(&"a".repeat(40), "Fix important bug");
        index.insert_commit(&commit).unwrap();

        // Update with embedding
        let embedding: Vec<f32> = vec![0.1, 0.2, 0.3];
        index.update_embedding(&commit.hash, &embedding).unwrap();

        // Verify embedding was stored (would need getter to fully test)
        let count = index.commit_count().unwrap();
        assert_eq!(count, 1);
    }

    #[test]
    fn test_checksum_changes_on_insert() {
        let mut index = GitHistoryIndex::in_memory().unwrap();

        let checksum1 = index.checksum().unwrap();

        // Message must be >= 10 chars to be indexable
        let commits = vec![create_test_commit(
            &"a".repeat(40),
            "Fix important bug in parser",
        )];
        index.insert_commits(&commits).unwrap();

        let checksum2 = index.checksum().unwrap();

        assert_ne!(checksum1, checksum2, "Checksum should change after insert");
    }

    // === Incremental Sync Tests (GH-RAG-004) ===

    #[test]
    fn test_sync_incremental_empty_index() {
        let mut index = GitHistoryIndex::in_memory().unwrap();

        let commits = vec![
            create_test_commit(&"a".repeat(40), "First commit message"),
            create_test_commit(&"b".repeat(40), "Second commit message"),
        ];

        let result = index.sync_incremental(&commits).unwrap();

        assert_eq!(result.commits_added, 2);
        assert_eq!(result.commits_skipped, 0);
        assert!(result.last_commit.is_some());
    }

    #[test]
    fn test_sync_incremental_adds_only_new() {
        let mut index = GitHistoryIndex::in_memory().unwrap();

        // Initial sync
        let commits1 = vec![create_test_commit(&"a".repeat(40), "First commit message")];
        index.sync_incremental(&commits1).unwrap();

        // Second sync with existing + new
        let commits2 = vec![
            create_test_commit(&"a".repeat(40), "First commit message"), // existing
            create_test_commit(&"b".repeat(40), "Second commit message"), // new
        ];

        let result = index.sync_incremental(&commits2).unwrap();

        assert_eq!(result.commits_added, 1, "Should only add 1 new commit");
        assert_eq!(index.commit_count().unwrap(), 2);
    }

    #[test]
    fn test_sync_incremental_skips_non_indexable() {
        let mut index = GitHistoryIndex::in_memory().unwrap();

        let commits = vec![
            create_test_commit(&"a".repeat(40), "Valid commit message"),
            CommitInfo {
                hash: "b".repeat(40),
                message_subject: "wip".to_string(), // too short
                message_body: None,
                author_name: "Test".to_string(),
                author_email: "test@example.com".to_string(),
                timestamp: 1700000001,
                is_merge: false,
                is_fix: false,
                is_feat: false,
                issue_refs: vec![],
                files: vec![],
            },
        ];

        let result = index.sync_incremental(&commits).unwrap();

        assert_eq!(result.commits_added, 1);
        assert_eq!(result.commits_skipped, 1);
    }

    #[test]
    fn test_sync_incremental_updates_last_indexed() {
        let mut index = GitHistoryIndex::in_memory().unwrap();

        assert!(index.get_last_indexed_commit().unwrap().is_none());

        let commits = vec![create_test_commit(&"a".repeat(40), "First commit message")];

        let result = index.sync_incremental(&commits).unwrap();

        assert!(result.last_commit.is_some());
        assert_eq!(index.get_last_indexed_commit().unwrap(), result.last_commit);
    }

    #[test]
    fn test_commit_exists() {
        let mut index = GitHistoryIndex::in_memory().unwrap();

        let hash = "a".repeat(40);
        assert!(!index.commit_exists(&hash).unwrap());

        let commits = vec![create_test_commit(&hash, "Test commit message")];
        index.insert_commits(&commits).unwrap();

        assert!(index.commit_exists(&hash).unwrap());
    }

    #[test]
    fn test_get_commits_since() {
        let mut index = GitHistoryIndex::in_memory().unwrap();

        let commits = vec![
            CommitInfo {
                hash: "a".repeat(40),
                message_subject: "Old commit message".to_string(),
                message_body: None,
                author_name: "Test".to_string(),
                author_email: "test@example.com".to_string(),
                timestamp: 1000000000,
                is_merge: false,
                is_fix: false,
                is_feat: false,
                issue_refs: vec![],
                files: vec![],
            },
            CommitInfo {
                hash: "b".repeat(40),
                message_subject: "New commit message".to_string(),
                message_body: None,
                author_name: "Test".to_string(),
                author_email: "test@example.com".to_string(),
                timestamp: 2000000000,
                is_merge: false,
                is_fix: false,
                is_feat: false,
                issue_refs: vec![],
                files: vec![],
            },
        ];

        index.insert_commits(&commits).unwrap();

        // Get commits since midpoint
        let recent = index.get_commits_since(1500000000, 10).unwrap();
        assert_eq!(recent.len(), 1);
        assert_eq!(recent[0].message_subject, "New commit message");
    }

    #[test]
    fn test_sync_empty_input() {
        let mut index = GitHistoryIndex::in_memory().unwrap();

        let result = index.sync_incremental(&[]).unwrap();

        assert_eq!(result.commits_added, 0);
        assert_eq!(result.commits_skipped, 0);
    }

    #[test]
    fn test_sync_all_already_present() {
        let mut index = GitHistoryIndex::in_memory().unwrap();

        let commits = vec![create_test_commit(&"a".repeat(40), "Test commit message")];

        // First sync
        index.sync_incremental(&commits).unwrap();

        // Second sync with same commits
        let result = index.sync_incremental(&commits).unwrap();

        assert_eq!(result.commits_added, 0);
        assert_eq!(index.commit_count().unwrap(), 1);
    }

    #[test]
    fn test_get_commits_without_embeddings() {
        let index = GitHistoryIndex::in_memory().unwrap();

        let commit = create_test_commit(&"a".repeat(40), "Fix bug in parser");
        index.insert_commit(&commit).unwrap();

        let needs_embedding = index.get_commits_without_embeddings(10).unwrap();
        assert_eq!(needs_embedding.len(), 1);
        assert_eq!(needs_embedding[0], "a".repeat(40));

        // After setting embedding, should not appear
        index.update_embedding(&commit.hash, &[0.1, 0.2]).unwrap();
        let needs_embedding = index.get_commits_without_embeddings(10).unwrap();
        assert!(needs_embedding.is_empty());
    }

    #[test]
    fn test_commit_info_full_message_no_body() {
        let info = CommitInfo {
            hash: "a".repeat(40),
            message_subject: "Fix bug".to_string(),
            message_body: None,
            author_name: "Test".to_string(),
            author_email: "test@example.com".to_string(),
            timestamp: 1700000000,
            is_merge: false,
            is_fix: true,
            is_feat: false,
            issue_refs: vec![],
            files: vec![],
        };
        assert_eq!(info.full_message(), "Fix bug");
    }

    #[test]
    fn test_commit_info_full_message_empty_body() {
        let info = CommitInfo {
            hash: "a".repeat(40),
            message_subject: "Fix bug".to_string(),
            message_body: Some("".to_string()),
            author_name: "Test".to_string(),
            author_email: "test@example.com".to_string(),
            timestamp: 1700000000,
            is_merge: false,
            is_fix: true,
            is_feat: false,
            issue_refs: vec![],
            files: vec![],
        };
        assert_eq!(info.full_message(), "Fix bug");
    }

    #[test]
    fn test_commit_info_is_indexable_merge_with_custom_message() {
        let info = CommitInfo {
            hash: "a".repeat(40),
            message_subject: "Custom merge: integrate feature X".to_string(),
            message_body: None,
            author_name: "Test".to_string(),
            author_email: "test@example.com".to_string(),
            timestamp: 1700000000,
            is_merge: true,
            is_fix: false,
            is_feat: false,
            issue_refs: vec![],
            files: vec![],
        };
        // Merge with custom message (not starting with "Merge ") IS indexable
        assert!(info.is_indexable());
    }

    #[test]
    fn test_change_type_as_str() {
        assert_eq!(ChangeType::Added.as_str(), "A");
        assert_eq!(ChangeType::Modified.as_str(), "M");
        assert_eq!(ChangeType::Deleted.as_str(), "D");
        assert_eq!(ChangeType::Renamed.as_str(), "R");
    }

    #[test]
    fn test_checksum_empty_index() {
        let index = GitHistoryIndex::in_memory().unwrap();
        let checksum = index.checksum().unwrap();
        assert_eq!(checksum, "0:");
    }

    // Falsification Test F1: Index Independence
    #[test]
    fn falsify_git_index_isolation() {
        // This test verifies that git index operations don't affect code index
        // In a real scenario, we'd have both indexes and verify cross-contamination
        let mut index = GitHistoryIndex::in_memory().unwrap();

        let initial_checksum = index.checksum().unwrap();

        // Insert commits
        let commits = vec![
            create_test_commit(&"a".repeat(40), "Fix bug in parser"),
            create_test_commit(&"b".repeat(40), "Add new feature"),
        ];
        index.insert_commits(&commits).unwrap();

        let final_checksum = index.checksum().unwrap();

        // Git index MUST have changed
        assert_ne!(
            initial_checksum, final_checksum,
            "FALSIFIED: Git index not updated after commit insertion"
        );

        // Verify we can get meaningful data back
        let count = index.commit_count().unwrap();
        assert_eq!(count, 2, "FALSIFIED: Commit count mismatch");
    }
}