Skip to content

Commit de1d05a

Browse files
authored
node: fix invalid breadcrumb name in FileBreadcrumbsStorage (#303)
* node: fix invalid breadcrumb name in FileBreadcrumbsStorage * node: use getAttachments in getAttachmentProviders in FileBreadcrumbsStorage * node: add unit tests for attachment names from FileBreadcrumbsStorage --------- Co-authored-by: Sebastian Alex <sebastian.alex@saucelabs.com>
1 parent 69d7c30 commit de1d05a

File tree

2 files changed

+96
-5
lines changed

2 files changed

+96
-5
lines changed

packages/node/src/breadcrumbs/FileBreadcrumbsStorage.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,13 @@ export class FileBreadcrumbsStorage implements BreadcrumbsStorage {
8686

8787
public getAttachments(): BacktraceAttachment<Readable>[] {
8888
const files = [...this._sink.files].map((f) => f.path.toString('utf-8'));
89-
return files.map((f) => new BacktraceFileAttachment(f, f, this._fileSystem));
89+
return files.map((f) => new BacktraceFileAttachment(f, path.basename(f), this._fileSystem));
9090
}
9191

9292
public getAttachmentProviders(): BacktraceAttachmentProvider[] {
9393
return [
9494
{
95-
get: () => {
96-
const files = [...this._sink.files].map((f) => f.path.toString('utf-8'));
97-
return files.map((f) => new BacktraceFileAttachment(f, f, this._fileSystem));
98-
},
95+
get: () => this.getAttachments(),
9996
type: 'dynamic',
10097
},
10198
];

packages/node/tests/breadcrumbs/FileBreadcrumbsStorage.spec.ts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,4 +327,98 @@ describe('FileBreadcrumbsStorage', () => {
327327
expect(actualMain).toEqual(expectedMain);
328328
expect(actualFallback).toEqual(expectedFallback);
329329
});
330+
331+
it('should return attachments with a valid name from getAttachments', async () => {
332+
const fs = mockStreamFileSystem();
333+
const session = new SessionFiles(fs, '.', 'sessionId');
334+
335+
const breadcrumbs: RawBreadcrumb[] = [
336+
{
337+
level: BreadcrumbLogLevel.Info,
338+
message: 'a',
339+
type: BreadcrumbType.Manual,
340+
attributes: {
341+
foo: 'bar',
342+
},
343+
},
344+
{
345+
level: BreadcrumbLogLevel.Debug,
346+
message: 'b',
347+
type: BreadcrumbType.Http,
348+
},
349+
{
350+
level: BreadcrumbLogLevel.Warning,
351+
message: 'c',
352+
type: BreadcrumbType.Navigation,
353+
attributes: {},
354+
},
355+
];
356+
357+
const storage = new FileBreadcrumbsStorage(session, fs, {
358+
maximumBreadcrumbs: 4,
359+
});
360+
361+
for (const breadcrumb of breadcrumbs) {
362+
storage.add(breadcrumb);
363+
await nextTick();
364+
}
365+
366+
// FileBreadcrumbsStorage is asynchronous in nature
367+
await nextTick();
368+
369+
const [fallbackAttachment, mainAttachment] = storage.getAttachments();
370+
371+
expect(fallbackAttachment.name).toEqual(expect.stringMatching(/^bt-breadcrumbs-0/));
372+
expect(mainAttachment.name).toEqual(expect.stringMatching(/^bt-breadcrumbs-1/));
373+
});
374+
375+
it('should return attachments with a valid name from getAttachmentProviders', async () => {
376+
const fs = mockStreamFileSystem();
377+
const session = new SessionFiles(fs, '.', 'sessionId');
378+
379+
const breadcrumbs: RawBreadcrumb[] = [
380+
{
381+
level: BreadcrumbLogLevel.Info,
382+
message: 'a',
383+
type: BreadcrumbType.Manual,
384+
attributes: {
385+
foo: 'bar',
386+
},
387+
},
388+
{
389+
level: BreadcrumbLogLevel.Debug,
390+
message: 'b',
391+
type: BreadcrumbType.Http,
392+
},
393+
{
394+
level: BreadcrumbLogLevel.Warning,
395+
message: 'c',
396+
type: BreadcrumbType.Navigation,
397+
attributes: {},
398+
},
399+
];
400+
401+
const storage = new FileBreadcrumbsStorage(session, fs, {
402+
maximumBreadcrumbs: 4,
403+
});
404+
405+
for (const breadcrumb of breadcrumbs) {
406+
storage.add(breadcrumb);
407+
await nextTick();
408+
}
409+
410+
// FileBreadcrumbsStorage is asynchronous in nature
411+
await nextTick();
412+
413+
const providers = storage.getAttachmentProviders();
414+
415+
const [fallbackAttachment, mainAttachment] = providers
416+
.map((v) => v.get())
417+
.map((v) => (Array.isArray(v) ? v : [v]))
418+
.filter((f) => !!f)
419+
.reduce((acc, arr) => [...acc, ...arr], []);
420+
421+
expect(fallbackAttachment?.name).toEqual(expect.stringMatching(/^bt-breadcrumbs-0/));
422+
expect(mainAttachment?.name).toEqual(expect.stringMatching(/^bt-breadcrumbs-1/));
423+
});
330424
});

0 commit comments

Comments
 (0)