Skip to content

Commit

Permalink
Fix Json decoding (#317)
Browse files Browse the repository at this point in the history
* Fix Json deserialization

* Update

* Update test data
  • Loading branch information
HannaTarasevich authored Feb 6, 2024
1 parent bc2f755 commit 22e9663
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 23 deletions.
14 changes: 6 additions & 8 deletions lib/generate-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,22 +406,20 @@ function generateReport(options) {
scenario.steps.forEach((step) => {
if (step.embeddings !== undefined) {
step.attachments = [];

step.embeddings.forEach((embedding, embeddingIndex) => {
/* Decode Base64 for Text-ish attachements */
if(
embedding.mime_type === 'application/json' ||
embedding.mime_type === 'text/html' ||
embedding.mime_type === 'text/plain'
if(
embedding.mime_type === 'text/html' ||
embedding.mime_type === 'text/plain'
) {
embedding.data = Buffer.from(embedding.data.toString(), 'base64')
}

embedding.data = Buffer.from(embedding.data.toString(), 'base64')
}
/* istanbul ignore else */
if (
embedding.mime_type === 'application/json' ||
(embedding.media && embedding.media.type === 'application/json')
) {
embedding.data = Buffer.from(embedding.data, 'base64').toString()
step.json = (step.json ? step.json : []).concat([
typeof embedding.data === 'string'
? JSON.parse(embedding.data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@
},
"embeddings": [
{
"data": "{\"some\":\"json\"}",
"data": "eyJzb21lIjoianNvbiJ9",
"media": {
"type": "application/json"
}
},
{
"data": "{\"other\":\"json\"}",
"data": "eyJvdGhlciI6Impzb24ifQ==",
"media": {
"type": "application/json"
}
Expand All @@ -71,13 +71,13 @@
}
},
{
"data": "{\"yet another json\":5}",
"data": "eyJ5ZXQgYW5vdGhlciBqc29uIjo1fQ==",
"media": {
"type": "application/json"
}
},
{
"data": 0,
"data": "MA==",
"media": {
"type": "application/json"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,13 @@
},
"embeddings": [
{
"data": "{\"some\":\"json\"}",
"data": "eyJzb21lIjoianNvbiJ9",
"media": {
"type": "application/json"
}
},
{
"data": "{\"other\":\"json\"}",
"data": "eyJvdGhlciI6Impzb24ifQ==",
"media": {
"type": "application/json"
}
Expand All @@ -149,7 +149,7 @@
}
},
{
"data": "{\"yet another json\":5}",
"data": "eyJ5ZXQgYW5vdGhlciBqc29uIjo1fQ==",
"media": {
"type": "application/json"
}
Expand Down
4 changes: 1 addition & 3 deletions test/unit/data/json/before-and-after-hook.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@
"mime_type": "text/plain"
},
{
"data": {
"json-string": true
},
"data": "eyJqc29uLXN0cmluZyI6IHRydWV9",
"mime_type": "application/json"
}
]
Expand Down
2 changes: 1 addition & 1 deletion test/unit/data/json/embeddings.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
},
"embeddings": [
{
"data": "[{\"data\":{\"x\":\"1\",\"y\":\"1\",\"url\":\"http://url.com\"},\"event\":\"start\",\"show\":1,\"id\":50},{\"data\":{\"x\":\"2\",\"y\":\"3\",\"url\":\"http://example.com\"},\"event\":\"stop\",\"show\":2,\"id\":51}]",
"data": "W3siZGF0YSI6eyJ4IjoiMSIsInkiOiIxIiwidXJsIjoiaHR0cDovL3VybC5jb20ifSwiZXZlbnQiOiJzdGFydCIsInNob3ciOjEsImlkIjo1MH0seyJkYXRhIjp7IngiOiIyIiwieSI6IjMiLCJ1cmwiOiJodHRwOi8vZXhhbXBsZS5jb20ifSwiZXZlbnQiOiJzdG9wIiwic2hvdyI6MiwiaWQiOjUxfV0=",
"mime_type": "application/json"
},
{
Expand Down
6 changes: 2 additions & 4 deletions test/unit/data/output/merged-output.json
Original file line number Diff line number Diff line change
Expand Up @@ -1090,9 +1090,7 @@
"mime_type": "text/plain"
},
{
"data": {
"json-string": true
},
"data": "eyJqc29uLXN0cmluZyI6IHRydWV9",
"mime_type": "application/json"
}
]
Expand Down Expand Up @@ -1265,7 +1263,7 @@
},
"embeddings": [
{
"data": "[{\"data\":{\"x\":\"1\",\"y\":\"1\",\"url\":\"http://url.com\"},\"event\":\"start\",\"show\":1,\"id\":50},{\"data\":{\"x\":\"2\",\"y\":\"3\",\"url\":\"http://example.com\"},\"event\":\"stop\",\"show\":2,\"id\":51}]",
"data": "W3siZGF0YSI6eyJ4IjoiMSIsInkiOiIxIiwidXJsIjoiaHR0cDovL3VybC5jb20ifSwiZXZlbnQiOiJzdGFydCIsInNob3ciOjEsImlkIjo1MH0seyJkYXRhIjp7IngiOiIyIiwieSI6IjMiLCJ1cmwiOiJodHRwOi8vZXhhbXBsZS5jb20ifSwiZXZlbnQiOiJzdG9wIiwic2hvdyI6MiwiaWQiOjUxfV0=",
"mime_type": "application/json"
},
{
Expand Down

0 comments on commit 22e9663

Please sign in to comment.