Repository navigation
Expand file tree
/
Copy pathtest_intent_parser.js
More file actions
123 lines (104 loc) · 4.62 KB
/
Copy pathtest_intent_parser.js
File metadata and controls
123 lines (104 loc) · 4.62 KB
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
// Validation Test Suite: Intent Recognition Heuristics
// Verifies that the natural language intent parser correctly routes expressions.
function detectIntentMock(text) {
// Strip trailing punctuation and global commas to preserve extensions like .pdf
let cleanedText = text.trim().replace(/[.?!]+$/g, '').replace(/,/g, '');
// Recursively strip conversational prefixes and suffixes
let prevText;
do {
prevText = cleanedText;
cleanedText = cleanedText
.replace(/^(?:please|can you|could you|would you mind|would you|hey luna|luna|i want to|want to|to|go and|go)\s+/i, '')
.replace(/\s+(?:please|for me|now)$/i, '');
} while (cleanedText !== prevText);
cleanedText = cleanedText.trim();
const input = cleanedText.toLowerCase();
// 1. Open Application (uses original casing text)
const openMatch = cleanedText.match(/^(?:open|launch|start)\s+(?:the\s+)?([a-zA-Z0-9\s.\-_]+)$/i);
if (openMatch) {
return {
action: 'open-app',
appName: openMatch[1].trim(),
};
}
// 2. Organize Files
const isCleanAction = input.includes('organize') || input.includes('clean') || input.includes('tidy');
const isTargetFolder = input.includes('download') || input.includes('folder') || input.includes('desk');
if (isCleanAction && isTargetFolder) {
return {
action: 'organize-files'
};
}
// 3. Search Local Files (uses original casing text)
const findMatch = cleanedText.match(/(?:find|search)\s+(?:files?|documents?)\s+(?:named|with|for)?\s*["']?([a-zA-Z0-9\s.\-_]+)["']?/i) ||
cleanedText.match(/(?:find|search)\s+(?:my)?\s*["']?([a-zA-Z0-9\s.\-_]{3,})["']?$/i);
if (findMatch) {
return {
action: 'search-files',
query: findMatch[1].trim()
};
}
// 4. Create Reminders (uses original casing text)
const remindMatch = cleanedText.match(/^(?:create\s+)?(?:a\s+)?reminder\s+(?:to|for)\s+(.+)$/i) ||
cleanedText.match(/^remind\s+me\s+to\s+(.+)$/i);
if (remindMatch) {
return {
action: 'create-reminder',
title: remindMatch[1].trim()
};
}
return null;
}
// Test cases
const testCases = [
{ input: "Open Safari", expected: { action: "open-app", appName: "Safari" } },
{ input: "launch Spotify", expected: { action: "open-app", appName: "Spotify" } },
{ input: "Please start Xcode", expected: { action: "open-app", appName: "Xcode" } },
{ input: "open apple maps", expected: { action: "open-app", appName: "apple maps" } },
{ input: "Hey Luna, can you open Safari please?", expected: { action: "open-app", appName: "Safari" } },
{ input: "organize downloads folder", expected: { action: "organize-files" } },
{ input: "clean my Downloads folder", expected: { action: "organize-files" } },
{ input: "find my resume", expected: { action: "search-files", query: "resume" } },
{ input: "search files named invoice.pdf", expected: { action: "search-files", query: "invoice.pdf" } },
{ input: "find project-notes", expected: { action: "search-files", query: "project-notes" } },
{ input: "remind me to buy groceries", expected: { action: "create-reminder", title: "buy groceries" } },
{ input: "create a reminder for team sync tomorrow", expected: { action: "create-reminder", title: "team sync tomorrow" } },
{ input: "How are you doing today?", expected: null } // standard chat fallback
];
let failed = 0;
console.log("=== STARTING LUNA INTENT PARSER TESTS ===");
testCases.forEach((tc, idx) => {
const result = detectIntentMock(tc.input);
let passed = false;
if (tc.expected === null) {
passed = result === null;
} else if (result && result.action === tc.expected.action) {
if (result.action === 'open-app') {
passed = result.appName === tc.expected.appName;
} else if (result.action === 'search-files') {
passed = result.query === tc.expected.query;
} else if (result.action === 'create-reminder') {
passed = result.title === tc.expected.title;
} else {
passed = true; // organize-files
}
}
if (passed) {
console.log(`[PASS] Case #${idx + 1}: "${tc.input}" -> Action: ${result ? result.action : 'Chat Fallback'}`);
} else {
failed++;
console.log(`[FAIL] Case #${idx + 1}: "${tc.input}"`);
console.log(` Expected: ${JSON.stringify(tc.expected)}`);
console.log(` Got: ${JSON.stringify(result)}`);
}
});
console.log("\n=== TEST RESULTS ===");
console.log(`Total tests run: ${testCases.length}`);
console.log(`Passed: ${testCases.length - failed}`);
console.log(`Failed: ${failed}`);
if (failed > 0) {
process.exit(1);
} else {
console.log("All intent recognizer validation checks passed!");
process.exit(0);
}