|
| 1 | +jest.mock('@ai-sdk/openai', () => ({ openai: jest.fn() })); |
| 2 | +jest.mock('ai', () => ({ |
| 3 | + generateObject: jest.fn(), |
| 4 | + jsonSchema: jest.fn((s) => s), |
| 5 | +})); |
| 6 | + |
| 7 | +import { |
| 8 | + looksLikeQuestionLine, |
| 9 | + buildQuestionAwareChunks, |
| 10 | + estimateQuestionCount, |
| 11 | +} from './question-parser'; |
| 12 | + |
| 13 | +const CHUNK_OPTS = { |
| 14 | + maxChunkChars: 80_000, |
| 15 | + minChunkChars: 5_000, |
| 16 | + maxQuestionsPerChunk: 1, |
| 17 | +}; |
| 18 | + |
| 19 | +describe('looksLikeQuestionLine', () => { |
| 20 | + describe('existing patterns (regression guard)', () => { |
| 21 | + it('detects question marks', () => { |
| 22 | + expect(looksLikeQuestionLine('Do you have a security policy?')).toBe( |
| 23 | + true, |
| 24 | + ); |
| 25 | + expect(looksLikeQuestionLine('Company Name:何ですか?')).toBe(true); |
| 26 | + }); |
| 27 | + |
| 28 | + it('detects "Question:" labels', () => { |
| 29 | + expect(looksLikeQuestionLine('[Question] SQ14.3')).toBe(false); |
| 30 | + expect( |
| 31 | + looksLikeQuestionLine('Question: Do you encrypt data at rest'), |
| 32 | + ).toBe(true); |
| 33 | + expect(looksLikeQuestionLine('question : Describe your BCP')).toBe(true); |
| 34 | + }); |
| 35 | + |
| 36 | + it('detects explicit question/Q prefix', () => { |
| 37 | + expect(looksLikeQuestionLine('Q1. What is your data retention?')).toBe( |
| 38 | + true, |
| 39 | + ); |
| 40 | + expect(looksLikeQuestionLine('Question 5: Describe controls')).toBe(true); |
| 41 | + }); |
| 42 | + |
| 43 | + it('detects interrogative-starting lines', () => { |
| 44 | + expect( |
| 45 | + looksLikeQuestionLine('What security certifications do you hold'), |
| 46 | + ).toBe(true); |
| 47 | + expect( |
| 48 | + looksLikeQuestionLine('How do you handle data breaches'), |
| 49 | + ).toBe(true); |
| 50 | + expect(looksLikeQuestionLine('Is data encrypted in transit')).toBe(true); |
| 51 | + expect(looksLikeQuestionLine('Are backups tested regularly')).toBe(true); |
| 52 | + expect(looksLikeQuestionLine('Does the company have SOC 2')).toBe(true); |
| 53 | + expect(looksLikeQuestionLine('Can users export their data')).toBe(true); |
| 54 | + expect( |
| 55 | + looksLikeQuestionLine('Describe your incident response plan'), |
| 56 | + ).toBe(true); |
| 57 | + expect(looksLikeQuestionLine('List all subprocessors')).toBe(true); |
| 58 | + }); |
| 59 | + |
| 60 | + it('detects numbered questions with interrogatives', () => { |
| 61 | + expect(looksLikeQuestionLine('06. Do you have a BCP?')).toBe(true); |
| 62 | + expect(looksLikeQuestionLine('1) What is your uptime SLA')).toBe(true); |
| 63 | + expect(looksLikeQuestionLine('Q1: How do you handle PII')).toBe(true); |
| 64 | + }); |
| 65 | + |
| 66 | + it('detects form-style numbered fields', () => { |
| 67 | + expect(looksLikeQuestionLine('1.1 Vendor Name')).toBe(true); |
| 68 | + expect(looksLikeQuestionLine('2.3 Contact Email')).toBe(true); |
| 69 | + expect(looksLikeQuestionLine('1.4 Company Address')).toBe(true); |
| 70 | + }); |
| 71 | + |
| 72 | + it('detects required markers', () => { |
| 73 | + expect(looksLikeQuestionLine('Company legal name *')).toBe(true); |
| 74 | + }); |
| 75 | + |
| 76 | + it('detects selection notes', () => { |
| 77 | + expect( |
| 78 | + looksLikeQuestionLine( |
| 79 | + 'Primary data center location (Single selection allowed)', |
| 80 | + ), |
| 81 | + ).toBe(true); |
| 82 | + expect( |
| 83 | + looksLikeQuestionLine( |
| 84 | + 'Which certifications (Multiple selections allowed)', |
| 85 | + ), |
| 86 | + ).toBe(true); |
| 87 | + }); |
| 88 | + |
| 89 | + it('rejects empty lines and pure metadata', () => { |
| 90 | + expect(looksLikeQuestionLine('')).toBe(false); |
| 91 | + expect(looksLikeQuestionLine(' ')).toBe(false); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + describe('compliance-statement patterns (new)', () => { |
| 96 | + it('detects "The organization" statements', () => { |
| 97 | + expect( |
| 98 | + looksLikeQuestionLine( |
| 99 | + 'The organization must determine the respective roles and responsibilities', |
| 100 | + ), |
| 101 | + ).toBe(true); |
| 102 | + expect( |
| 103 | + looksLikeQuestionLine( |
| 104 | + 'The organization has entered into a contract with the PII data controller', |
| 105 | + ), |
| 106 | + ).toBe(true); |
| 107 | + expect( |
| 108 | + looksLikeQuestionLine( |
| 109 | + 'The organization ensures that temporary files are deleted', |
| 110 | + ), |
| 111 | + ).toBe(true); |
| 112 | + expect( |
| 113 | + looksLikeQuestionLine( |
| 114 | + 'The organization conducts a risk analysis regarding PII processing', |
| 115 | + ), |
| 116 | + ).toBe(true); |
| 117 | + expect( |
| 118 | + looksLikeQuestionLine( |
| 119 | + 'The organization implements procedures and technical solutions', |
| 120 | + ), |
| 121 | + ).toBe(true); |
| 122 | + }); |
| 123 | + |
| 124 | + it('detects "The company/vendor/supplier" statements', () => { |
| 125 | + expect( |
| 126 | + looksLikeQuestionLine( |
| 127 | + 'The company maintains a processing register for compliance', |
| 128 | + ), |
| 129 | + ).toBe(true); |
| 130 | + expect( |
| 131 | + looksLikeQuestionLine( |
| 132 | + 'The vendor provides the data controller with appropriate information', |
| 133 | + ), |
| 134 | + ).toBe(true); |
| 135 | + expect( |
| 136 | + looksLikeQuestionLine( |
| 137 | + 'The supplier has defined policies for managing data subject requests', |
| 138 | + ), |
| 139 | + ).toBe(true); |
| 140 | + }); |
| 141 | + |
| 142 | + it('detects "Our organization/company/team" statements', () => { |
| 143 | + expect( |
| 144 | + looksLikeQuestionLine( |
| 145 | + 'Our organization maintains SOC 2 Type II certification', |
| 146 | + ), |
| 147 | + ).toBe(true); |
| 148 | + expect( |
| 149 | + looksLikeQuestionLine('Our company encrypts all data at rest'), |
| 150 | + ).toBe(true); |
| 151 | + expect( |
| 152 | + looksLikeQuestionLine('Our team conducts quarterly security reviews'), |
| 153 | + ).toBe(true); |
| 154 | + }); |
| 155 | + |
| 156 | + it('does NOT match ambiguous "We X" lines (could be answers)', () => { |
| 157 | + expect( |
| 158 | + looksLikeQuestionLine('We retain data for 90 days.'), |
| 159 | + ).toBe(false); |
| 160 | + expect( |
| 161 | + looksLikeQuestionLine('We follow our IRP documented in SOC 2.'), |
| 162 | + ).toBe(false); |
| 163 | + }); |
| 164 | + |
| 165 | + it('does NOT false-positive on section headers and metadata', () => { |
| 166 | + expect( |
| 167 | + looksLikeQuestionLine('Information Security Program'), |
| 168 | + ).toBe(false); |
| 169 | + expect(looksLikeQuestionLine('General Information')).toBe(false); |
| 170 | + expect(looksLikeQuestionLine('Section 2: Data Protection')).toBe(false); |
| 171 | + expect(looksLikeQuestionLine('Acme Corp')).toBe(false); |
| 172 | + expect(looksLikeQuestionLine('2026-01-15')).toBe(false); |
| 173 | + expect(looksLikeQuestionLine('Version 3.0')).toBe(false); |
| 174 | + expect(looksLikeQuestionLine('Confidential')).toBe(false); |
| 175 | + }); |
| 176 | + |
| 177 | + it('handles case-insensitive matching', () => { |
| 178 | + expect( |
| 179 | + looksLikeQuestionLine('THE ORGANIZATION MUST PROVIDE EVIDENCE'), |
| 180 | + ).toBe(true); |
| 181 | + expect( |
| 182 | + looksLikeQuestionLine('the organization supports the data controller'), |
| 183 | + ).toBe(true); |
| 184 | + }); |
| 185 | + }); |
| 186 | +}); |
| 187 | + |
| 188 | +describe('buildQuestionAwareChunks', () => { |
| 189 | + it('returns empty for empty input', () => { |
| 190 | + expect(buildQuestionAwareChunks('', CHUNK_OPTS)).toEqual([]); |
| 191 | + expect(buildQuestionAwareChunks(' ', CHUNK_OPTS)).toEqual([]); |
| 192 | + }); |
| 193 | + |
| 194 | + it('chunks interrogative questions one per chunk', () => { |
| 195 | + const content = [ |
| 196 | + 'What is your data retention policy?', |
| 197 | + 'We retain data for 90 days.', |
| 198 | + 'How do you handle security incidents?', |
| 199 | + 'We follow our IRP documented in SOC 2.', |
| 200 | + 'Do you encrypt data at rest?', |
| 201 | + 'Yes, AES-256.', |
| 202 | + ].join('\n'); |
| 203 | + |
| 204 | + const chunks = buildQuestionAwareChunks(content, CHUNK_OPTS); |
| 205 | + expect(chunks.length).toBe(3); |
| 206 | + expect(chunks[0].content).toContain('data retention policy'); |
| 207 | + expect(chunks[1].content).toContain('security incidents'); |
| 208 | + expect(chunks[2].content).toContain('encrypt data at rest'); |
| 209 | + }); |
| 210 | + |
| 211 | + it('chunks compliance statements one per chunk', () => { |
| 212 | + const content = [ |
| 213 | + 'The organization must determine roles and responsibilities for PII processing.', |
| 214 | + 'The organization has entered into a contract with the PII data controller.', |
| 215 | + 'The organization conducts a risk analysis regarding PII processing.', |
| 216 | + ].join('\n'); |
| 217 | + |
| 218 | + const chunks = buildQuestionAwareChunks(content, CHUNK_OPTS); |
| 219 | + expect(chunks.length).toBe(3); |
| 220 | + expect(chunks[0].content).toContain('roles and responsibilities'); |
| 221 | + expect(chunks[1].content).toContain('entered into a contract'); |
| 222 | + expect(chunks[2].content).toContain('risk analysis'); |
| 223 | + }); |
| 224 | + |
| 225 | + it('handles mixed interrogative + compliance content', () => { |
| 226 | + const content = [ |
| 227 | + 'The organization must have documented procedures for PII deletion.', |
| 228 | + 'How often do you review your data retention policies?', |
| 229 | + 'The organization ensures temporary files are deleted.', |
| 230 | + 'Do you have a DPIA process?', |
| 231 | + ].join('\n'); |
| 232 | + |
| 233 | + const chunks = buildQuestionAwareChunks(content, CHUNK_OPTS); |
| 234 | + expect(chunks.length).toBe(4); |
| 235 | + }); |
| 236 | + |
| 237 | + it('keeps non-question context lines with the preceding question', () => { |
| 238 | + const content = [ |
| 239 | + 'What is your encryption standard?', |
| 240 | + 'Please provide details about key management.', |
| 241 | + 'Additional notes on rotation schedule.', |
| 242 | + 'How do you handle key rotation?', |
| 243 | + ].join('\n'); |
| 244 | + |
| 245 | + const chunks = buildQuestionAwareChunks(content, CHUNK_OPTS); |
| 246 | + expect(chunks.length).toBe(2); |
| 247 | + expect(chunks[0].content).toContain('key management'); |
| 248 | + expect(chunks[0].content).toContain('rotation schedule'); |
| 249 | + expect(chunks[1].content).toContain('key rotation'); |
| 250 | + }); |
| 251 | + |
| 252 | + it('falls back to single chunk when no patterns match', () => { |
| 253 | + const content = [ |
| 254 | + 'Acme Corp Security Assessment', |
| 255 | + 'Prepared by: John Smith', |
| 256 | + 'Date: 2026-01-15', |
| 257 | + 'Version 3.0', |
| 258 | + ].join('\n'); |
| 259 | + |
| 260 | + const chunks = buildQuestionAwareChunks(content, CHUNK_OPTS); |
| 261 | + expect(chunks.length).toBe(1); |
| 262 | + expect(chunks[0].content).toContain('Acme Corp'); |
| 263 | + }); |
| 264 | +}); |
| 265 | + |
| 266 | +describe('estimateQuestionCount', () => { |
| 267 | + it('counts question marks when present', () => { |
| 268 | + expect( |
| 269 | + estimateQuestionCount('Q1? Q2? Q3?'), |
| 270 | + ).toBe(3); |
| 271 | + }); |
| 272 | + |
| 273 | + it('counts lines matching looksLikeQuestionLine when no question marks', () => { |
| 274 | + const text = [ |
| 275 | + 'The organization must have a BCP.', |
| 276 | + 'The organization ensures data is encrypted.', |
| 277 | + 'Section header', |
| 278 | + ].join('\n'); |
| 279 | + expect(estimateQuestionCount(text)).toBe(2); |
| 280 | + }); |
| 281 | + |
| 282 | + it('uses fallback heuristic for unrecognized content', () => { |
| 283 | + const text = 'a'.repeat(3600); |
| 284 | + expect(estimateQuestionCount(text)).toBe(3); |
| 285 | + }); |
| 286 | +}); |
0 commit comments