Skip to content

Commit eec48ac

Browse files
committed
add tests for sds findings in sdk response and abort error
1 parent 5448f3c commit eec48ac

1 file changed

Lines changed: 27 additions & 2 deletions

File tree

dd-java-agent/agent-aiguard/src/test/groovy/com/datadog/aiguard/AIGuardInternalTests.groovy

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,13 @@ class AIGuardInternalTests extends DDSpecification {
210210
error.action == suite.action
211211
error.reason == suite.reason
212212
error.tags == suite.tags
213+
error.sds == []
213214
} else {
214215
error == null
215216
eval.action == suite.action
216217
eval.reason == suite.reason
217218
eval.tags == suite.tags
219+
eval.sds == []
218220
}
219221
assertTelemetry('ai_guard.requests', "action:$suite.action", "block:$throwAbortError", 'error:false')
220222

@@ -366,14 +368,15 @@ class AIGuardInternalTests extends DDSpecification {
366368
Map<String, Object> receivedMeta
367369

368370
when:
369-
aiguard.evaluate(PROMPT, AIGuard.Options.DEFAULT)
371+
final result = aiguard.evaluate(PROMPT, AIGuard.Options.DEFAULT)
370372

371373
then:
372374
1 * span.setMetaStruct(AIGuardInternal.META_STRUCT_TAG, _) >> {
373375
receivedMeta = it[1] as Map<String, Object>
374376
return span
375377
}
376378
receivedMeta.sds == sdsFindings
379+
result.sds == sdsFindings
377380
}
378381

379382
void 'test evaluate with empty sds findings'() {
@@ -382,19 +385,41 @@ class AIGuardInternalTests extends DDSpecification {
382385
Map<String, Object> receivedMeta
383386

384387
when:
385-
aiguard.evaluate(PROMPT, AIGuard.Options.DEFAULT)
388+
final result = aiguard.evaluate(PROMPT, AIGuard.Options.DEFAULT)
386389

387390
then:
388391
1 * span.setMetaStruct(AIGuardInternal.META_STRUCT_TAG, _) >> {
389392
receivedMeta = it[1] as Map<String, Object>
390393
return span
391394
}
392395
!receivedMeta.containsKey('sds')
396+
result.sds == (sdsFindings ?: [])
393397

394398
where:
395399
sdsFindings << [null, []]
396400
}
397401

402+
void 'test evaluate with sds findings in abort error'() {
403+
given:
404+
final sdsFindings = [
405+
[
406+
rule_display_name: 'Credit Card Number',
407+
rule_tag: 'credit_card',
408+
category: 'pii',
409+
matched_text: '4111111111111111',
410+
location: [start_index: 10, end_index_exclusive: 26, path: 'messages[0].content[0].text']
411+
]
412+
]
413+
final aiguard = mockClient(200, [data: [attributes: [action: 'ABORT', reason: 'PII detected', tags: ['pii'], sds_findings: sdsFindings, is_blocking_enabled: true]]])
414+
415+
when:
416+
aiguard.evaluate(PROMPT, new AIGuard.Options().block(true))
417+
418+
then:
419+
final error = thrown(AIGuard.AIGuardAbortError)
420+
error.sds == sdsFindings
421+
}
422+
398423
void 'test missing tool name'() {
399424
given:
400425
final aiguard = mockClient(200, [data: [attributes: [action: 'ALLOW', reason: 'Just do it']]])

0 commit comments

Comments
 (0)