Skip to content

Commit

Permalink
fix: issue with a previous change discovered by regression testing
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarenaldi committed Dec 12, 2023
1 parent 45b0b8b commit 0c8ca52
Show file tree
Hide file tree
Showing 17 changed files with 51 additions and 35 deletions.
1 change: 0 additions & 1 deletion jsonschemas/publications/3d/3.0.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
},
"name": {
"type": "string",
"minLength": 1,
"description": "Name of the NFT item."
},
"attributes": {
Expand Down
1 change: 0 additions & 1 deletion jsonschemas/publications/article/3.0.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
},
"name": {
"type": "string",
"minLength": 1,
"description": "Name of the NFT item."
},
"attributes": {
Expand Down
1 change: 0 additions & 1 deletion jsonschemas/publications/audio/3.0.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
},
"name": {
"type": "string",
"minLength": 1,
"description": "Name of the NFT item."
},
"attributes": {
Expand Down
1 change: 0 additions & 1 deletion jsonschemas/publications/checking-in/3.0.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
},
"name": {
"type": "string",
"minLength": 1,
"description": "Name of the NFT item."
},
"attributes": {
Expand Down
1 change: 0 additions & 1 deletion jsonschemas/publications/embed/3.0.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
},
"name": {
"type": "string",
"minLength": 1,
"description": "Name of the NFT item."
},
"attributes": {
Expand Down
1 change: 0 additions & 1 deletion jsonschemas/publications/event/3.0.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
},
"name": {
"type": "string",
"minLength": 1,
"description": "Name of the NFT item."
},
"attributes": {
Expand Down
1 change: 0 additions & 1 deletion jsonschemas/publications/image/3.0.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
},
"name": {
"type": "string",
"minLength": 1,
"description": "Name of the NFT item."
},
"attributes": {
Expand Down
1 change: 0 additions & 1 deletion jsonschemas/publications/link/3.0.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
},
"name": {
"type": "string",
"minLength": 1,
"description": "Name of the NFT item."
},
"attributes": {
Expand Down
1 change: 0 additions & 1 deletion jsonschemas/publications/livestream/3.0.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
},
"name": {
"type": "string",
"minLength": 1,
"description": "Name of the NFT item."
},
"attributes": {
Expand Down
1 change: 0 additions & 1 deletion jsonschemas/publications/mint/3.0.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
},
"name": {
"type": "string",
"minLength": 1,
"description": "Name of the NFT item."
},
"attributes": {
Expand Down
1 change: 0 additions & 1 deletion jsonschemas/publications/space/3.0.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
},
"name": {
"type": "string",
"minLength": 1,
"description": "Name of the NFT item."
},
"attributes": {
Expand Down
1 change: 0 additions & 1 deletion jsonschemas/publications/story/3.0.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
},
"name": {
"type": "string",
"minLength": 1,
"description": "Name of the NFT item."
},
"attributes": {
Expand Down
1 change: 0 additions & 1 deletion jsonschemas/publications/text-only/3.0.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
},
"name": {
"type": "string",
"minLength": 1,
"description": "Name of the NFT item."
},
"attributes": {
Expand Down
1 change: 0 additions & 1 deletion jsonschemas/publications/transaction/3.0.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
},
"name": {
"type": "string",
"minLength": 1,
"description": "Name of the NFT item."
},
"attributes": {
Expand Down
1 change: 0 additions & 1 deletion jsonschemas/publications/video/3.0.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
},
"name": {
"type": "string",
"minLength": 1,
"description": "Name of the NFT item."
},
"attributes": {
Expand Down
69 changes: 50 additions & 19 deletions src/publication/__tests__/regression.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import {
} from '@lens-protocol/client';

import { formatZodError } from '../../formatters.js';
import { invariant } from '../../utils.js';
import { PublicationMetadataSchema } from '../index.js';

export const toUsableUrl = (src: string) => {
function toUsableUrl(src: string) {
const url = new URL(src);

switch (url.protocol) {
Expand All @@ -23,32 +22,64 @@ export const toUsableUrl = (src: string) => {
default:
return src;
}
};
}

async function fetchMetadata(url: string): Promise<unknown> {
try {
const response = await fetch(url);

if (!response.ok) {
// ignore 404s
return null;
}

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const json = await response.json();

return json as unknown;
} catch {
return null;
}
}

async function fetchLastPublications(client: LensClient, pages: number) {
const pagination = await client.explore.publications({
orderBy: ExplorePublicationsOrderByType.Latest,
limit: LimitType.Fifty,
});

const publications = pagination.items;

while (--pages) {
const nextPage = await pagination.next();

if (nextPage) {
publications.push(...nextPage.items);
}
}

return publications;
}

describe(`Given the PublicationMetadataSchema`, () => {
const client = new LensClient({
environment: production,
});
const pages = 20;

describe(`when parsing the last 50 publications`, () => {
describe(`when parsing the last ${pages * 50} publications`, () => {
it('should pass without errors', async () => {
const page = await client.explore.publications({
orderBy: ExplorePublicationsOrderByType.Latest,
limit: LimitType.Fifty,
});
const publications = await fetchLastPublications(client, pages);

await Promise.all(
page.items.map(async (pub) => {
publications.map(async (pub, idx) => {
const url = toUsableUrl(pub.metadata.rawURI);
const response = await fetch(url);
const json = await fetchMetadata(url);

invariant(
response.ok,
`Failed to fetch metadata for ${pub.id}: ${url}, status: ${response.status}`,
);

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const json = await response.json();
if (!json) {
// skip in case we can't fetch the metadata
return;
}

const result = PublicationMetadataSchema.safeParse(json);

Expand All @@ -57,12 +88,12 @@ describe(`Given the PublicationMetadataSchema`, () => {
}

throw new Error(
`Failed to parse metadata for ${pub.id} (${url}) due to ${formatZodError(
`Failed to parse metadata for ${pub.id} at ${idx} (${url}) due to ${formatZodError(
result.error,
)}`,
);
}),
);
}, 100_000);
}, 200_000);
});
});
2 changes: 1 addition & 1 deletion src/publication/common/marketplace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export const MarketplaceMetadataSchema = z.object({
'and will allow users to leave OpenSea and view the item on the site.',
).optional(),

name: nonEmptyStringSchema('Name of the NFT item.').optional(),
name: z.string({ description: 'Name of the NFT item.' }).optional(),

attributes: MarketplaceMetadataAttributeSchema.array()
.optional()
Expand Down

0 comments on commit 0c8ca52

Please sign in to comment.