167 lines
5.6 KiB
JavaScript
167 lines
5.6 KiB
JavaScript
/**
|
||
* Sanity CMS rollout — Phase 0 + document presence (Steps 2–7).
|
||
*
|
||
* Run from Hydrogen/web:
|
||
* node --env-file=.env ./scripts/sanity-rollout-verify.mjs
|
||
*
|
||
* Read-only (no write token). Uses public API read; if queries fail, check project/dataset/CORS.
|
||
*/
|
||
|
||
import {createClient} from '@sanity/client';
|
||
|
||
function req(name) {
|
||
const v = process.env[name];
|
||
if (!v || !String(v).trim()) {
|
||
console.error(`Missing required env: ${name}`);
|
||
process.exit(1);
|
||
}
|
||
return String(v).trim();
|
||
}
|
||
|
||
const projectId = req('SANITY_PROJECT_ID');
|
||
const dataset = req('SANITY_DATASET');
|
||
const apiVersion = process.env.SANITY_API_VERSION?.trim() || '2026-04-16';
|
||
|
||
const client = createClient({
|
||
projectId,
|
||
dataset,
|
||
apiVersion,
|
||
useCdn: process.env.NODE_ENV === 'production',
|
||
});
|
||
|
||
function ok(label, pass, detail = '') {
|
||
const mark = pass ? '✓' : '✗';
|
||
console.log(`${mark} ${label}${detail ? ` — ${detail}` : ''}`);
|
||
}
|
||
|
||
async function main() {
|
||
console.log(`Sanity rollout verify — project=${projectId} dataset=${dataset}\n`);
|
||
|
||
const writeConfigured = Boolean(process.env.SANITY_API_WRITE_TOKEN?.trim());
|
||
ok('SANITY_API_WRITE_TOKEN set (needed for PDP seed / reviews API)', writeConfigured);
|
||
|
||
let productTotal = 0;
|
||
let productWithSlug = 0;
|
||
try {
|
||
productTotal = await client.fetch(`count(*[_type == "product"])`);
|
||
productWithSlug = await client.fetch(
|
||
`count(*[_type == "product" && defined(store.slug.current)])`,
|
||
);
|
||
ok('Sanity reachable (read API)', true);
|
||
} catch (e) {
|
||
ok('Sanity reachable (read API)', false, e?.message || String(e));
|
||
console.error('\nIf this failed: confirm CORS allows this origin, or run from a network that can reach api.sanity.io.');
|
||
process.exit(1);
|
||
}
|
||
|
||
ok(
|
||
`product documents (Shopify sync)`,
|
||
productTotal > 0,
|
||
`total=${productTotal}, with store.slug.current=${productWithSlug}`,
|
||
);
|
||
|
||
const sampleSlug = await client.fetch(
|
||
`*[_type == "product" && defined(store.slug.current)][0].store.slug.current`,
|
||
);
|
||
ok(
|
||
'Sample product slug (for PDP GROQ match)',
|
||
Boolean(sampleSlug),
|
||
sampleSlug ? String(sampleSlug) : 'none — run Shopify→Sanity sync',
|
||
);
|
||
|
||
const pdpFilled = await client.fetch(
|
||
`count(*[_type == "product" && defined(store.slug.current) && defined(abiirPdp.subtitle)])`,
|
||
);
|
||
ok(
|
||
'Products with abiirPdp.subtitle (editorial started)',
|
||
pdpFilled >= 0,
|
||
`${pdpFilled} of ${productWithSlug}`,
|
||
);
|
||
|
||
const displayRatings = await client.fetch(
|
||
`count(*[_type == "product" && defined(store.slug.current) && abiirPdp.rating > 0 && abiirPdp.reviewCount > 0])`,
|
||
);
|
||
ok(
|
||
'Products with display rating+count (cards / optional PDP stars)',
|
||
true,
|
||
`${displayRatings} products`,
|
||
);
|
||
|
||
console.log('\n--- Step 2: Global layout ---');
|
||
let settings = await client.fetch(
|
||
`*[_type == "settings" && _id == "settings"][0]{
|
||
"hasHeader": defined(header),
|
||
"hasFooter": defined(abiirFooter)
|
||
}`,
|
||
);
|
||
if (!settings) {
|
||
settings = await client.fetch(
|
||
`*[_type == "settings"][0]{
|
||
"hasHeader": defined(header),
|
||
"hasFooter": defined(abiirFooter)
|
||
}`,
|
||
);
|
||
}
|
||
ok('settings document', Boolean(settings));
|
||
ok('settings.header present', Boolean(settings?.hasHeader));
|
||
ok('settings.abiirFooter present', Boolean(settings?.hasFooter));
|
||
|
||
console.log('\n--- Step 3: Homepage (home singleton) ---');
|
||
const home = await client.fetch(`*[_type == "home" && _id == "home"][0]{
|
||
"hero": defined(hero),
|
||
"springGlowBanner": defined(springGlowBanner),
|
||
"brandValues": defined(brandValues),
|
||
"featuredProducts": defined(featuredProducts),
|
||
"ingredientsSection": defined(ingredientsSection),
|
||
"skinRitualSection": defined(skinRitualSection),
|
||
"testimonialsSection": defined(testimonialsSection),
|
||
"lifestyleGridSection": defined(lifestyleGridSection),
|
||
"newsletterSection": defined(newsletterSection)
|
||
}`);
|
||
ok('home document (_id=home)', Boolean(home));
|
||
if (home) {
|
||
for (const [k, v] of Object.entries(home)) {
|
||
ok(` home.${k}`, Boolean(v));
|
||
}
|
||
}
|
||
|
||
console.log('\n--- Step 4: About ---');
|
||
const aboutId = await client.fetch(`*[_type == "aboutPage" && _id == "aboutPage"][0]._id`);
|
||
ok('aboutPage (_id=aboutPage)', Boolean(aboutId));
|
||
|
||
console.log('\n--- Step 5: Journal ---');
|
||
const journalIndex = await client.fetch(`*[_type == "journalIndex" && _id == "journalIndex"][0]._id`);
|
||
const postCount = await client.fetch(`count(*[_type == "journalPost"])`);
|
||
ok('journalIndex (_id=journalIndex)', Boolean(journalIndex));
|
||
ok('journalPost count', postCount >= 0, String(postCount));
|
||
|
||
console.log('\n--- Step 6: Static / legal singletons ---');
|
||
const staticChecks = [
|
||
['abiirFaqPage', 'abiirFaqPage'],
|
||
['abiirShippingPage', 'abiirShippingPage'],
|
||
['abiirTrackOrderPage', 'abiirTrackOrderPage'],
|
||
['abiirSkincareQuizPage', 'abiirSkincareQuizPage'],
|
||
['abiirContactPage', 'abiirContactPage'],
|
||
['abiirPressPage', 'abiirPressPage'],
|
||
['abiirPrivacyPolicyPage', 'abiirPrivacyPolicyPage'],
|
||
['abiirTermsOfServicePage', 'abiirTermsOfServicePage'],
|
||
];
|
||
for (const [t, id] of staticChecks) {
|
||
const exists = await client.fetch(`*[_type == "${t}" && _id == "${id}"][0]._id`);
|
||
ok(`${t} (_id=${id})`, Boolean(exists));
|
||
}
|
||
|
||
console.log('\n--- CORS reminder (manual in sanity.io/manage) ---');
|
||
console.log(
|
||
' Add your local + production storefront origins to API → CORS + allowed query hosts.',
|
||
);
|
||
console.log(' PDP preview: ensure Studio “Presentation” URL matches Hydrogen dev/preview URL.\n');
|
||
|
||
process.exit(0);
|
||
}
|
||
|
||
main().catch((e) => {
|
||
console.error(e);
|
||
process.exit(1);
|
||
});
|