From ccb4ab6d7c45ed3f95b641e45c7085471171b9a2 Mon Sep 17 00:00:00 2001 From: Cody Olsen Date: Thu, 16 Oct 2025 20:53:13 +0200 Subject: [PATCH] chore: add more stega tests --- src/csm/createEditUrl.ts | 1 + .../stegaEncodeSourceMap.test.ts.snap | 156 ++++++++++++ test/stega/stegaEncodeSourceMap.test.ts | 28 +++ test/stega/stegaSnapshotContentReleases.json | 230 ++++++++++++++++++ 4 files changed, 415 insertions(+) create mode 100644 test/stega/stegaSnapshotContentReleases.json diff --git a/src/csm/createEditUrl.ts b/src/csm/createEditUrl.ts index 98d697b70..533abbe2b 100644 --- a/src/csm/createEditUrl.ts +++ b/src/csm/createEditUrl.ts @@ -56,6 +56,7 @@ export function createEditUrl(options: CreateEditUrlOptions): `${StudioBaseUrl}$ if (dataset) { searchParams.set('dataset', dataset) } + // @TODO what is going on here? if (isPublishedId(_id)) { searchParams.set('perspective', 'published') } else if (isVersionId(_id)) { diff --git a/test/stega/__snapshots__/stegaEncodeSourceMap.test.ts.snap b/test/stega/__snapshots__/stegaEncodeSourceMap.test.ts.snap index a79df17f7..f572ab85c 100644 --- a/test/stega/__snapshots__/stegaEncodeSourceMap.test.ts.snap +++ b/test/stega/__snapshots__/stegaEncodeSourceMap.test.ts.snap @@ -42,6 +42,162 @@ exports[`GraphQL API > logger.table 1`] = ` ] `; +exports[`Handles Content Releases > decode all 1`] = ` +[ + [ + "https://test.sanity.studio/intent/edit/mode=presentation;id=18c9ae1a-5c14-4ffe-b32e-8e488f043718;type=author;path=name", + Map { + "baseUrl" => "https://test.sanity.studio", + "id" => "18c9ae1a-5c14-4ffe-b32e-8e488f043718", + "type" => "author", + "path" => "name", + "perspective" => "published", + }, + ], + [ + "https://test.sanity.studio/intent/edit/mode=presentation;id=18c9ae1a-5c14-4ffe-b32e-8e488f043718;type=author;path=picture.alt", + Map { + "baseUrl" => "https://test.sanity.studio", + "id" => "18c9ae1a-5c14-4ffe-b32e-8e488f043718", + "type" => "author", + "path" => "picture.alt", + "perspective" => "published", + }, + ], + [ + "https://test.sanity.studio/intent/edit/mode=presentation;id=e1674ab7-5f96-49b0-8291-7608e087ef0a;type=author;path=name", + Map { + "baseUrl" => "https://test.sanity.studio", + "id" => "e1674ab7-5f96-49b0-8291-7608e087ef0a", + "type" => "author", + "path" => "name", + "perspective" => "rrRHoDGSr", + }, + ], + [ + "https://test.sanity.studio/intent/edit/mode=presentation;id=e1674ab7-5f96-49b0-8291-7608e087ef0a;type=author;path=picture.alt", + Map { + "baseUrl" => "https://test.sanity.studio", + "id" => "e1674ab7-5f96-49b0-8291-7608e087ef0a", + "type" => "author", + "path" => "picture.alt", + "perspective" => "rrRHoDGSr", + }, + ], + [ + "https://test.sanity.studio/intent/edit/mode=presentation;id=f99b873f-85c2-41c4-ac65-807b1d72c084;type=author;path=name", + Map { + "baseUrl" => "https://test.sanity.studio", + "id" => "f99b873f-85c2-41c4-ac65-807b1d72c084", + "type" => "author", + "path" => "name", + }, + ], + [ + "https://test.sanity.studio/intent/edit/mode=presentation;id=f99b873f-85c2-41c4-ac65-807b1d72c084;type=author;path=picture.alt", + Map { + "baseUrl" => "https://test.sanity.studio", + "id" => "f99b873f-85c2-41c4-ac65-807b1d72c084", + "type" => "author", + "path" => "picture.alt", + }, + ], + [ + "https://test.sanity.studio/intent/edit/mode=presentation;id=f99b873f-85c2-41c4-ac65-807b1d72c084;type=author;path=name", + Map { + "baseUrl" => "https://test.sanity.studio", + "id" => "f99b873f-85c2-41c4-ac65-807b1d72c084", + "type" => "author", + "path" => "name", + }, + ], + [ + "https://test.sanity.studio/intent/edit/mode=presentation;id=f99b873f-85c2-41c4-ac65-807b1d72c084;type=author;path=picture.alt", + Map { + "baseUrl" => "https://test.sanity.studio", + "id" => "f99b873f-85c2-41c4-ac65-807b1d72c084", + "type" => "author", + "path" => "picture.alt", + }, + ], +] +`; + +exports[`Handles Content Releases > logger.error 1`] = `[]`; + +exports[`Handles Content Releases > logger.log 1`] = ` +[ + [ + "[@sanity/client]: Encoding source map into result", + ], + [ + "[@sanity/client]: Paths encoded: 8, skipped: 20", + ], + [ + "[@sanity/client]: Table of encoded paths", + ], + [ + "[@sanity/client]: List of skipped paths", + [ + "_id", + "_originalId", + "picture._type", + "picture.asset._ref", + "picture.asset._type", + ], + ], +] +`; + +exports[`Handles Content Releases > logger.table 1`] = ` +[ + [ + [ + { + "length": 11, + "path": "name", + "value": "Test Author", + }, + { + "length": 65, + "path": "picture.alt", + "value": "a computer generated...", + }, + { + "length": 26, + "path": "name", + "value": "Another One!! BLACK ...", + }, + { + "length": 37, + "path": "picture.alt", + "value": "the letter a is lit ...", + }, + { + "length": 8, + "path": "name", + "value": "John Doe", + }, + { + "length": 37, + "path": "picture.alt", + "value": "the letter a is lit ...", + }, + { + "length": 8, + "path": "name", + "value": "John Doe", + }, + { + "length": 37, + "path": "picture.alt", + "value": "the letter a is lit ...", + }, + ], + ], +] +`; + exports[`resolveEditUrl '/' > decode all 1`] = ` [ "/intent/edit/mode=presentation;id=462efcc6-3c8b-47c6-8474-5544e1a4acde;type=product;path=description[_key=="d8fa6d59e49b"].children[_key=="6a057cf2fb310"].text", diff --git a/test/stega/stegaEncodeSourceMap.test.ts b/test/stega/stegaEncodeSourceMap.test.ts index 2c1073b72..59cca7d4f 100644 --- a/test/stega/stegaEncodeSourceMap.test.ts +++ b/test/stega/stegaEncodeSourceMap.test.ts @@ -3,6 +3,7 @@ import {expect, test, vi} from 'vitest' import {stegaEncodeSourceMap} from '../../src/stega/stegaEncodeSourceMap' import type {ContentSourceMap, Logger} from '../../src/stega/types' +import stegaSnapshotContentReleases from './stegaSnapshotContentReleases.json' with {type: 'json'} const mock = { query: @@ -622,3 +623,30 @@ test('GraphQL API', () => { expect(logger.log.mock.calls).toMatchSnapshot('logger.log') expect(logger.table.mock.calls).toMatchSnapshot('logger.table') }) + +test('Handles Content Releases', () => { + const logger = { + error: vi.fn(), + log: vi.fn(), + table: vi.fn(), + } satisfies Logger + const studioUrl = 'https://test.sanity.studio' + const encoded = stegaEncodeSourceMap( + stegaSnapshotContentReleases.result, + stegaSnapshotContentReleases.resultSourceMap as ContentSourceMap, + { + enabled: true, + studioUrl, + logger, + }, + ) + expect( + vercelStegaDecodeAll(JSON.stringify(encoded)).map(({href}: any) => { + const [url, search] = decodeURIComponent(href).split('?') + return [url, new Map(new URLSearchParams(search).entries())] + }), + ).toMatchSnapshot('decode all') + expect(logger.error.mock.calls).toMatchSnapshot('logger.error') + expect(logger.log.mock.calls).toMatchSnapshot('logger.log') + expect(logger.table.mock.calls).toMatchSnapshot('logger.table') +}) diff --git a/test/stega/stegaSnapshotContentReleases.json b/test/stega/stegaSnapshotContentReleases.json new file mode 100644 index 000000000..69812c596 --- /dev/null +++ b/test/stega/stegaSnapshotContentReleases.json @@ -0,0 +1,230 @@ +{ + "query": "*[_type == \"post\" && defined(slug.current)] | order(date desc, _updatedAt desc) {\n \"author\": author-\u003E{_id, _originalId, \"name\": coalesce(name, \"Anonymous\"), picture},\n \n }", + "result": [ + { + "author": { + "_id": "18c9ae1a-5c14-4ffe-b32e-8e488f043718", + "_originalId": "18c9ae1a-5c14-4ffe-b32e-8e488f043718", + "name": "Test Author", + "picture": { + "_type": "image", + "alt": "a computer generated image of a green swirl on a black background", + "asset": { + "_ref": "image-bcc7684cc81521607725dcc720fb260377c4a813-4000x6000-jpg", + "_type": "reference" + } + } + } + }, + { + "author": { + "_id": "e1674ab7-5f96-49b0-8291-7608e087ef0a", + "_originalId": "versions.rrRHoDGSr.e1674ab7-5f96-49b0-8291-7608e087ef0a", + "name": "Another One!! BLACK FRIDAY", + "picture": { + "_type": "image", + "alt": "the letter a is lit up on a blue wall", + "asset": { + "_ref": "image-c4c54a778ff4d2587a598403f59ee148dc869e14-5436x3514-jpg", + "_type": "reference" + } + } + } + }, + { + "author": { + "_id": "f99b873f-85c2-41c4-ac65-807b1d72c084", + "_originalId": "drafts.f99b873f-85c2-41c4-ac65-807b1d72c084", + "name": "John Doe", + "picture": { + "_type": "image", + "alt": "the letter a is lit up on a blue wall", + "asset": { + "_ref": "image-c4c54a778ff4d2587a598403f59ee148dc869e14-5436x3514-jpg", + "_type": "reference" + } + } + } + }, + { + "author": { + "_id": "f99b873f-85c2-41c4-ac65-807b1d72c084", + "_originalId": "drafts.f99b873f-85c2-41c4-ac65-807b1d72c084", + "name": "John Doe", + "picture": { + "_type": "image", + "alt": "the letter a is lit up on a blue wall", + "asset": { + "_ref": "image-c4c54a778ff4d2587a598403f59ee148dc869e14-5436x3514-jpg", + "_type": "reference" + } + } + } + } + ], + "resultSourceMap": { + "documents": [ + { + "_id": "18c9ae1a-5c14-4ffe-b32e-8e488f043718", + "_type": "author" + }, + { + "_id": "drafts.f99b873f-85c2-41c4-ac65-807b1d72c084", + "_type": "author" + }, + { + "_id": "versions.rrRHoDGSr.e1674ab7-5f96-49b0-8291-7608e087ef0a", + "_type": "author" + } + ], + "paths": [ + "$['_id']", + "$['_originalId']", + "$['name']", + "$['picture']" + ], + "mappings": { + "$[0]['author']['_id']": { + "source": { + "document": 0, + "path": 0, + "type": "documentValue" + }, + "type": "value" + }, + "$[0]['author']['_originalId']": { + "source": { + "document": 0, + "path": 1, + "type": "documentValue" + }, + "type": "value" + }, + "$[0]['author']['name']": { + "source": { + "document": 0, + "path": 2, + "type": "documentValue" + }, + "type": "value" + }, + "$[0]['author']['picture']": { + "source": { + "document": 0, + "path": 3, + "type": "documentValue" + }, + "type": "value" + }, + "$[1]['author']['_id']": { + "source": { + "document": 2, + "path": 0, + "type": "documentValue" + }, + "type": "value" + }, + "$[1]['author']['_originalId']": { + "source": { + "document": 2, + "path": 1, + "type": "documentValue" + }, + "type": "value" + }, + "$[1]['author']['name']": { + "source": { + "document": 2, + "path": 2, + "type": "documentValue" + }, + "type": "value" + }, + "$[1]['author']['picture']": { + "source": { + "document": 2, + "path": 3, + "type": "documentValue" + }, + "type": "value" + }, + "$[2]['author']['_id']": { + "source": { + "document": 1, + "path": 0, + "type": "documentValue" + }, + "type": "value" + }, + "$[2]['author']['_originalId']": { + "source": { + "document": 1, + "path": 1, + "type": "documentValue" + }, + "type": "value" + }, + "$[2]['author']['name']": { + "source": { + "document": 1, + "path": 2, + "type": "documentValue" + }, + "type": "value" + }, + "$[2]['author']['picture']": { + "source": { + "document": 1, + "path": 3, + "type": "documentValue" + }, + "type": "value" + }, + "$[3]['author']['_id']": { + "source": { + "document": 1, + "path": 0, + "type": "documentValue" + }, + "type": "value" + }, + "$[3]['author']['_originalId']": { + "source": { + "document": 1, + "path": 1, + "type": "documentValue" + }, + "type": "value" + }, + "$[3]['author']['name']": { + "source": { + "document": 1, + "path": 2, + "type": "documentValue" + }, + "type": "value" + }, + "$[3]['author']['picture']": { + "source": { + "document": 1, + "path": 3, + "type": "documentValue" + }, + "type": "value" + } + } + }, + "syncTags": [ + "s1:0SlxPg", + "s1:ZJkujg", + "s1:QDYUMw", + "s1:yDDKLw", + "s1:7/9iuA", + "s1:ilW8Kw", + "s1:84gAkQ", + "s1:ZUu7JA", + "s1:w/zRqQ", + "s1:ujwhFw" + ], + "ms": 8 +} \ No newline at end of file