All files / apps/wp-editor/shared rules.ts

97.14% Statements 102/105
92.63% Branches 88/95
100% Functions 36/36
100% Lines 85/85

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238                      3x         3x 34x               3x 34x   34x       3x 37x     3x 227x   3x 65x     3x 45x     3x 62x   62x   76x 88x   62x     3x 3x   37x   3x   4x   3x   3x   3x 5x     3x       33x   33x   33x   33x   35x   33x 6x   6x     32x   27x           3x 15x   15x 151x     15x       3x 15x   15x       3x 10x 8x   6x   6x   6x           3x         29x   27x             3x 9x   7x           3x     5x             3x 26x     3x 10x   10x               10x 8x           2x   2x       3x 32x                       3x           26x   26x   26x   26x 23x   21x   4x   21x 25x 3x     21x        
import { Refusal } from "./refusal.ts";
import type { Changeset } from "./types/Changeset.ts";
import type { Item } from "./types/Item.ts";
import type { ItemState } from "./types/ItemState.ts";
import type { Proposal } from "./types/Proposal.ts";
import type { ReviewLevel } from "./types/ReviewLevel.ts";
import type { RunReport } from "./types/RunReport.ts";
import type { Settings } from "./types/Settings.ts";
import type { Snapshot } from "./types/Snapshot.ts";
import { DEFAULT_SETTINGS, MAX_PAUSE_MS, REVIEW_GATES } from "./constants.ts";
 
const RANK = { unreviewed: 0, seen: 1, confirmed: 2 } as const satisfies Record<
    ReviewLevel,
    number
>;
 
const slugify = (title: string): string =>
    title
        .normalize("NFD")
        .replace(/\p{Diacritic}/gu, "")
        .toLowerCase()
        .replace(/[^a-z0-9]+/g, "-")
        .replace(/^-|-$/g, "")
        .slice(0, 40) || "changeset";
 
export const nextChangesetId = (existing: Changeset[], title: string): string => {
    const last = Math.max(0, ...existing.map(({ id }) => Number.parseInt(id, 10) || 0));
 
    return `${String(last + 1).padStart(4, "0")}-${slugify(title)}`;
};
 
/** Returns the open changeset that blocks a new one, or null when a new one may open. */
export const openBlocker = (existing: Changeset[], override: boolean): Changeset | null =>
    override ? null : (existing.find(({ status }) => status === "open") ?? null);
 
/** Has a proposal that is neither rejected nor live; a restored article is pending again. */
export const isPending = ({ proposal, state }: Pick<Item, "proposal" | "state">): boolean =>
    proposal !== null && !state.rejected && !(state.applied && !state.restored);
 
export const isApplied = ({ state }: Pick<Item, "state">): boolean =>
    !!state.applied && !state.restored;
 
/** The live `modified_gmt` if nothing but this tool wrote the article since the pull. */
export const expectedModified = ({ base, state }: Pick<Item, "base" | "state">): string =>
    state.restored?.modifiedGmt ?? base.modifiedGmt;
 
/** Open until every key is scanned and every proposal is applied or rejected. */
export const derivedStatus = (changeset: Changeset, items: Item[]): Changeset["status"] => {
    Iif (changeset.status === "abandoned") return "abandoned";
 
    const scanned = new Set(changeset.scanned);
    // A restore never reopens a changeset, or restoring through the stack would open a second one.
    const waiting = items.some((item) => isPending(item) && !item.state.applied);
    const done = changeset.keys.every((key) => scanned.has(key)) && !waiting;
 
    return done ? "closed" : "open";
};
 
export const markSeen = (state: ItemState): ItemState =>
    state.level === "unreviewed" ? { ...state, level: "seen" } : state;
 
export const confirm = (state: ItemState): ItemState => ({ ...state, level: "confirmed" });
 
export const unconfirm = (state: ItemState): ItemState => ({ ...state, level: "seen" });
 
export const afterEdit = (state: ItemState): ItemState => ({ ...state, level: "seen" });
 
export const reject = (state: ItemState): ItemState => ({ ...state, rejected: true });
 
export const unreject = (state: ItemState): ItemState => ({ ...state, rejected: undefined });
 
export const isReviewRequested = ({ proposal }: Pick<Item, "proposal">): boolean =>
    !!proposal?.reviewRequested;
 
/** Why an empty queue, the review gate or drift keeps bulk apply shut, or null when none do. */
export const gateBlocker = (
    items: Item[],
    { reviewGate, bulkApplyMinLevel }: Settings,
): string | null => {
    const pending = items.filter(isPending);
 
    Iif (pending.length === 0) return "Nothing left to apply.";
 
    const gated = reviewGate === "requested" ? pending.filter(isReviewRequested) : pending;
    const short =
        reviewGate === "none"
            ? []
            : gated.filter(({ state }) => RANK[state.level] < RANK[bulkApplyMinLevel]);
 
    if (short.length > 0) {
        const scope = reviewGate === "requested" ? "review-requested" : "pending";
 
        return `${short.length} of ${gated.length} ${scope} articles are not yet ${bulkApplyMinLevel}.`;
    }
 
    const drifted = pending.filter(({ state }) => state.drift).length;
 
    return drifted > 0
        ? `${drifted} articles changed in WordPress since pull; re-pull them.`
        : null;
};
 
// FNV-1a: only compared for equality, never secret, and the app bundle can't use node:crypto.
const hash = (text: string): string => {
    let value = 0x811c9dc5;
 
    for (const char of text) {
        value = Math.imul(value ^ (char.codePointAt(0) ?? 0), 0x01000193);
    }
 
    return (value >>> 0).toString(16).padStart(8, "0");
};
 
/** Changes whenever what an apply would write, or the timestamp it checks drift against, changes. */
export const itemFingerprint = (item: Pick<Item, "base" | "state" | "proposal">): string => {
    const { content = "", title = "", excerpt = "" } = item.proposal ?? {};
 
    return hash([expectedModified(item), content, title, excerpt].join("\0"));
};
 
/** Why the last dry run doesn't cover the pending articles, or null when it does. */
const dryRunBlocker = (items: Item[], lastDryRun: RunReport | null): string | null => {
    if (!lastDryRun) return "Run a dry run first.";
    if (lastDryRun.incomplete) return "The last dry run didn't finish; run it again.";
 
    const unchecked = items
        .filter(isPending)
        .filter((item) => lastDryRun.fingerprints?.[item.key] !== itemFingerprint(item)).length;
 
    return unchecked > 0
        ? `${unchecked} pending articles weren't cleared by the last dry run; run it again.`
        : null;
};
 
/** Why bulk apply is disabled, or null when it may run. */
export const bulkApplyBlocker = (
    items: Item[],
    settings: Settings,
    lastDryRun: RunReport | null,
): string | null => {
    if (settings.dryRun) return "Dry-run mode is on, so nothing is written to WordPress.";
 
    return (
        gateBlocker(items, settings) ??
        (settings.requireDryRun ? dryRunBlocker(items, lastDryRun) : null)
    );
};
 
/** Why restore-all is disabled, or null when it may run. */
export const bulkRestoreBlocker = (items: Item[], settings: Settings): string | null => {
    if (settings.dryRun) return "Dry-run mode is on, so nothing is written to WordPress.";
 
    return items.some(isApplied) ? null : "Nothing is applied, so there is nothing to restore.";
};
 
type Text = Pick<Snapshot, "content" | "title" | "excerpt">;
 
/** What WordPress holds once `item` is applied; a title or excerpt it leaves out stays as it was. */
export const appliedText = ({
    base,
    proposal,
}: Pick<Item, "base"> & { proposal: Proposal }): Text => ({
    content: proposal.content,
    title: proposal.title ?? base.title,
    excerpt: proposal.excerpt ?? base.excerpt,
});
 
/** Whether WordPress already holds `text`, as when a write landed but its response never came back. */
export const holdsText = (live: Text, text: Text): boolean =>
    live.content === text.content && live.title === text.title && live.excerpt === text.excerpt;
 
/** Merges a settings patch over `current`, refusing any field with an unknown value. */
export const parseSettings = (patch: unknown, current: Settings = DEFAULT_SETTINGS): Settings => {
    const next = { ...current, ...(patch as Partial<Settings> | null) };
    const valid =
        REVIEW_GATES.includes(next.reviewGate) &&
        (next.bulkApplyMinLevel === "seen" || next.bulkApplyMinLevel === "confirmed") &&
        typeof next.requireDryRun === "boolean" &&
        typeof next.dryRun === "boolean" &&
        Number.isInteger(next.pauseMs) &&
        next.pauseMs >= 0 &&
        next.pauseMs <= MAX_PAUSE_MS;
 
    if (!valid) {
        throw new Refusal(
            `Settings are reviewGate ${REVIEW_GATES.join("|")}, bulkApplyMinLevel seen|confirmed, ` +
                `requireDryRun and dryRun booleans, pauseMs a whole number from 0 to ${MAX_PAUSE_MS}.`,
        );
    }
 
    const { reviewGate, bulkApplyMinLevel, requireDryRun, dryRun, pauseMs } = next;
 
    return { reviewGate, bulkApplyMinLevel, requireDryRun, dryRun, pauseMs };
};
 
/** Why a write is refused because WordPress moved since `expected`, or null when it may go ahead. */
export const driftRefusal = (expected: string, live: string): string | null =>
    expected === live ? null : "Changed in WordPress since pull, re-pull.";
 
interface StackEntry {
    changeset: Changeset;
    item: Item | undefined;
}
 
/**
 * Why `item` of the changeset at `stack[index]` can't be restored, or null when it can.
 * `stack` is every changeset in creation order, each with its copy of the same key. `landed` is
 * true when WordPress already holds this changeset's base, so its timestamp can't count as drift.
 */
export const restoreBlocker = (
    stack: StackEntry[],
    index: number,
    live: string,
    landed = false,
): string | null => {
    const own = stack[index]?.item;
 
    Iif (!own || !isApplied(own)) return "Not applied, so there is nothing to restore.";
 
    const later = stack.slice(index + 1).find(({ item }) => item && isApplied(item));
 
    if (later) return `Restore it in changeset ${later.changeset.id} first; it is applied there.`;
    if (landed) return null;
 
    const writes = [
        own.state.applied!,
        ...stack.slice(index + 1).map(({ item }) => item?.state.restored),
    ];
    const last = writes
        .filter((write) => write !== undefined)
        .sort((a, b) => a.at.localeCompare(b.at))
        .at(-1)!;
 
    return last.modifiedGmt === live
        ? null
        : "Changed in WordPress since it was applied; restoring would overwrite that.";
};