Skip to content

Commit 5f79593

Browse files
committed
Additional test coverage
1 parent 75f3d26 commit 5f79593

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

experimental/packages/api-logs/test/common/utils/validation.test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,30 @@ describe('isLogAttributeValue', () => {
128128
it('should accept objects with byte arrays', () => {
129129
assert.strictEqual(isLogAttributeValue({ bytes: new Uint8Array([1, 2, 3]) }), true);
130130
});
131+
132+
it('should accept plain objects without prototypes', () => {
133+
const objWithoutProto = Object.create(null);
134+
objWithoutProto.key = 'value';
135+
objWithoutProto.number = 42;
136+
assert.strictEqual(isLogAttributeValue(objWithoutProto), true);
137+
});
138+
139+
it('should accept nested objects without prototypes', () => {
140+
const parent = Object.create(null);
141+
const child = Object.create(null);
142+
child.deep = 'value';
143+
parent.nested = child;
144+
parent.regular = { withProto: true };
145+
assert.strictEqual(isLogAttributeValue(parent), true);
146+
});
147+
148+
it('should accept objects without prototypes containing arrays', () => {
149+
const obj = Object.create(null);
150+
obj.strings = ['a', 'b', 'c'];
151+
obj.mixed = [1, 'two', true];
152+
obj.bytes = new Uint8Array([1, 2, 3]);
153+
assert.strictEqual(isLogAttributeValue(obj), true);
154+
});
131155
});
132156

133157
describe('should accept complex combinations', () => {
@@ -193,16 +217,40 @@ describe('isLogAttributeValue', () => {
193217
assert.strictEqual(isLogAttributeValue(new TestClass()), false);
194218
});
195219

220+
it('should reject Map objects', () => {
221+
assert.strictEqual(isLogAttributeValue(new Map()), false);
222+
assert.strictEqual(isLogAttributeValue(new Map([['key', 'value']])), false);
223+
224+
const nestedMap = new Map();
225+
nestedMap.set('nested', new Map([['inner', 'value']]));
226+
assert.strictEqual(isLogAttributeValue(nestedMap), false);
227+
});
228+
229+
it('should reject Set objects', () => {
230+
assert.strictEqual(isLogAttributeValue(new Set()), false);
231+
assert.strictEqual(isLogAttributeValue(new Set([1, 2, 3])), false);
232+
assert.strictEqual(isLogAttributeValue(new Set(['a', 'b', 'c'])), false);
233+
});
234+
235+
it('should reject WeakMap and WeakSet objects', () => {
236+
assert.strictEqual(isLogAttributeValue(new WeakMap()), false);
237+
assert.strictEqual(isLogAttributeValue(new WeakSet()), false);
238+
});
239+
196240
it('should reject arrays containing invalid values', () => {
197241
assert.strictEqual(isLogAttributeValue(['valid', () => {}]), false);
198242
assert.strictEqual(isLogAttributeValue([Symbol('test'), 'valid']), false);
199243
assert.strictEqual(isLogAttributeValue([new Date()]), false);
244+
assert.strictEqual(isLogAttributeValue([new Map()]), false);
245+
assert.strictEqual(isLogAttributeValue(['valid', new Set([1, 2, 3])]), false);
200246
});
201247

202248
it('should reject objects containing invalid values', () => {
203249
assert.strictEqual(isLogAttributeValue({ valid: 'test', invalid: () => {} }), false);
204250
assert.strictEqual(isLogAttributeValue({ symbol: Symbol('test') }), false);
205251
assert.strictEqual(isLogAttributeValue({ date: new Date() }), false);
252+
assert.strictEqual(isLogAttributeValue({ map: new Map([['key', 'value']]) }), false);
253+
assert.strictEqual(isLogAttributeValue({ set: new Set([1, 2, 3]) }), false);
206254
});
207255

208256
it('should reject deeply nested invalid values', () => {

experimental/packages/sdk-logs/test/common/LogRecord.test.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,66 @@ describe('LogRecord', () => {
617617
});
618618
});
619619

620+
describe('should reject empty attribute keys', () => {
621+
it('should not set attributes with empty string keys', () => {
622+
const warnStub = sinon.spy(diag, 'warn');
623+
const { logRecord } = setup();
624+
625+
// Try to set an attribute with an empty key
626+
logRecord.setAttribute('', 'value');
627+
628+
// The attribute should not be set
629+
assert.strictEqual(logRecord.attributes[''], undefined);
630+
assert.deepStrictEqual(logRecord.attributes, {});
631+
632+
// A warning should be logged
633+
sinon.assert.calledWith(warnStub, 'Invalid attribute key: ');
634+
warnStub.restore();
635+
});
636+
637+
it('should reject empty keys but accept other valid attributes', () => {
638+
const warnStub = sinon.spy(diag, 'warn');
639+
const { logRecord } = setup();
640+
641+
// Set some valid attributes
642+
logRecord.setAttribute('valid1', 'value1');
643+
logRecord.setAttribute('', 'should not be set');
644+
logRecord.setAttribute('valid2', 'value2');
645+
646+
// Only valid attributes should be set
647+
assert.deepStrictEqual(logRecord.attributes, {
648+
valid1: 'value1',
649+
valid2: 'value2'
650+
});
651+
652+
// Warning should be logged for empty key
653+
sinon.assert.calledWith(warnStub, 'Invalid attribute key: ');
654+
warnStub.restore();
655+
});
656+
657+
it('should reject empty keys in setAttributes', () => {
658+
const warnStub = sinon.spy(diag, 'warn');
659+
const { logRecord } = setup();
660+
661+
// Try to set multiple attributes including empty keys
662+
logRecord.setAttributes({
663+
valid: 'value',
664+
'': 'empty key',
665+
anotherValid: 'another value'
666+
});
667+
668+
// Only valid attributes should be set
669+
assert.deepStrictEqual(logRecord.attributes, {
670+
valid: 'value',
671+
anotherValid: 'another value'
672+
});
673+
674+
// Warning should be logged for empty key
675+
sinon.assert.calledWith(warnStub, 'Invalid attribute key: ');
676+
warnStub.restore();
677+
});
678+
});
679+
620680
describe('log record processor', () => {
621681
it('should call onEmit synchronously when log record is emitted', () => {
622682
let emitted = false;

0 commit comments

Comments
 (0)