@taprootio/site-authoring 0.36.1 → 0.37.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,9 @@
1
1
  import { lstat, realpath } from "node:fs/promises";
2
2
  import path from "node:path";
3
3
 
4
- import { NAVIGATION_MAXIMUM_DEPTH } from "../api.js";
4
+ import { NAVIGATION_MAXIMUM_DEPTH, withRefusalGuidance } from "../api.js";
5
5
  import { loadSiteConfig } from "../config.js";
6
- import { VERB_VALIDATE } from "../constants.js";
6
+ import { SURFACE_DOCS_PRESENTATION, VERB_VALIDATE } from "../constants.js";
7
7
  import { freeFormRootPresentation } from "../content/free-form-sections.js";
8
8
  import { autolinkWarnings } from "../content/link-warnings.js";
9
9
  import { isCanonicalUuid, SiteAuthoringError } from "../errors.js";
@@ -21,8 +21,20 @@ import { FORMS_DIRECTORY } from "../forms-contract.js";
21
21
  import { authorWarnings, descriptionWarnings } from "../page-metadata.js";
22
22
  import { CHECK_AREA, collectProblems, refuseProblems } from "../problems.js";
23
23
  import { isRedirectMapRevision, REDIRECT_KIND_GONE, REDIRECTS_FILE_NAME } from "../redirects-contract.js";
24
- import { boundedList, successResult } from "../session.js";
25
- import { evaluateOfflineSiteChecks } from "../site-check-findings.js";
24
+ import { boundedList, openSession, successResult } from "../session.js";
25
+ import {
26
+ evaluateOfflineSiteChecks,
27
+ IMAGE_CHECK_DIRECTORY_ARGUMENT,
28
+ IMAGE_CHECK_DOCS_SITE,
29
+ IMAGE_CHECK_NOT_REQUESTED,
30
+ imageCheckSkipped,
31
+ } from "../site-check-findings.js";
32
+ import {
33
+ evaluateImagePrecheck,
34
+ fitPrecheckToBudget,
35
+ placedImageIdsForPages,
36
+ readImageRecords,
37
+ } from "../site-health-precheck.js";
26
38
  import { SETTINGS_GROUPS } from "../settings-catalog.js";
27
39
  import { checkWorkspace } from "../workspace-check.js";
28
40
  import {
@@ -49,6 +61,13 @@ import {
49
61
  import { documentImageIds, isAuthorableTemplateType, placedVideoIds } from "../typed-pages.js";
50
62
 
51
63
  const MAXIMUM_REPORTED = 200;
64
+ // What no validate run proves about the site checks: the ones the site itself answers, the deploy's and the editor's.
65
+ const SITE_CHECKS_NOT_PROVEN =
66
+ "image description defaults (unless --site reads the site's image records), and the site checks listed in siteChecks.checkedBySite (missing images, forms), which 'taproot-site status' and "
67
+ + "'taproot-site health' report from the stored deployment report, siteChecks.checkedAtDeploy, which only a deploy "
68
+ + "applies, or siteChecks.checkedInEditor, which only the page editor evaluates (a cover, a description field, or a "
69
+ + "description's length); images an integration places, or a video poster a push copies, which the stored report "
70
+ + "after staging covers";
52
71
  const FIXTURE_ROOT_KEYS = new Set(FIXTURE_ROOT_FIELDS);
53
72
  const FIXTURE_METADATA_KEYS = new Set(FIXTURE_METADATA_FIELDS);
54
73
 
@@ -56,6 +75,16 @@ function fail(code, message, field, exitCode) {
56
75
  throw new SiteAuthoringError(code, message, { field, exitCode });
57
76
  }
58
77
 
78
+ /** `validate --site` reads the site for the workspace the configuration names, so a directory or --init has none. */
79
+ export function siteNeedsWorkspaceError(init) {
80
+ return new SiteAuthoringError(
81
+ "validate.site_needs_workspace",
82
+ "validate --site reads the site for the workspace taproot-site.json names, so it takes no directory and no "
83
+ + "--init: a directory has no configuration to read the site with. Run it from the workspace.",
84
+ { field: init ? "init" : "fixturePath", exitCode: 2 },
85
+ );
86
+ }
87
+
59
88
  function isPlainObject(value) {
60
89
  return value !== null && typeof value === "object" && !Array.isArray(value);
61
90
  }
@@ -653,18 +682,65 @@ function pageHints(checked, onProgress) {
653
682
  * The offline site checks (TR01085) over the planned pages: the rules the shared
654
683
  * catalogue lists for `validate`, with the rest named as checked by the site.
655
684
  */
656
- function offlineSiteChecks(checked, manifest, onProgress) {
685
+ function offlineSiteChecks(checked, manifest, onProgress, imageCheck) {
657
686
  if (checked.settingsOnly) return {};
658
687
  return evaluateOfflineSiteChecks({
659
688
  planned: checked.pages?.planned,
660
689
  pagePaths: manifest.pages.map((entry) => normalizePagePath(entry?.path)).filter((value) => value !== undefined),
661
690
  pagesComplete: manifest.pagesTruncated !== true,
662
691
  formKeysChecked: checked.formKeysChecked === true,
692
+ imageCheck,
663
693
  onProgress,
664
694
  });
665
695
  }
666
696
 
697
+ /**
698
+ * `validate --site`: reads the site's records for the images the workspace places and reports the ones with no
699
+ * default description (TR01371). Runs after the workspace's own problems are refused, so those come before any
700
+ * credential error. A Docs workspace, and a workspace placing no images, open no session and make no request.
701
+ */
702
+ async function readSiteImages(checked, invocation, onProgress, config) {
703
+ if (checked.settingsOnly || config?.authoringSurface === SURFACE_DOCS_PRESENTATION) {
704
+ return { imageCheck: imageCheckSkipped(IMAGE_CHECK_DOCS_SITE) };
705
+ }
706
+ const pages = placedImageIdsForPages(checked.pages.planned);
707
+ const ids = pages.flatMap((page) => page.imageIds);
708
+ if (ids.length === 0) return { imageCheck: { ran: true }, pages, block: emptyBlock(pages), opened: false };
709
+ // The configuration this workspace was validated against, not a reread one: a site switched meanwhile must not be
710
+ // the one whose images are read.
711
+ const session = await openSession({ ...invocation, resolvedConfig: config });
712
+ onProgress("Reading the site's records for the images this workspace places.");
713
+ const state = await withRefusalGuidance(onProgress, "validate", async () => {
714
+ const read = await readImageRecords(session.client, session.siteId, ids);
715
+ // A key the site refused is a failure of this verb, with the server's own description (the read made the
716
+ // first request, so a revoked key shows up here).
717
+ if (read.refusalError !== undefined) throw read.refusalError;
718
+ return read;
719
+ });
720
+ const block = evaluateImagePrecheck(state, pages, onProgress);
721
+ return {
722
+ imageCheck: state.reason === undefined ? { ran: true } : { ran: false, code: state.reason.code, message: state.reason.message },
723
+ pages,
724
+ block: state.status === "not_checked" ? undefined : block,
725
+ opened: true,
726
+ };
727
+ }
728
+
729
+ function emptyBlock(pages) {
730
+ return {
731
+ status: "checked",
732
+ checkedPages: pages.length,
733
+ imageFindings: { total: 0, items: [] },
734
+ notInSiteLibrary: { total: 0, items: [] },
735
+ skipped: [],
736
+ };
737
+ }
738
+
667
739
  export async function validateFixture(invocation = {}) {
740
+ // Refused before any file is read: a directory, or a fixture to export, has no site to read.
741
+ if (invocation.fromSite === true && (invocation.init || invocation.fixturePath !== undefined)) {
742
+ throw siteNeedsWorkspaceError(Boolean(invocation.init));
743
+ }
668
744
  if (invocation.init) return await initializeFixture(invocation, validateFixture);
669
745
  const onProgress = typeof invocation.onProgress === "function" ? invocation.onProgress : () => {};
670
746
  const cwd = invocation.cwd ?? process.cwd();
@@ -677,7 +753,7 @@ export async function validateFixture(invocation = {}) {
677
753
  "siteId",
678
754
  );
679
755
  }
680
- return await validatePulledWorkspace(config.workspaceDir, config.siteId, invocation, onProgress);
756
+ return await validatePulledWorkspace(config.workspaceDir, config.siteId, invocation, onProgress, { config });
681
757
  }
682
758
  const fixtureRoot = await resolveFixtureRoot(cwd, invocation.fixturePath);
683
759
  if (
@@ -688,7 +764,7 @@ export async function validateFixture(invocation = {}) {
688
764
  if (!isCanonicalUuid(siteId)) {
689
765
  fail("workspace.manifest_invalid", `${MANIFEST_FILE_NAME} does not record a canonical site id.`, "siteId");
690
766
  }
691
- return await validatePulledWorkspace(fixtureRoot, siteId, invocation, onProgress);
767
+ return await validatePulledWorkspace(fixtureRoot, siteId, invocation, onProgress, { directoryArgument: true });
692
768
  }
693
769
 
694
770
  onProgress(`Reading ${FIXTURE_MANIFEST_FILE_NAME}.`);
@@ -752,7 +828,7 @@ export async function validateFixture(invocation = {}) {
752
828
  ...autolinkWarnings(checked.pages?.planned, onProgress),
753
829
  ...descriptionWarnings(checked.pages?.planned, onProgress),
754
830
  ...authorWarnings(checked.pages?.planned, onProgress),
755
- ...offlineSiteChecks(checked, manifest, onProgress),
831
+ ...offlineSiteChecks(checked, manifest, onProgress, imageCheckSkipped(IMAGE_CHECK_DIRECTORY_ARGUMENT)),
756
832
  };
757
833
  onProgress(
758
834
  `Validated ${validatedPages.length} page(s), ${checked.navigation.items} navigation item(s), two themes, appearance, and footer without credentials or mutation.`,
@@ -782,9 +858,7 @@ export async function validateFixture(invocation = {}) {
782
858
  "credential authorization or live site ownership",
783
859
  "remote concurrency or current revisions",
784
860
  "server normalization and pull round trips",
785
- "the site checks listed in siteChecks.checkedBySite (images, forms), which 'taproot-site status' reports under "
786
- + "siteHealth, siteChecks.checkedAtDeploy, which only a deploy applies, or siteChecks.checkedInEditor, "
787
- + "which only the page editor evaluates (a cover, a description field, or a description's length)",
861
+ SITE_CHECKS_NOT_PROVEN,
788
862
  "preview or published rendering",
789
863
  ],
790
864
  nextStep: "Run a real pull and authorized preview before approval or deployment.",
@@ -796,7 +870,7 @@ export async function validateFixture(invocation = {}) {
796
870
  * runs before it sends anything, for every page and setting at once, against
797
871
  * what the manifests recorded at the pull — no credential and no request.
798
872
  */
799
- async function validatePulledWorkspace(workspaceDir, siteId, invocation, onProgress) {
873
+ async function validatePulledWorkspace(workspaceDir, siteId, invocation, onProgress, { config, directoryArgument = false }) {
800
874
  onProgress(`Reading ${MANIFEST_FILE_NAME}.`);
801
875
  const manifest = await readManifest(workspaceDir, siteId);
802
876
  const mediaManifest = await readMediaManifest(workspaceDir, siteId);
@@ -811,6 +885,10 @@ async function validatePulledWorkspace(workspaceDir, siteId, invocation, onProgr
811
885
  reportThemeWarnings(checked.presentation, onProgress);
812
886
  refuseProblems(checked.problems, "validate found problems in the workspace");
813
887
 
888
+ // Only --site reads the site; without it the image rule is skipped with the reason (TR01371).
889
+ const site = invocation.fromSite === true
890
+ ? await readSiteImages(checked, invocation, onProgress, config)
891
+ : { imageCheck: imageCheckSkipped(directoryArgument ? IMAGE_CHECK_DIRECTORY_ARGUMENT : IMAGE_CHECK_NOT_REQUESTED) };
814
892
  const pages = checked.settingsOnly
815
893
  ? []
816
894
  : checked.pages.planned.map((page) => ({ file: page.file, path: displayPagePath(page.pagePath) }));
@@ -821,20 +899,36 @@ async function validatePulledWorkspace(workspaceDir, siteId, invocation, onProgr
821
899
  ...autolinkWarnings(checked.pages.planned, onProgress),
822
900
  ...descriptionWarnings(checked.pages.planned, onProgress),
823
901
  ...authorWarnings(checked.pages.planned, onProgress),
824
- ...offlineSiteChecks(checked, manifest, onProgress),
902
+ ...offlineSiteChecks(checked, manifest, onProgress, site.imageCheck),
825
903
  };
904
+ // A Docs workspace has no `siteChecks` to carry the reason, so it is said at the top level.
905
+ const docsReason = checked.settingsOnly && invocation.fromSite === true
906
+ ? {
907
+ imageCheckReason: {
908
+ code: site.imageCheck.code,
909
+ ...(site.imageCheck.message === undefined ? {} : { message: site.imageCheck.message }),
910
+ },
911
+ }
912
+ : {};
913
+ const readSite = site.opened === true;
914
+ const noImages = invocation.fromSite === true && site.opened === false;
826
915
  onProgress(
827
916
  checked.settingsOnly
828
917
  ? "Validated the workspace's two themes, appearance, and footer without credentials or mutation."
829
918
  : `Validated ${pages.length} page(s), ${checked.navigation.items} navigation item(s), two themes, appearance, and `
830
- + "footer without credentials or mutation.",
919
+ + (readSite
920
+ ? "footer, reading the site's image records without mutation."
921
+ : noImages
922
+ ? "footer without credentials or mutation; the workspace places no images, so the site was not read."
923
+ : "footer without credentials or mutation."),
831
924
  );
832
- return successResult(VERB_VALIDATE, siteId, {
833
- offline: true,
925
+ const base = successResult(VERB_VALIDATE, siteId, {
926
+ offline: !readSite,
834
927
  workspace: { manifest: MANIFEST_FILE_NAME, settingsOnly: checked.settingsOnly },
835
928
  ...validatedSummary(checked, pages),
836
929
  hints,
837
930
  ...warnings,
931
+ ...docsReason,
838
932
  proves: [
839
933
  "every page source: metadata, Markdown conversion, media references, content vocabulary, and section contexts",
840
934
  "page paths, templates, and system pages against the pages the pull recorded",
@@ -842,16 +936,26 @@ async function validatePulledWorkspace(workspaceDir, siteId, invocation, onProgr
842
936
  "redirect-map shape, normalization, chains, loops, and workspace page paths",
843
937
  "complete theme, appearance, header, brand, and footer semantics",
844
938
  "form files against the shared field schema",
939
+ ...(site.block === undefined ? [] : [
940
+ site.imageCheck.ran
941
+ ? "which placed images have no default description, from the site's own image records (imageFindings)"
942
+ : "which of the placed images that were read have no default description (imageFindings); some records could not be read",
943
+ ]),
845
944
  ],
846
945
  doesNotProve: [
847
946
  "credential authorization or live site ownership",
848
947
  "pages, revisions, or videos that changed on the site since the pull",
849
948
  "server normalization and pull round trips",
850
- "the site checks listed in siteChecks.checkedBySite (images, forms), which 'taproot-site status' reports under "
851
- + "siteHealth, siteChecks.checkedAtDeploy, which only a deploy applies, or siteChecks.checkedInEditor, "
852
- + "which only the page editor evaluates (a cover, a description field, or a description's length)",
949
+ SITE_CHECKS_NOT_PROVEN,
853
950
  "preview or published rendering",
854
951
  ],
855
952
  nextStep: "Run 'taproot-site plan' to check the workspace against the live site and see the ordered apply.",
856
953
  });
954
+ if (site.block === undefined) return base;
955
+ const fitted = fitPrecheckToBudget(
956
+ base,
957
+ { imageFindings: site.block.imageFindings, notInSiteLibrary: site.block.notInSiteLibrary },
958
+ ["notInSiteLibrary", "imageFindings"],
959
+ );
960
+ return { ...base, ...fitted };
857
961
  }