All files / apps/developer-portal/layers/workflows/shared project.ts

87.82% Statements 101/115
74.68% Branches 59/79
92.5% Functions 37/40
87.87% Lines 87/99

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 239 240 241 242 243 244 245 246                        46x 161x   161x 63x       161x     46x 161x   161x 9x 9x   9x 9x   9x     9x               161x     46x 161x   161x             46x 75x     46x 8x   6x 8x   8x 1x     1x     5x   8x       46x                   46x       161x 161x   161x   161x   161x 1x         161x 161x       8x     161x                         161x     46x 1195x     46x         863x 863x     46x 6x   6x       46x 741x         46x 86x 70x 59x   59x   59x 1x   58x 2x   56x     46x 1x   1x     1x     1x     1x   1x           1x       46x 86x 119x   86x     3x   1x     86x         46x 317x 268x       234x   113x 112x         191x       517x 205x           122x            
import { cycleProblem, readCapability, withStageNeeds } from "./capability.ts";
import { complexityOf } from "./complexity.ts";
import { asText, isRecord, Reader } from "./reader.ts";
import { SETTLED_STATES, STAGE_NEED_PREFIX, TICKET_SIZES } from "./constants.ts";
import type { Capability } from "./types/Capability.ts";
import type { Feature } from "./types/Feature.ts";
import type { Group } from "./types/Group.ts";
import type { Project } from "./types/Project.ts";
import type { SettledState } from "./types/SettledState.ts";
import type { Template } from "./types/Template.ts";
import type { TicketSize } from "./types/TicketSize.ts";
 
const readSwitches = (reader: Reader, source: Record<string, unknown>): Project["switches"] => {
    const switches: Project["switches"] = {};
 
    for (const [id, value] of Object.entries(reader.record(source, "switches", "project"))) {
        if (typeof value === "boolean") switches[id] = value;
        else Ereader.problems.push(`switch \`${id}\` must be true or false`);
    }
 
    return switches;
};
 
const readSettled = (reader: Reader, source: Record<string, unknown>): Project["settled"] => {
    const settled: Project["settled"] = {};
 
    for (const [id, value] of Object.entries(reader.record(source, "settled", "project"))) {
        const entry = isRecord(value) ? value : {};
        const status = asText(entry.status) ?? "";
 
        if ((SETTLED_STATES as readonly string[]).includes(status)) {
            const on = asText(entry.on);
 
            Iif (on !== null && !/^\d{4}-\d{2}-\d{2}$/.test(on)) {
                reader.problems.push(`settled \`${id}\`: \`on\` must be a YYYY-MM-DD date`);
            }
            settled[id] = { status: status as SettledState, note: asText(entry.note) ?? "", on };
        } else E{
            reader.problems.push(
                `settled \`${id}\`: \`status: ${status || "(missing)"}\` isn't one of ${SETTLED_STATES.join(", ")}`,
            );
        }
    }
 
    return settled;
};
 
const readTicketSize = (reader: Reader, source: Record<string, unknown>): TicketSize => {
    const size = reader.text(source, "ticketSize", "project", "block");
 
    Eif ((TICKET_SIZES as readonly string[]).includes(size)) return size as TicketSize;
 
    reader.problems.push(`\`ticketSize: ${size}\` isn't one of ${TICKET_SIZES.join(", ")}`);
    return "block";
};
 
/** The raw fields a type gives an extra; nulls dropped, since the reader rejects an explicit null. */
const fieldsOf = (type: Capability): Record<string, unknown> =>
    Object.fromEntries(Object.entries(type).filter(([, value]) => value !== null));
 
/** An extra naming a `type:`, with that type's fields under its own and both lists of needs. */
const withType = (reader: Reader, raw: unknown, types: Capability[]): unknown => {
    if (!isRecord(raw) || raw.type === undefined) return raw;
 
    const name = asText(raw.type) ?? "";
    const type = types.find((candidate) => candidate.id === name);
 
    if (!type) {
        reader.problems.push(
            `extra \`${asText(raw.id) ?? "?"}\` names type \`${name}\`, which the template doesn't have`,
        );
        return raw;
    }
 
    const own: unknown[] = Array.isArray(raw.dependsOn) ? raw.dependsOn : [];
 
    return { ...fieldsOf(type), ...raw, dependsOn: [...new Set([...type.dependsOn, ...own])] };
};
 
/** A page as the block its type makes it, as `readProject` reads an extra naming that type. */
export const typedExtra = (type: Capability, page: { id: string; title: string }): Capability => ({
    ...type,
    id: page.id,
    title: page.title,
});
 
/**
 * A project read from parsed YAML. Rules that need its template — a switch on a capability that
 * doesn't exist — live in `projectProblems`, because the template may be the broken file.
 */
export const readProject = (
    raw: unknown,
    types: Capability[] = [],
): { project: Project; problems: string[] } => {
    const reader = new Reader();
    const source = isRecord(raw) ? raw : {};
 
    Iif (!isRecord(raw)) reader.problems.push("the file isn't a mapping");
 
    const switches = readSwitches(reader, source);
 
    if (source.gates !== undefined) {
        reader.problems.push(
            "`gates:` is gone: a gate is an `owner` block now, approved by closing its ticket or settling it as done",
        );
    }
 
    const settled = readSettled(reader, source);
    const extras = reader
        .list(source, "extras", "project")
        .flatMap(
            (capability, index) =>
                readCapability(reader, withType(reader, capability, types), index) ?? [],
        );
 
    const project: Project = {
        id: reader.text(source, "id", "project"),
        title: reader.text(source, "title", "project"),
        template: reader.text(source, "template", "project"),
        area: reader.text(source, "area", "project", ""),
        milestone: reader.text(source, "milestone", "project"),
        summary: reader.text(source, "summary", "project", ""),
        switches,
        ticketSize: readTicketSize(reader, source),
        settled,
        extras,
    };
 
    return { project, problems: reader.problems };
};
 
const switchedOn = (switchable: Group | Capability, project: Project): boolean =>
    switchable.optional ? (project.switches[switchable.id] ?? switchable.default) : true;
 
/** Whether a group is on in a project; a capability outside any group is never held back by one. */
export const isGroupEnabled = (
    id: string | null,
    project: Project,
    template: Template,
): boolean => {
    const group = template.groups.find((candidate) => candidate.id === id);
    return group ? switchedOn(group, project) : true;
};
 
const isDemanded = (capability: Capability, project: Project, template: Template): boolean =>
    project.extras.some(
        (extra) =>
            extra.dependsOn.includes(capability.id) &&
            isGroupEnabled(extra.group, project, template),
    );
 
export const isEnabled = (capability: Capability, project: Project, template: Template): boolean =>
    isGroupEnabled(capability.group, project, template) &&
    (capability.onDemand
        ? isDemanded(capability, project, template)
        : switchedOn(capability, project));
 
const switchProblems = (project: Project, template: Template): string[] =>
    Object.keys(project.switches).flatMap((id) => {
        const capability = template.capabilities.find((candidate) => candidate.id === id);
        const switchable = capability ?? template.groups.find((group) => group.id === id);
 
        Iif (!switchable)
            return [`switch \`${id}\` names no capability or group in \`${template.id}\``];
        if (capability?.onDemand) {
            return [`switch \`${id}\` is on a block that comes in with the pages that need it`];
        }
        if (!switchable.optional) {
            return [`switch \`${id}\` is on a required ${capability ? "capability" : "group"}`];
        }
        return [];
    });
 
const extraProblems = (extra: Capability, template: Template, known: Set<string>): string[] => {
    const problems: string[] = [];
 
    Iif (template.capabilities.some((capability) => capability.id === extra.id)) {
        problems.push(`extra \`${extra.id}\` reuses a template id`);
    }
    Iif (extra.group && !template.groups.some((group) => group.id === extra.group)) {
        problems.push(`extra \`${extra.id}\` names group \`${extra.group}\`, which doesn't exist`);
    }
    Iif (!template.stages.some((stage) => stage.id === extra.stage)) {
        problems.push(`extra \`${extra.id}\` names stage \`${extra.stage}\`, which doesn't exist`);
    }
    const stageNeeds = new Set(template.stages.map(({ id }) => `${STAGE_NEED_PREFIX}${id}`));
 
    for (const dependency of extra.dependsOn.filter(
        (id) => !known.has(id) && !stageNeeds.has(id),
    )) {
        problems.push(`extra \`${extra.id}\` depends on \`${dependency}\`, which doesn't exist`);
    }
 
    return problems;
};
 
/** What a project says about its template that the template doesn't back up. */
export const projectProblems = (project: Project, template: Template): string[] => {
    const all = [...template.capabilities, ...project.extras];
    const known = new Set(all.map((capability) => capability.id));
 
    const problems = [
        ...switchProblems(project, template),
        ...Object.keys(project.settled)
            .filter((id) => !known.has(id))
            .map((id) => `settled \`${id}\` names no capability`),
        ...project.extras.flatMap((extra) => extraProblems(extra, template, known)),
    ];
 
    return project.extras.length > 0
        ? [...problems, ...cycleProblem(withStageNeeds(all))]
        : problems;
};
 
export const featuresOf = (project: Project, template: Template): Feature[] => {
    const priced = (capabilities: Capability[]) => ({
        blocks: capabilities.map(({ title, complexity }) => ({ title, complexity })),
        points: complexityOf(capabilities),
    });
 
    return [
        ...template.groups
            .filter((group) => group.optional)
            .map((group) => ({
                id: group.id,
                title: group.title,
                summary: group.summary,
                enabled: isGroupEnabled(group.id, project, template),
                ...priced(template.capabilities.filter((c) => c.group === group.id && !c.optional)),
                within: null,
            })),
        ...template.capabilities
            .filter((capability) => capability.optional)
            .map((capability) => ({
                id: capability.id,
                title: capability.title,
                summary: capability.summary,
                enabled: switchedOn(capability, project),
                ...priced([capability]),
                within: template.groups.some((g) => g.id === capability.group && g.optional)
                    ? capability.group
                    : null,
            })),
    ];
};