|
7 | 7 | * Preserves: false, 0, "0" |
8 | 8 | */ |
9 | 9 |
|
10 | | -import { stripEmpty } from '../utils/compact.js'; |
| 10 | +import { stripEmpty, stripEntryMeta, stripEntryMetaFromResponse } from '../utils/compact.js'; |
11 | 11 | import { TestRunner, TestAssert } from './helpers.js'; |
12 | 12 |
|
13 | 13 | const runner = new TestRunner('Compact Utility Tests'); |
@@ -220,6 +220,123 @@ runner.test('stripEmpty: does not mutate original object', () => { |
220 | 220 | TestAssert.equal(Object.keys(input).length, 3, 'original key count unchanged'); |
221 | 221 | }); |
222 | 222 |
|
| 223 | +// --- stripEntryMeta --- |
| 224 | + |
| 225 | +runner.test('stripEntryMeta: keeps core properties and field values', () => { |
| 226 | + const entry = { |
| 227 | + id: '123', |
| 228 | + form_id: '29', |
| 229 | + status: 'active', |
| 230 | + date_created: '2024-01-15', |
| 231 | + date_updated: '2024-01-15', |
| 232 | + is_starred: '0', |
| 233 | + is_read: '0', |
| 234 | + ip: '127.0.0.1', |
| 235 | + source_url: 'https://example.com', |
| 236 | + user_agent: 'node', |
| 237 | + currency: 'USD', |
| 238 | + created_by: '1', |
| 239 | + '2': 'test@example.com', |
| 240 | + '3': 'some survey value', |
| 241 | + '6': '21621', |
| 242 | + gv_revision_parent_id: false, |
| 243 | + gv_revision_date: false, |
| 244 | + gv_revision_date_gmt: false, |
| 245 | + gv_revision_user_id: false, |
| 246 | + gv_revision_changed: false, |
| 247 | + helpscout_conversation_id: false, |
| 248 | + conversion_bridge_session: false |
| 249 | + }; |
| 250 | + const result = stripEntryMeta(entry); |
| 251 | + |
| 252 | + // Core properties kept |
| 253 | + TestAssert.equal(result.id, '123', 'id kept'); |
| 254 | + TestAssert.equal(result.form_id, '29', 'form_id kept'); |
| 255 | + TestAssert.equal(result.status, 'active', 'status kept'); |
| 256 | + TestAssert.equal(result.created_by, '1', 'created_by kept'); |
| 257 | + TestAssert.equal(result.currency, 'USD', 'currency kept'); |
| 258 | + |
| 259 | + // Field values kept |
| 260 | + TestAssert.equal(result['2'], 'test@example.com', 'field 2 kept'); |
| 261 | + TestAssert.equal(result['3'], 'some survey value', 'field 3 kept'); |
| 262 | + TestAssert.equal(result['6'], '21621', 'field 6 kept'); |
| 263 | + |
| 264 | + // Plugin meta stripped |
| 265 | + TestAssert.equal(result.gv_revision_parent_id, undefined, 'gv_revision_parent_id stripped'); |
| 266 | + TestAssert.equal(result.gv_revision_date, undefined, 'gv_revision_date stripped'); |
| 267 | + TestAssert.equal(result.helpscout_conversation_id, undefined, 'helpscout_conversation_id stripped'); |
| 268 | + TestAssert.equal(result.conversion_bridge_session, undefined, 'conversion_bridge_session stripped'); |
| 269 | +}); |
| 270 | + |
| 271 | +runner.test('stripEntryMeta: keeps compound field keys (dot notation)', () => { |
| 272 | + const entry = { |
| 273 | + id: '456', |
| 274 | + form_id: '5', |
| 275 | + status: 'active', |
| 276 | + '5.1': '123 Main St', |
| 277 | + '5.2': 'Apt 4', |
| 278 | + '5.3': 'Springfield', |
| 279 | + '5.4': 'IL', |
| 280 | + '5.5': '62701', |
| 281 | + some_plugin_meta: 'value' |
| 282 | + }; |
| 283 | + const result = stripEntryMeta(entry); |
| 284 | + TestAssert.equal(result['5.1'], '123 Main St', 'compound field 5.1 kept'); |
| 285 | + TestAssert.equal(result['5.3'], 'Springfield', 'compound field 5.3 kept'); |
| 286 | + TestAssert.equal(result.some_plugin_meta, undefined, 'plugin meta stripped'); |
| 287 | +}); |
| 288 | + |
| 289 | +runner.test('stripEntryMeta: keeps payment fields when present', () => { |
| 290 | + const entry = { |
| 291 | + id: '789', |
| 292 | + form_id: '10', |
| 293 | + status: 'active', |
| 294 | + payment_status: 'Paid', |
| 295 | + payment_amount: '49.99', |
| 296 | + payment_method: 'stripe', |
| 297 | + transaction_id: 'txn_123', |
| 298 | + custom_addon_field: 'noise' |
| 299 | + }; |
| 300 | + const result = stripEntryMeta(entry); |
| 301 | + TestAssert.equal(result.payment_status, 'Paid', 'payment_status kept'); |
| 302 | + TestAssert.equal(result.payment_amount, '49.99', 'payment_amount kept'); |
| 303 | + TestAssert.equal(result.transaction_id, 'txn_123', 'transaction_id kept'); |
| 304 | + TestAssert.equal(result.custom_addon_field, undefined, 'custom addon stripped'); |
| 305 | +}); |
| 306 | + |
| 307 | +// --- stripEntryMetaFromResponse --- |
| 308 | + |
| 309 | +runner.test('stripEntryMetaFromResponse: handles entries array', () => { |
| 310 | + const response = { |
| 311 | + entries: [ |
| 312 | + { id: '1', form_id: '5', status: 'active', '2': 'val', plugin_meta: 'x' }, |
| 313 | + { id: '2', form_id: '5', status: 'active', '3': 'val', plugin_meta: 'y' } |
| 314 | + ], |
| 315 | + total_count: 2 |
| 316 | + }; |
| 317 | + const result = stripEntryMetaFromResponse(response); |
| 318 | + TestAssert.equal(result.entries.length, 2, 'array length preserved'); |
| 319 | + TestAssert.equal(result.total_count, 2, 'total_count preserved'); |
| 320 | + TestAssert.equal(result.entries[0].plugin_meta, undefined, 'meta stripped from first'); |
| 321 | + TestAssert.equal(result.entries[1].plugin_meta, undefined, 'meta stripped from second'); |
| 322 | + TestAssert.equal(result.entries[0]['2'], 'val', 'field value kept in first'); |
| 323 | +}); |
| 324 | + |
| 325 | +runner.test('stripEntryMetaFromResponse: handles single entry', () => { |
| 326 | + const response = { |
| 327 | + entry: { id: '1', form_id: '5', status: 'active', '2': 'val', plugin_meta: 'x' } |
| 328 | + }; |
| 329 | + const result = stripEntryMetaFromResponse(response); |
| 330 | + TestAssert.equal(result.entry.plugin_meta, undefined, 'meta stripped'); |
| 331 | + TestAssert.equal(result.entry['2'], 'val', 'field value kept'); |
| 332 | +}); |
| 333 | + |
| 334 | +runner.test('stripEntryMetaFromResponse: passes through non-entry responses', () => { |
| 335 | + const response = { forms: [{ id: 1 }] }; |
| 336 | + const result = stripEntryMetaFromResponse(response); |
| 337 | + TestAssert.equal(result.forms[0].id, 1, 'non-entry response unchanged'); |
| 338 | +}); |
| 339 | + |
223 | 340 | // Run tests |
224 | 341 | runner.run().then(results => { |
225 | 342 | process.exit(results.failed > 0 ? 1 : 0); |
|
0 commit comments