feat: expand ISAC content modeling
Validate and deploy production / validate (push) Canceled after 0s
Validate and deploy production / deploy (push) Canceled after 0s
Validate and deploy staging / validate (push) Successful in 28s
Validate and deploy staging / deploy (push) Successful in 1m3s

This commit is contained in:
jb
2026-08-03 01:23:20 -04:00
parent d9924a95b9
commit 57068b61cd
90 changed files with 9635 additions and 1270 deletions
+361 -227
View File
@@ -25,7 +25,10 @@ import {
getRegistrySearch,
getRegistrySort,
} from '$lib/server/pagination';
import { refreshAbilityFocuses } from '$lib/server/specialization-readiness';
import {
refreshAbilityFocuses,
refreshSpecializationFocusReady,
} from '$lib/server/specialization-readiness';
import { fail } from '@sveltejs/kit';
import { randomUUID } from 'node:crypto';
import type { Actions, PageServerLoad } from './$types';
@@ -36,6 +39,18 @@ import {
saveConditionalRuleInTransaction,
} from '$lib/server/conditional-effects';
import { MECHANIC_RECIPE_REGISTRY } from '$lib/calculations/mechanic-recipes';
import {
normalizeSkillChipProfiles,
SKILL_CHIP_QUALITIES,
SKILL_CHIP_SECONDARY_QUALITIES,
} from '$lib/skill-chips/profiles';
import {
getSkillChipPrimaryAttributeIds,
getSkillChipPrimaryProfiles,
getSkillChipSecondaryPool,
replaceSkillChipPrimaryProfiles,
replaceSkillChipSecondaryPool,
} from '$lib/server/skill-chip-profiles';
const mechanicOutcomes = (
rule: ReturnType<typeof parseConditionalRulePayload>,
@@ -68,6 +83,16 @@ const renderAuditMetadata = (render: Record<string, any> | null | undefined) =>
}
: null;
async function refreshAllFocusReadiness(
database: Awaited<ReturnType<typeof getDatabase>>,
) {
const focuses = await (
await database.prepare('SELECT id FROM specialization_focuses')
).all();
for (const focus of focuses)
await refreshSpecializationFocusReady(database, String(focus.id));
}
async function snapshot(id: string) {
const database = await getDatabase();
const talent = await (
@@ -91,16 +116,6 @@ async function snapshot(id: string) {
'SELECT * FROM skill_chip_two_piece_bonuses WHERE skill_chip_set_id=?',
)
).get(id)) ?? null,
primaryOptions: await (
await database.prepare(
'SELECT stat_attribute_id FROM skill_chip_primary_options WHERE skill_chip_set_id=? ORDER BY stat_attribute_id',
)
).all(id),
secondaryOptions: await (
await database.prepare(
'SELECT stat_attribute_id FROM skill_chip_secondary_options WHERE skill_chip_set_id=? ORDER BY stat_attribute_id',
)
).all(id),
talent: talent ?? null,
talentEffects: talent
? await (
@@ -150,8 +165,8 @@ export const load: PageServerLoad = async ({ cookies, url }) => {
desc: "COALESCE((SELECT talent.name FROM skill_chip_talents talent WHERE talent.skill_chip_set_id=sets.id),'') COLLATE NOCASE DESC,sets.name COLLATE NOCASE ASC",
},
rollPools: {
asc: '((SELECT COUNT(*) FROM skill_chip_primary_options primary_option WHERE primary_option.skill_chip_set_id=sets.id)+(SELECT COUNT(*) FROM skill_chip_secondary_options secondary_option WHERE secondary_option.skill_chip_set_id=sets.id)) ASC,sets.name COLLATE NOCASE ASC',
desc: '((SELECT COUNT(*) FROM skill_chip_primary_options primary_option WHERE primary_option.skill_chip_set_id=sets.id)+(SELECT COUNT(*) FROM skill_chip_secondary_options secondary_option WHERE secondary_option.skill_chip_set_id=sets.id)) DESC,sets.name COLLATE NOCASE ASC',
asc: 'sets.name COLLATE NOCASE ASC,sets.id ASC',
desc: 'sets.name COLLATE NOCASE DESC,sets.id DESC',
},
state: {
asc: 'sets.active ASC,sets.name COLLATE NOCASE ASC',
@@ -173,7 +188,6 @@ export const load: PageServerLoad = async ({ cookies, url }) => {
: filter === 'configured'
? [
'EXISTS (SELECT 1 FROM skill_chip_two_piece_bonuses bonus WHERE bonus.skill_chip_set_id=sets.id)',
'EXISTS (SELECT 1 FROM skill_chip_talents talent WHERE talent.skill_chip_set_id=sets.id)',
]
: []),
...(!requestedItemId && search
@@ -198,7 +212,7 @@ export const load: PageServerLoad = async ({ cookies, url }) => {
SUM(CASE WHEN active=1 THEN 1 ELSE 0 END) AS active,
SUM(CASE WHEN active=0 THEN 1 ELSE 0 END) AS archived,
SUM(CASE WHEN specialization_ability_id IS NOT NULL THEN 1 ELSE 0 END) AS assigned,
SUM(CASE WHEN EXISTS (SELECT 1 FROM skill_chip_talents talent WHERE talent.skill_chip_set_id=skill_chip_sets.id) THEN 1 ELSE 0 END) AS configured
SUM(CASE WHEN EXISTS (SELECT 1 FROM skill_chip_two_piece_bonuses bonus WHERE bonus.skill_chip_set_id=skill_chip_sets.id) THEN 1 ELSE 0 END) AS configured
FROM skill_chip_sets`)
).get(),
(
@@ -211,7 +225,7 @@ export const load: PageServerLoad = async ({ cookies, url }) => {
module: 'attributes',
action: 'save',
kind: 'stat',
fields: ['unit', 'chipMin', 'chipMax'],
fields: ['unit'],
},
]),
getPendingEntities(user.id, [
@@ -224,8 +238,7 @@ export const load: PageServerLoad = async ({ cookies, url }) => {
'twoPieceStatId',
'twoPieceMin',
'twoPieceMax',
'primaryStatIds',
'secondaryStatIds',
'hasTalent',
'talentName',
'descriptionTemplate',
'conditionalRule',
@@ -252,8 +265,6 @@ export const load: PageServerLoad = async ({ cookies, url }) => {
const [
sets,
twoPiece,
primaryOptions,
secondaryOptions,
talents,
talentEffects,
stats,
@@ -262,6 +273,9 @@ export const load: PageServerLoad = async ({ cookies, url }) => {
abilityChipCounts,
icons,
renders,
primaryProfiles,
secondaryPool,
primaryAttributeIds,
] = await Promise.all([
(
await database.prepare(
@@ -271,10 +285,6 @@ export const load: PageServerLoad = async ({ cookies, url }) => {
(
await database.prepare('SELECT * FROM skill_chip_two_piece_bonuses')
).all(),
(await database.prepare('SELECT * FROM skill_chip_primary_options')).all(),
(
await database.prepare('SELECT * FROM skill_chip_secondary_options')
).all(),
(await database.prepare('SELECT * FROM skill_chip_talents')).all(),
(
await database.prepare(
@@ -283,7 +293,7 @@ export const load: PageServerLoad = async ({ cookies, url }) => {
).all(),
(
await database.prepare(
'SELECT id,name,unit,skill_chip_min,skill_chip_max FROM stat_attributes WHERE active=1 ORDER BY name COLLATE NOCASE',
'SELECT id,name,unit FROM stat_attributes WHERE active=1 ORDER BY name COLLATE NOCASE',
)
).all(),
(
@@ -310,13 +320,14 @@ export const load: PageServerLoad = async ({ cookies, url }) => {
"SELECT source_entity_id,id,updated_at FROM managed_images WHERE source_entity_type='skill_chip_render'",
)
).all(),
getSkillChipPrimaryProfiles(database),
getSkillChipSecondaryPool(database),
getSkillChipPrimaryAttributeIds(database),
]);
const availableStats = [
...pendingStats.map((item) => ({
...item,
unit: String(item.unit ?? ''),
skill_chip_min: item.chipMin === '' ? null : Number(item.chipMin),
skill_chip_max: item.chipMax === '' ? null : Number(item.chipMax),
})),
...stats,
];
@@ -347,9 +358,7 @@ export const load: PageServerLoad = async ({ cookies, url }) => {
const currentPendingSets = pendingSets.filter(
(set) =>
Boolean(set.abilityId) &&
Array.isArray(set.primaryStatIds) &&
Array.isArray(set.secondaryStatIds) &&
Boolean(set.conditionalRule),
(String(set.hasTalent) !== '1' || Boolean(set.conditionalRule)),
);
const pendingSetRows = requestedItemId
? currentPendingSets.filter((set) => set.id === requestedItemId)
@@ -374,19 +383,22 @@ export const load: PageServerLoad = async ({ cookies, url }) => {
min_value: Number(set.twoPieceMin),
max_value: Number(set.twoPieceMax),
},
primaryStatIds: set.primaryStatIds,
secondaryStatIds: set.secondaryStatIds,
talent: {
name: set.talentName,
description_template: set.descriptionTemplate,
conditionalRule: (() => {
try {
return JSON.parse(String(set.conditionalRule ?? 'null'));
} catch {
return null;
}
})(),
},
talent:
String(set.hasTalent) === '1'
? {
name: set.talentName,
description_template: set.descriptionTemplate,
conditionalRule: (() => {
try {
return JSON.parse(
String(set.conditionalRule ?? 'null'),
);
} catch {
return null;
}
})(),
}
: null,
}));
const configuredSets = await Promise.all(
sets.map(async (set) => {
@@ -407,12 +419,6 @@ export const load: PageServerLoad = async ({ cookies, url }) => {
})(),
twoPiece:
twoPiece.find((item) => item.skill_chip_set_id === set.id) ?? null,
primaryStatIds: primaryOptions
.filter((item) => item.skill_chip_set_id === set.id)
.map((item) => String(item.stat_attribute_id)),
secondaryStatIds: secondaryOptions
.filter((item) => item.skill_chip_set_id === set.id)
.map((item) => String(item.stat_attribute_id)),
talent: talent
? {
...talent,
@@ -433,9 +439,15 @@ export const load: PageServerLoad = async ({ cookies, url }) => {
return {
sets: [...pendingSetRows, ...configuredSets],
stats: availableStats,
rollStats: availableStats.filter(
(stat) => stat.skill_chip_min !== null && stat.skill_chip_max !== null,
primaryProfiles: primaryAttributeIds.map(
(statId) =>
primaryProfiles.find((profile) => profile.statId === statId) ?? {
statId,
values: {},
},
),
primaryProfileBase: primaryProfiles,
secondaryPool,
abilities: loadedAbilities,
summary: {
active: Number(summary?.active ?? 0),
@@ -490,12 +502,7 @@ export const actions: Actions = {
const twoPieceStatId = String(form.get('twoPieceStatId') ?? '');
const twoPieceMin = Number(form.get('twoPieceMin'));
const twoPieceMax = Number(form.get('twoPieceMax'));
const primaryStatIds = [
...new Set(form.getAll('primaryStatIds').map(String).filter(Boolean)),
];
const secondaryStatIds = [
...new Set(form.getAll('secondaryStatIds').map(String).filter(Boolean)),
];
const hasTalent = form.get('hasTalent') === '1';
const talentName = String(form.get('talentName') ?? '')
.normalize('NFKC')
.trim();
@@ -503,25 +510,30 @@ export const actions: Actions = {
.normalize('NFKC')
.trim();
const database = await getDatabase();
let conditionalRule;
let conditionalRule: ReturnType<typeof parseConditionalRulePayload> | null =
null;
try {
const conditionalRuleRaw = form.get('conditionalRule');
const activeStats = await (
await database.prepare('SELECT id FROM stat_attributes WHERE active=1')
).all();
conditionalRule = parseConditionalRulePayload(
conditionalRuleRaw,
[
{ key: 'minimum', variantKey: 'minimum' },
{ key: 'maximum', variantKey: 'maximum' },
],
{
allowedAttributeIds: new Set(
activeStats.map((row) => String(row.id)),
),
allowedRecipeKeys: new Set(Object.keys(MECHANIC_RECIPE_REGISTRY)),
},
);
if (hasTalent) {
const conditionalRuleRaw = form.get('conditionalRule');
const activeStats = await (
await database.prepare(
'SELECT id FROM stat_attributes WHERE active=1',
)
).all();
conditionalRule = parseConditionalRulePayload(
conditionalRuleRaw,
[
{ key: 'minimum', variantKey: 'minimum' },
{ key: 'maximum', variantKey: 'maximum' },
],
{
allowedAttributeIds: new Set(
activeStats.map((row) => String(row.id)),
),
allowedRecipeKeys: new Set(Object.keys(MECHANIC_RECIPE_REGISTRY)),
},
);
}
} catch (error) {
return fail(400, {
skillChipError:
@@ -547,38 +559,30 @@ export const actions: Actions = {
return fail(400, {
skillChipError: 'Configure a valid 2-piece stat range.',
});
if (!primaryStatIds.length)
return fail(400, {
skillChipError: 'Configure at least one possible primary stat.',
});
if (secondaryStatIds.length < 2)
return fail(400, {
skillChipError: 'Configure at least two possible secondary stats.',
});
if (primaryStatIds.some((statId) => secondaryStatIds.includes(statId)))
return fail(400, {
skillChipError: 'Primary and secondary stat pools must be distinct.',
});
const outcomes = mechanicOutcomes(conditionalRule);
const numberedOutcomes = descriptionOutcomes(conditionalRule);
const outcomes = conditionalRule ? mechanicOutcomes(conditionalRule) : [];
const numberedOutcomes = conditionalRule
? descriptionOutcomes(conditionalRule)
: [];
const durationSeconds =
conditionalRule.pools.find((pool) => pool.expiresAfterSeconds)
conditionalRule?.pools.find((pool) => pool.expiresAfterSeconds)
?.expiresAfterSeconds ?? null;
const maxStacks = conditionalRule.pools.length
const maxStacks = conditionalRule?.pools.length
? Math.max(...conditionalRule.pools.map((pool) => pool.max ?? 1))
: null;
if (
talentName.length < 2 ||
talentName.length > 80 ||
!descriptionTemplate ||
!conditionalRule.clauses.length ||
!outcomes.length
hasTalent &&
(talentName.length < 2 ||
talentName.length > 80 ||
!descriptionTemplate ||
!conditionalRule?.clauses.length ||
!outcomes.length)
)
return fail(400, {
skillChipError:
'The 3-piece talent requires a name, description, mechanic branch, and outcome.',
});
if (
hasTalent &&
outcomes.some(
(outcome) =>
outcome.variants.maximum.value < outcome.variants.minimum.value,
@@ -592,6 +596,7 @@ export const actions: Actions = {
(match) => Number(match[1]),
);
if (
hasTalent &&
placeholders.some(
(index) =>
!numberedOutcomes.some(
@@ -603,12 +608,16 @@ export const actions: Actions = {
skillChipError:
'Description placeholders must reference configured numbered outcomes.',
});
if (descriptionTemplate.includes('{duration}') && durationSeconds === null)
if (
hasTalent &&
descriptionTemplate.includes('{duration}') &&
durationSeconds === null
)
return fail(400, {
skillChipError:
'The description uses {duration}, but no expiring state pool is defined.',
});
if (descriptionTemplate.includes('{max_stacks}') && !maxStacks)
if (hasTalent && descriptionTemplate.includes('{max_stacks}') && !maxStacks)
return fail(400, {
skillChipError:
'The description uses {max_stacks}, but no state pool maximum is defined.',
@@ -656,11 +665,21 @@ export const actions: Actions = {
).get(id)
: null;
if (
await (
existingTalent &&
!hasTalent &&
form.get('confirmTalentRemoval') !== 'REMOVE'
)
return fail(400, {
skillChipError:
'Confirm removal of the existing 3-piece Talent and mechanic program.',
});
if (
hasTalent &&
(await (
await database.prepare(
'SELECT id FROM skill_chip_talents WHERE name=? COLLATE NOCASE AND id<>?',
)
).get(talentName, existingTalent?.id ?? 'new')
).get(talentName, existingTalent?.id ?? 'new'))
)
return fail(409, {
skillChipError: 'That Skill Chip talent name is already in use.',
@@ -668,14 +687,7 @@ export const actions: Actions = {
const outcomeStatIds = outcomes
.map((effect) => effect.attributeId)
.filter((value): value is string => Boolean(value));
const allStatIds = [
...new Set([
twoPieceStatId,
...primaryStatIds,
...secondaryStatIds,
...outcomeStatIds,
]),
];
const allStatIds = [...new Set([twoPieceStatId, ...outcomeStatIds])];
const validStats = await (
await database.prepare(
`SELECT COUNT(*) AS count FROM stat_attributes WHERE active=1 AND id IN (${allStatIds.map(() => '?').join(',')})`,
@@ -685,17 +697,6 @@ export const actions: Actions = {
return fail(400, {
skillChipError: 'One or more selected stats are unavailable.',
});
const rollStatIds = [...primaryStatIds, ...secondaryStatIds];
const validRollStats = await (
await database.prepare(
`SELECT COUNT(*) AS count FROM stat_attributes WHERE active=1 AND skill_chip_min IS NOT NULL AND skill_chip_max IS NOT NULL AND id IN (${rollStatIds.map(() => '?').join(',')})`,
)
).get(...rollStatIds);
if (Number(validRollStats?.count) !== rollStatIds.length)
return fail(400, {
skillChipError:
'Every primary and secondary option requires a configured Skill Chip range.',
});
const setId =
id || String(form.get('_proposedEntityId') ?? '') || randomUUID();
const talentId = String(existingTalent?.id ?? randomUUID());
@@ -724,61 +725,50 @@ export const actions: Actions = {
await database.prepare(`INSERT INTO skill_chip_two_piece_bonuses(id,skill_chip_set_id,stat_attribute_id,min_value,max_value) VALUES(?,?,?,?,?)
ON CONFLICT(skill_chip_set_id) DO UPDATE SET stat_attribute_id=excluded.stat_attribute_id,min_value=excluded.min_value,max_value=excluded.max_value`)
).run(randomUUID(), setId, twoPieceStatId, twoPieceMin, twoPieceMax);
await (
await database.prepare(
'DELETE FROM skill_chip_primary_options WHERE skill_chip_set_id=?',
)
).run(setId);
for (const statId of primaryStatIds)
if (hasTalent && conditionalRule) {
await (
await database.prepare(
'INSERT INTO skill_chip_primary_options(skill_chip_set_id,stat_attribute_id) VALUES(?,?)',
)
).run(setId, statId);
await (
await database.prepare(
'DELETE FROM skill_chip_secondary_options WHERE skill_chip_set_id=?',
)
).run(setId);
for (const statId of secondaryStatIds)
await (
await database.prepare(
'INSERT INTO skill_chip_secondary_options(skill_chip_set_id,stat_attribute_id) VALUES(?,?)',
)
).run(setId, statId);
await (
await database.prepare(`INSERT INTO skill_chip_talents(id,skill_chip_set_id,name,description_template,created_at,updated_at) VALUES(?,?,?,?,?,?)
await database.prepare(`INSERT INTO skill_chip_talents(id,skill_chip_set_id,name,description_template,created_at,updated_at) VALUES(?,?,?,?,?,?)
ON CONFLICT(skill_chip_set_id) DO UPDATE SET name=excluded.name,description_template=excluded.description_template,updated_at=excluded.updated_at`)
).run(talentId, setId, talentName, descriptionTemplate, now, now);
await (
await database.prepare(
'DELETE FROM skill_chip_talent_effects WHERE talent_id=?',
)
).run(talentId);
let compatibilitySortOrder = 0;
for (const outcome of numberedOutcomes) {
if (outcome.kind !== 'attribute_modifier' || !outcome.attributeId)
continue;
compatibilitySortOrder += 1;
).run(talentId, setId, talentName, descriptionTemplate, now, now);
await (
await database.prepare(
'INSERT INTO skill_chip_talent_effects(id,talent_id,stat_attribute_id,min_value,max_value,sort_order) VALUES(?,?,?,?,?,?)',
'DELETE FROM skill_chip_talent_effects WHERE talent_id=?',
)
).run(
randomUUID(),
).run(talentId);
let compatibilitySortOrder = 0;
for (const outcome of numberedOutcomes) {
if (outcome.kind !== 'attribute_modifier' || !outcome.attributeId)
continue;
compatibilitySortOrder += 1;
await (
await database.prepare(
'INSERT INTO skill_chip_talent_effects(id,talent_id,stat_attribute_id,min_value,max_value,sort_order) VALUES(?,?,?,?,?,?)',
)
).run(
randomUUID(),
talentId,
outcome.attributeId,
outcome.variants.minimum.value,
outcome.variants.maximum.value,
compatibilitySortOrder,
);
}
await saveConditionalRuleInTransaction(
database,
'skill_chip_talent',
talentId,
outcome.attributeId,
outcome.variants.minimum.value,
outcome.variants.maximum.value,
compatibilitySortOrder,
conditionalRule,
);
} else if (existingTalent) {
await (
await database.prepare(
"DELETE FROM conditional_effect_rules WHERE source_type='skill_chip_talent' AND source_id=?",
)
).run(String(existingTalent.id));
await (
await database.prepare('DELETE FROM skill_chip_talents WHERE id=?')
).run(String(existingTalent.id));
}
await saveConditionalRuleInTransaction(
database,
'skill_chip_talent',
talentId,
conditionalRule,
);
await applyIconMutation(
database,
'skill_chip',
@@ -804,6 +794,7 @@ export const actions: Actions = {
entityId: setId,
before,
after: await snapshot(setId),
database,
});
await database.exec('COMMIT');
} catch (error) {
@@ -816,33 +807,163 @@ export const actions: Actions = {
resultToken: randomUUID(),
};
},
savePrimaryProfile: async ({ request, cookies }) => {
const operator = await requirePermission(
await getSessionUser(cookies),
'data.manage',
);
const database = await getDatabase();
const form = await request.formData();
const expectedStatIds = await getSkillChipPrimaryAttributeIds(database);
const normalized = normalizeSkillChipProfiles(
(() => {
try {
return JSON.parse(String(form.get('profiles') ?? ''));
} catch {
return null;
}
})(),
SKILL_CHIP_QUALITIES,
{ expectedStatIds, minimumOptions: 4 },
);
if (normalized.error)
return fail(400, { skillChipPrimaryProfileError: normalized.error });
await database.exec('BEGIN IMMEDIATE');
try {
const before = await getSkillChipPrimaryProfiles(database);
if (JSON.stringify(before) !== String(form.get('baseProfiles') ?? '')) {
await database.exec('ROLLBACK');
if (JSON.stringify(before) === JSON.stringify(normalized.profiles))
return {
skillChipPrimaryProfileSaved: true,
resultToken: randomUUID(),
};
return fail(409, {
skillChipPrimaryProfileError:
'The shared primary profile changed after this editor opened. Review the latest values and try again.',
});
}
await replaceSkillChipPrimaryProfiles(database, normalized.profiles);
await refreshAllFocusReadiness(database);
const after = await getSkillChipPrimaryProfiles(database);
await recordIsacAudit({
actorId: operator.id,
category: 'game_data',
action: 'skill_chip_primary_profile.updated',
entityId: 'skill-chip-primary-profile',
before,
after,
database,
});
await database.exec('COMMIT');
} catch (error) {
await database.exec('ROLLBACK');
throw error;
}
return { skillChipPrimaryProfileSaved: true, resultToken: randomUUID() };
},
saveSecondaryPool: async ({ request, cookies }) => {
const operator = await requirePermission(
await getSessionUser(cookies),
'data.manage',
);
const database = await getDatabase();
const form = await request.formData();
const normalized = normalizeSkillChipProfiles(
(() => {
try {
return JSON.parse(String(form.get('profiles') ?? ''));
} catch {
return null;
}
})(),
SKILL_CHIP_SECONDARY_QUALITIES,
{ minimumOptions: 2 },
);
if (normalized.error)
return fail(400, { skillChipSecondaryPoolError: normalized.error });
const statIds = normalized.profiles.map((profile) => profile.statId);
const validStats = await (
await database.prepare(
`SELECT COUNT(*) AS count FROM stat_attributes WHERE active=1 AND id IN (${statIds.map(() => '?').join(',')})`,
)
).get(...statIds);
if (Number(validStats?.count) !== statIds.length)
return fail(400, {
skillChipSecondaryPoolError:
'One or more selected Attributes are unavailable.',
});
await database.exec('BEGIN IMMEDIATE');
try {
const before = await getSkillChipSecondaryPool(database);
if (JSON.stringify(before) !== String(form.get('baseProfiles') ?? '')) {
await database.exec('ROLLBACK');
if (JSON.stringify(before) === JSON.stringify(normalized.profiles))
return {
skillChipSecondaryPoolSaved: true,
resultToken: randomUUID(),
};
return fail(409, {
skillChipSecondaryPoolError:
'The shared secondary pool changed after this editor opened. Review the latest values and try again.',
});
}
await replaceSkillChipSecondaryPool(database, normalized.profiles);
await refreshAllFocusReadiness(database);
const after = await getSkillChipSecondaryPool(database);
await recordIsacAudit({
actorId: operator.id,
category: 'game_data',
action: 'skill_chip_secondary_pool.updated',
entityId: 'skill-chip-secondary-pool',
before,
after,
database,
});
await database.exec('COMMIT');
} catch (error) {
await database.exec('ROLLBACK');
throw error;
}
return { skillChipSecondaryPoolSaved: true, resultToken: randomUUID() };
},
archive: async ({ request, cookies }) => {
const operator = await requirePermission(
await getSessionUser(cookies),
'data.manage',
);
const id = String((await request.formData()).get('id') ?? '');
const before = await snapshot(id);
if (!before.record)
return fail(404, { skillChipError: 'Skill Chip set not found.' });
const database = await getDatabase();
await (
await database.prepare(
'UPDATE skill_chip_sets SET active=0,updated_at=? WHERE id=?',
)
).run(new Date().toISOString(), id);
await refreshAbilityFocuses(
database,
String(before.record.specialization_ability_id),
);
await recordIsacAudit({
actorId: operator.id,
category: 'game_data',
action: 'skill_chip.archived',
entityId: id,
before,
after: await snapshot(id),
});
await database.exec('BEGIN IMMEDIATE');
try {
const before = await snapshot(id);
if (!before.record) {
await database.exec('ROLLBACK');
return fail(404, { skillChipError: 'Skill Chip set not found.' });
}
await (
await database.prepare(
'UPDATE skill_chip_sets SET active=0,updated_at=? WHERE id=?',
)
).run(new Date().toISOString(), id);
await refreshAbilityFocuses(
database,
String(before.record.specialization_ability_id),
);
await recordIsacAudit({
actorId: operator.id,
category: 'game_data',
action: 'skill_chip.archived',
entityId: id,
before,
after: await snapshot(id),
database,
});
await database.exec('COMMIT');
} catch (error) {
await database.exec('ROLLBACK');
throw error;
}
return { skillChipArchived: true };
},
unarchive: async ({ request, cookies }) => {
@@ -851,50 +972,63 @@ export const actions: Actions = {
'data.manage',
);
const id = String((await request.formData()).get('id') ?? '');
const before = await snapshot(id);
if (!before.record)
return fail(404, { skillChipError: 'Skill Chip set not found.' });
const abilityId = String(before.record.specialization_ability_id);
const database = await getDatabase();
if (
!(await (
await database.prepare(
'SELECT id FROM specialization_abilities WHERE id=? AND active=1',
)
).get(abilityId))
) {
return fail(400, {
skillChipError:
'The assigned specialization ability must be active before restoring this Skill Chip.',
});
}
const assignedCount = Number(
(
await (
await database.prepare(`SELECT COUNT(*) AS count FROM skill_chip_sets
await database.exec('BEGIN IMMEDIATE');
try {
const before = await snapshot(id);
if (!before.record) {
await database.exec('ROLLBACK');
return fail(404, { skillChipError: 'Skill Chip set not found.' });
}
const abilityId = String(before.record.specialization_ability_id);
if (
!(await (
await database.prepare(
'SELECT id FROM specialization_abilities WHERE id=? AND active=1',
)
).get(abilityId))
) {
await database.exec('ROLLBACK');
return fail(400, {
skillChipError:
'The assigned specialization ability must be active before restoring this Skill Chip.',
});
}
const assignedCount = Number(
(
await (
await database.prepare(`SELECT COUNT(*) AS count FROM skill_chip_sets
WHERE specialization_ability_id=? AND active=1 AND id<>?`)
).get(abilityId, id)
)?.count ?? 0,
);
if (assignedCount >= 3)
return fail(409, {
skillChipError:
'That ability already has all three active Skill Chips assigned.',
).get(abilityId, id)
)?.count ?? 0,
);
if (assignedCount >= 3) {
await database.exec('ROLLBACK');
return fail(409, {
skillChipError:
'That ability already has all three active Skill Chips assigned.',
});
}
await (
await database.prepare(
'UPDATE skill_chip_sets SET active=1,updated_at=? WHERE id=?',
)
).run(new Date().toISOString(), id);
await refreshAbilityFocuses(database, abilityId);
await recordIsacAudit({
actorId: operator.id,
category: 'game_data',
action: 'skill_chip.unarchived',
entityId: id,
before,
after: await snapshot(id),
database,
});
await (
await database.prepare(
'UPDATE skill_chip_sets SET active=1,updated_at=? WHERE id=?',
)
).run(new Date().toISOString(), id);
await refreshAbilityFocuses(database, abilityId);
await recordIsacAudit({
actorId: operator.id,
category: 'game_data',
action: 'skill_chip.unarchived',
entityId: id,
before,
after: await snapshot(id),
});
await database.exec('COMMIT');
} catch (error) {
await database.exec('ROLLBACK');
throw error;
}
return { skillChipUnarchived: true };
},
};
+35 -18
View File
@@ -10,6 +10,7 @@
import SearchableAssignment from '$lib/components/SearchableAssignment.svelte';
import SegmentedChoice from '$lib/components/SegmentedChoice.svelte';
import MechanicProgramEditor from '$lib/components/isac/MechanicProgramEditor.svelte';
import SkillChipSharedProfiles from '$lib/components/SkillChipSharedProfiles.svelte';
import type { ConditionalMechanicDraftV1 } from '$lib/calculations/mechanic-editor';
let { data, form } = $props();
@@ -22,7 +23,7 @@
clauses:[{key:'primary',label:'Primary',event:'skill_hit',if:{kind:'true'},reducer:{kind:'none'},mutations:[],outcomes:[{key:'effect-1',label:'Effect 1',phase:'while_active',kind:'attribute_modifier',scope:'assigned_ability',attributeId:'',basis:'flat',perStack:true,descriptionSlot:1,variants:{minimum:{value:0},maximum:{value:0}}}]}]
});
const blankTalent = () => ({ id: '', name: '', description_template: '', conditionalRule: blankRule() });
const blank = () => ({ id: '', name: '', abilityId: '', iconUrl: '', renderUrl: '', active: 1, twoPiece: { stat_attribute_id: '', min_value: '', max_value: '' }, primaryStatIds: [], secondaryStatIds: [], talent: blankTalent() });
const blank = () => ({ id: '', name: '', abilityId: '', iconUrl: '', renderUrl: '', active: 1, twoPiece: { stat_attribute_id: '', min_value: '', max_value: '' }, hasTalent: false, talentOpen: false, talent: blankTalent() });
let editing = $state<any | null>(null);
let highEndCount = $state('0');
let handledResultToken = $state('');
@@ -65,9 +66,9 @@
...item,
abilityId: String(item.specialization_ability_id),
twoPiece: item.twoPiece ? { ...item.twoPiece } : { stat_attribute_id: '', min_value: '', max_value: '' },
primaryStatIds: [...item.primaryStatIds],
secondaryStatIds: [...item.secondaryStatIds],
talent: item.talent ? { ...item.talent, conditionalRule: item.talent.conditionalRule ?? blankRule() } : blankTalent()
hasTalent: Boolean(item.talent),
talentOpen: false,
talent: item.talent ? structuredClone({ ...item.talent, conditionalRule: item.talent.conditionalRule ?? blankRule() }) : blankTalent()
};
highEndCount = '0';
};
@@ -86,17 +87,30 @@
editing = form.skillChipMode === 'add' ? blank() : null;
highEndCount = '0';
}
if (form?.skillChipError && editing?.hasTalent) editing.talentOpen = true;
});
</script>
<svelte:head><title>Skill Chips — SHD.build admin</title></svelte:head>
<div class="module-toolbar"><div><p class="eyebrow">SKILL MODIFICATION SYNERGY REGISTRY</p><h2>SKILL CHIPS</h2><p>Configure Superior and High-end chip rolls, 2-piece bonuses, and quality-scaled 3-piece talents.</p></div><button class="primary" onclick={() => editing = blank()}>ADD SKILL CHIP <span>+</span></button></div>
<div class="module-toolbar"><div><p class="eyebrow">SKILL MODIFICATION SYNERGY REGISTRY</p><h2>SKILL CHIPS</h2><p>Configure shared quality rolls, ability-linked chip sets, 2-piece bonuses, and optional 3-piece talents.</p></div><button class="primary" onclick={() => editing = blank()}>ADD SKILL CHIP <span>+</span></button></div>
<SkillChipSharedProfiles
primaryProfiles={data.primaryProfiles}
primaryProfileBase={data.primaryProfileBase}
secondaryPool={data.secondaryPool}
stats={data.stats}
primaryError={form?.skillChipPrimaryProfileError}
secondaryError={form?.skillChipSecondaryPoolError}
primarySaved={form?.skillChipPrimaryProfileSaved}
secondarySaved={form?.skillChipSecondaryPoolSaved}
resultToken={form?.resultToken}
/>
<KpiFilters active={data.filter} items={[
{value:'',label:'TOTAL SETS',count:data.summary.total,description:'Show all definitions'},
{value:'active',label:'ACTIVE',count:data.summary.active,description:'Available to builder'},
{value:'assigned',label:'ABILITY LINKED',count:data.summary.assigned,description:'Assigned to a class ability'},
{value:'configured',label:'CONFIGURED',count:data.summary.configured,description:'Complete 2-piece and 3-piece effects'},
{value:'configured',label:'CONFIGURED',count:data.summary.configured,description:'Complete 2-piece bonus data'},
{value:'archived',label:'ARCHIVED',count:data.summary.archived,description:'Hidden definitions'}
]}/>
@@ -106,8 +120,9 @@
<div class="editor-title"><div><p class="eyebrow">{editing.id ? 'MODIFY CHIP SET' : 'NEW CHIP SET'}</p><h3>{editing.id ? editing.name : 'SKILL CHIP DEFINITION'}</h3></div><button aria-label="Close editor" onclick={() => editing = null}>×</button></div>
<PendingForm action="?/save" enctype="multipart/form-data">
<input type="hidden" name="id" value={editing.id}/>
<input type="hidden" name="hasTalent" value={editing.hasTalent ? '1' : '0'}/>
<input type="hidden" name="conditionalRule" value={JSON.stringify(editing.talent.conditionalRule)}/>
<div class="skill-chip-identity"><IconUploadField currentUrl={editing.iconUrl} label="SKILL CHIP ICON" resetToken={iconResetToken}/><GameDataRenderUploadField currentUrl={editing.renderUrl} label="SKILL CHIP RENDER" resetToken={renderResetToken}/><label class="field">CHIP SET NAME<input name="name" bind:value={editing.name} maxlength="80" required/></label><div class="quality-scope"><span>SUPPORTED QUALITY</span><div><b class="quality-superior">SUPERIOR</b><b class="quality-highend">HIGH-END</b></div><small>Only these qualities participate in chip scaling.</small></div></div>
<div class="skill-chip-identity"><IconUploadField currentUrl={editing.iconUrl} label="SKILL CHIP ICON" resetToken={iconResetToken}/><GameDataRenderUploadField currentUrl={editing.renderUrl} label="SKILL CHIP RENDER" resetToken={renderResetToken}/><label class="field">CHIP SET NAME<input name="name" bind:value={editing.name} maxlength="80" required/></label><div class="quality-scope"><span>SUPPORTED QUALITY</span><div><b>STANDARD</b><b>SPECIALIZED</b><b class="quality-superior">SUPERIOR</b><b class="quality-highend">HIGH-END</b></div><small>Shared profiles define primary and secondary ranges.</small></div></div>
<section class="skill-chip-ability-assignment">
<div class="selection-head"><div><span>SPECIALIZATION ABILITY</span><small>EXCLUSIVE RELATIONSHIP · THREE ACTIVE SKILL CHIPS PER ABILITY</small></div><b>{editing.abilityId ? `${ability(editing.abilityId)?.chipCount ?? 0} / 3 ASSIGNED` : 'REQUIRED'}</b></div>
<SearchableAssignment items={data.abilities} fieldName="abilityId" mode="single" compact subjectLabel="SPECIALIZATION ABILITY" searchPlaceholder="Search abilities or specializations…" emptySelectionLabel="NO ABILITY ASSIGNED" bind:value={editing.abilityId}/>
@@ -118,15 +133,14 @@
<div class="skill-chip-bonus-grid"><div><SearchableAssignment items={data.stats} fieldName="twoPieceStatId" mode="single" subjectLabel="BONUS ATTRIBUTE" searchPlaceholder="Search attributes…" emptySelectionLabel="NO BONUS ATTRIBUTE SELECTED" bind:value={editing.twoPiece.stat_attribute_id}/></div><div class="skill-chip-range-fields"><label class="field">MINIMUM VALUE<input name="twoPieceMin" type="number" step="any" bind:value={editing.twoPiece.min_value} required/></label><label class="field">MAXIMUM VALUE<input name="twoPieceMax" type="number" step="any" bind:value={editing.twoPiece.max_value} required/></label></div></div>
</section>
<div class="skill-chip-pools">
<div><div class="selection-head"><div><span>PRIMARY STAT POOL</span><small>ONE RANDOM STAT · USES ATTRIBUTE SKILL CHIP RANGE</small></div><b>{editing.primaryStatIds.length}</b></div><SearchableAssignment items={data.rollStats} fieldName="primaryStatIds" subjectLabel="PRIMARY STATS" searchPlaceholder="Search Skill Chip stats…" excludedIds={editing.secondaryStatIds} bind:selected={editing.primaryStatIds}/></div>
<div><div class="selection-head"><div><span>SECONDARY STAT POOL</span><small>TWO DISTINCT RANDOM STATS · USE ATTRIBUTE RANGES</small></div><b>{editing.secondaryStatIds.length}</b></div><SearchableAssignment items={data.rollStats} fieldName="secondaryStatIds" subjectLabel="SECONDARY STATS" searchPlaceholder="Search Skill Chip stats…" excludedIds={editing.primaryStatIds} bind:selected={editing.secondaryStatIds}/></div>
</div>
<section class="skill-chip-talent">
<div class="selection-head"><div><span>3-PIECE TALENT</span><small>QUALITY-SCALED OUTCOMES · TYPED TRIGGER AND DESCRIPTION</small></div><b>{previewOutcomes().length} OUTCOME{previewOutcomes().length === 1 ? '' : 'S'}</b></div>
<div class="selection-head"><div><span>3-PIECE TALENT</span><small>OPTIONAL SET BONUS · QUALITY-SCALED WHEN ASSIGNED</small></div><SegmentedChoice legend="TALENT ASSIGNMENT" name="talent-assignment" options={[{value:'0',label:'NOT ASSIGNED'},{value:'1',label:'ASSIGNED'}]} value={editing.hasTalent ? '1' : '0'} onchange={(values) => { editing.hasTalent = values[0] === '1'; if (editing.hasTalent) editing.talentOpen = true; }}/></div>
{#if editing.hasTalent}
<details class="modeling-disclosure skill-chip-talent-disclosure" bind:open={editing.talentOpen}>
<summary><span><b>{editing.talent.name || 'TALENT WORKSPACE'}</b><small>{previewOutcomes().length} QUALITY-SCALED OUTCOME{previewOutcomes().length === 1 ? '' : 'S'}</small></span><strong>{editing.talentOpen ? 'COLLAPSE' : 'EXPAND'}</strong></summary>
<div class="modeling-content">
<div class="skill-chip-talent-identity"><label class="field">TALENT NAME<input name="talentName" bind:value={editing.talent.name} maxlength="80" required/></label><label class="field">DESCRIPTION TEMPLATE<textarea name="descriptionTemplate" bind:value={editing.talent.description_template} placeholder="While active, gain +{1} Skill Damage…" required></textarea><small>Use {'{1}'}, {'{2}'}, and so on for scaled effect values and units.</small></label></div>
<div class="skill-chip-preview"><SegmentedChoice legend="QUALITY MIX PREVIEW" name="qualityPreview" options={qualitySteps} bind:value={highEndCount}/><div><span>{3 - Number(highEndCount)} SUPERIOR · {highEndCount} HIGH-END</span><p>{talentPreview() || 'Talent description preview'}</p></div></div>
<div class="skill-chip-preview"><SegmentedChoice legend="QUALITY MIX PREVIEW" name="qualityPreview" options={qualitySteps} bind:value={highEndCount}/><div><span>{3 - Number(highEndCount)} NON-HIGH-END · {highEndCount} HIGH-END</span><p>{talentPreview() || 'Talent description preview'}</p></div></div>
<MechanicProgramEditor
attributes={data.stats}
allowAssignedAbility
@@ -139,9 +153,12 @@
}}
bind:rule={editing.talent.conditionalRule}
/>
</div>
</details>
{:else}<p class="console-note">This set has no 3-piece Talent. Its 2-piece bonus remains active when the requirement is met.</p>{#if editing.talent.id}<label class="skill-chip-talent-removal"><input type="checkbox" name="confirmTalentRemoval" value="REMOVE" required/> CONFIRM REMOVAL OF THE EXISTING TALENT AND MECHANIC PROGRAM</label>{/if}{/if}
</section>
{#if form?.skillChipError}<p class="error">{form.skillChipError}</p>{/if}
<div class="form-actions split-actions"><div>{#if editing.id}<button class="danger" formaction={Number(editing.active) ? '?/archive' : '?/unarchive'} name="id" value={editing.id}>{Number(editing.active) ? 'ARCHIVE CHIP SET' : 'UNARCHIVE CHIP SET'}</button>{/if}</div><div><button type="button" class="secondary" onclick={() => editing = null}>CANCEL</button><button class="primary" disabled={!editing.abilityId || !editing.primaryStatIds.length || editing.secondaryStatIds.length < 2 || !data.rollStats.length}>SAVE SKILL CHIP →</button></div></div>
<div class="form-actions split-actions"><div>{#if editing.id}<button class="danger" formaction={Number(editing.active) ? '?/archive' : '?/unarchive'} name="id" value={editing.id}>{Number(editing.active) ? 'ARCHIVE CHIP SET' : 'UNARCHIVE CHIP SET'}</button>{/if}</div><div><button type="button" class="secondary" onclick={() => editing = null}>CANCEL</button><button class="primary" disabled={!editing.abilityId}>SAVE SKILL CHIP →</button></div></div>
</PendingForm>
</div>
{/if}
@@ -150,9 +167,9 @@
<RegistryPagination pagination={data.pagination} search={data.search}/>
<div class="dashboard-table">
<div class="dashboard-row skill-chip-row table-heading"><SortableColumnHeader label="SKILL CHIP" column="name" sort={data.sort}/><SortableColumnHeader label="ABILITY" column="ability" sort={data.sort}/><span>QUALITY</span><SortableColumnHeader label="2-PIECE BONUS" column="twoPiece" sort={data.sort}/><SortableColumnHeader label="3-PIECE TALENT" column="talent" sort={data.sort}/><SortableColumnHeader label="ROLL POOLS" column="rollPools" sort={data.sort}/><SortableColumnHeader label="STATE" column="state" sort={data.sort} defaultDirection="desc"/><span></span></div>
<div class="dashboard-row skill-chip-row table-heading"><SortableColumnHeader label="SKILL CHIP" column="name" sort={data.sort}/><SortableColumnHeader label="ABILITY" column="ability" sort={data.sort}/><span>QUALITY</span><SortableColumnHeader label="2-PIECE BONUS" column="twoPiece" sort={data.sort}/><SortableColumnHeader label="3-PIECE TALENT" column="talent" sort={data.sort}/><span>ROLL PROFILE</span><SortableColumnHeader label="STATE" column="state" sort={data.sort} defaultDirection="desc"/><span></span></div>
{#each data.sets as item}
<div class="dashboard-row skill-chip-row" class:archived={Number(item.active) !== 1} class:pending-record={item.pending} class:registry-row-editing={editing?.id === item.id}><div class="registry-icon-identity"><ItemIcon src={item.iconUrl} alt=""/><div><strong>{item.name}</strong><small>{item.pending ? `PROPOSED BY ${item.proposerName}` : 'SUPERIOR · HIGH-END'}</small></div></div><div class="skill-chip-ability-cell"><strong>{ability(String(item.specialization_ability_id))?.name ?? 'UNKNOWN ABILITY'}</strong><small>EXCLUSIVE ABILITY</small></div><span class="skill-chip-quality-pair"><b class="quality-superior">SUP</b><b class="quality-highend">HE</b></span><span>{item.twoPiece ? `${stat(item.twoPiece.stat_attribute_id)?.name ?? 'UNKNOWN'} · ${format(item.twoPiece.min_value, item.twoPiece.stat_attribute_id)}–${format(item.twoPiece.max_value, item.twoPiece.stat_attribute_id)}` : 'NOT CONFIGURED'}</span><div><strong>{item.talent?.name ?? 'NOT CONFIGURED'}</strong><small>{item.talent?.conditionalRule?.clauses?.length || item.pending ? 'MODELED · QUALITY SCALED' : 'CONFIG REQUIRED'}</small></div><span>{item.primaryStatIds.length} PRIMARY · {item.secondaryStatIds.length} SECONDARY</span><b class:pending={item.pending} class="state-dot">{item.pending ? 'PENDING' : Number(item.active) ? 'ACTIVE' : 'ARCHIVED'}</b>{#if item.pending}<span></span>{:else}<button aria-expanded={editing?.id === item.id} aria-controls={`skill-chip-editor-${item.id}`} onclick={() => edit(item)}>EDIT</button>{/if}</div>
<div class="dashboard-row skill-chip-row" class:archived={Number(item.active) !== 1} class:pending-record={item.pending} class:registry-row-editing={editing?.id === item.id}><div class="registry-icon-identity"><ItemIcon src={item.iconUrl} alt=""/><div><strong>{item.name}</strong><small>{item.pending ? `PROPOSED BY ${item.proposerName}` : 'STANDARD · SPECIALIZED · SUPERIOR · HIGH-END'}</small></div></div><div class="skill-chip-ability-cell"><strong>{ability(String(item.specialization_ability_id))?.name ?? 'UNKNOWN ABILITY'}</strong><small>EXCLUSIVE ABILITY</small></div><span class="skill-chip-quality-pair"><b>STD</b><b>SP</b><b class="quality-superior">SUP</b><b class="quality-highend">HE</b></span><span>{item.twoPiece ? `${stat(item.twoPiece.stat_attribute_id)?.name ?? 'UNKNOWN'} · ${format(item.twoPiece.min_value, item.twoPiece.stat_attribute_id)}–${format(item.twoPiece.max_value, item.twoPiece.stat_attribute_id)}` : 'NOT CONFIGURED'}</span><div><strong>{item.talent?.name ?? 'NOT ASSIGNED'}</strong><small>{item.talent ? item.talent.conditionalRule?.clauses?.length || item.pending ? 'MODELED · QUALITY SCALED' : 'CONFIG REQUIRED' : 'OPTIONAL'}</small></div><span>SHARED · 4 PRIMARY · {data.secondaryPool.length} SECONDARY</span><b class:pending={item.pending} class="state-dot">{item.pending ? 'PENDING' : Number(item.active) ? 'ACTIVE' : 'ARCHIVED'}</b>{#if item.pending}<span></span>{:else}<button aria-expanded={editing?.id === item.id} aria-controls={`skill-chip-editor-${item.id}`} onclick={() => edit(item)}>EDIT</button>{/if}</div>
{#if editing?.id === item.id}<RegistryInlineEditor id={`skill-chip-editor-${item.id}`} label={`Edit ${item.name}`}>{@render skillChipEditor()}</RegistryInlineEditor>{/if}
{:else}<div class="empty-dashboard"><strong>NO SKILL CHIP SETS REGISTERED</strong><span>Add the first Superior and High-end Skill Chip definition.</span></div>{/each}
{:else}<div class="empty-dashboard"><strong>NO SKILL CHIP SETS REGISTERED</strong><span>Add the first ability-linked Skill Chip definition.</span></div>{/each}
</div>
@@ -0,0 +1,256 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
const mocks = vi.hoisted(() => {
const database = {
exec: vi.fn(async () => undefined),
prepare: vi.fn(),
};
return {
database,
getDatabase: vi.fn(async () => database),
getPrimaryAttributeIds: vi.fn(),
getPrimaryProfiles: vi.fn(),
getSecondaryPool: vi.fn(),
recordIsacAudit: vi.fn(),
refreshFocus: vi.fn(),
replacePrimaryProfiles: vi.fn(),
replaceSecondaryPool: vi.fn(),
requirePermission: vi.fn(async (user) => user),
};
});
vi.mock('$lib/server/auth/session', () => ({
getSessionUser: vi.fn(async () => ({ id: 'operator-1' })),
}));
vi.mock('$lib/server/auth/authorization', () => ({
requireDataView: vi.fn(async (user) => user),
requirePermission: mocks.requirePermission,
}));
vi.mock('$lib/server/database', () => ({
getDatabase: mocks.getDatabase,
}));
vi.mock('$lib/server/audit', () => ({
auditRecord: vi.fn(),
recordIsacAudit: mocks.recordIsacAudit,
}));
vi.mock('$lib/server/data-moderation', () => ({
getPendingEntities: vi.fn(async () => []),
}));
vi.mock('$lib/server/skill-chip-profiles', () => ({
getSkillChipPrimaryAttributeIds: mocks.getPrimaryAttributeIds,
getSkillChipPrimaryProfiles: mocks.getPrimaryProfiles,
getSkillChipSecondaryPool: mocks.getSecondaryPool,
replaceSkillChipPrimaryProfiles: mocks.replacePrimaryProfiles,
replaceSkillChipSecondaryPool: mocks.replaceSecondaryPool,
}));
vi.mock('$lib/server/specialization-readiness', () => ({
refreshAbilityFocuses: vi.fn(),
refreshSpecializationFocusReady: mocks.refreshFocus,
}));
vi.mock('$lib/server/managed-images', () => ({
applyGameDataRender: vi.fn(),
ManagedImageValidationError: class extends Error {},
managedImageUrl: vi.fn(() => ''),
processManagedImage: vi.fn(),
readGameDataRenderMutation: vi.fn(),
}));
import { actions } from './+page.server';
const primaryIds = [
'skill-duration',
'skill-radius',
'skill-cooldown-recovery',
'skill-intensity',
];
const primaryQualities = ['Standard', 'Specialized', 'Superior', 'High-end'];
const secondaryQualities = ['Specialized', 'Superior', 'High-end'];
const profiles = (ids: string[], qualities: string[], offset = 0) =>
ids.map((statId, profileIndex) => ({
statId,
values: Object.fromEntries(
qualities.map((quality, qualityIndex) => [
quality,
{
min: offset + profileIndex * 10 + qualityIndex + 1,
max: offset + profileIndex * 10 + qualityIndex + 2,
},
]),
),
}));
const primaryAfter = profiles(primaryIds, primaryQualities, 10);
const secondaryIds = ['critical-hit-chance', 'critical-hit-damage'];
const secondaryAfter = profiles(secondaryIds, secondaryQualities, 20);
const actionRequest = (action: string, payload: unknown, base: unknown) => {
const form = new FormData();
form.set('profiles', JSON.stringify(payload));
form.set('baseProfiles', JSON.stringify(base));
return new Request(`http://localhost/isac/skillchips?/${action}`, {
method: 'POST',
body: form,
});
};
const cases = [
{
actionName: 'savePrimaryProfile' as const,
payload: primaryAfter,
getProfiles: mocks.getPrimaryProfiles,
replaceProfiles: mocks.replacePrimaryProfiles,
before: profiles(primaryIds, primaryQualities),
after: profiles(primaryIds, primaryQualities, 30),
auditAction: 'skill_chip_primary_profile.updated',
entityId: 'skill-chip-primary-profile',
savedKey: 'skillChipPrimaryProfileSaved',
},
{
actionName: 'saveSecondaryPool' as const,
payload: secondaryAfter,
getProfiles: mocks.getSecondaryPool,
replaceProfiles: mocks.replaceSecondaryPool,
before: profiles(secondaryIds, secondaryQualities),
after: profiles(secondaryIds, secondaryQualities, 40),
auditAction: 'skill_chip_secondary_pool.updated',
entityId: 'skill-chip-secondary-pool',
savedKey: 'skillChipSecondaryPoolSaved',
},
];
describe('Skill Chip shared profile actions', () => {
beforeEach(() => {
vi.clearAllMocks();
mocks.getPrimaryAttributeIds.mockResolvedValue(primaryIds);
mocks.database.prepare.mockImplementation(async (sql: string) => ({
all: vi.fn(async () =>
sql.includes('specialization_focuses') ? [{ id: 'focus-1' }] : [],
),
get: vi.fn(async (...statIds: unknown[]) => ({ count: statIds.length })),
run: vi.fn(),
}));
});
for (const testCase of cases) {
it(`${testCase.actionName} audits complete evidence and commits its transaction`, async () => {
testCase.getProfiles
.mockResolvedValueOnce(testCase.before)
.mockResolvedValueOnce(testCase.after);
const result = await actions[testCase.actionName]?.({
cookies: {} as never,
request: actionRequest(
testCase.actionName,
testCase.payload,
testCase.before,
),
} as never);
expect(result).toMatchObject({
[testCase.savedKey]: true,
resultToken: expect.any(String),
});
expect(mocks.requirePermission).toHaveBeenCalledWith(
{ id: 'operator-1' },
'data.manage',
);
expect(testCase.getProfiles).toHaveBeenNthCalledWith(1, mocks.database);
expect(testCase.replaceProfiles).toHaveBeenCalledWith(
mocks.database,
testCase.payload,
);
expect(testCase.getProfiles).toHaveBeenNthCalledWith(2, mocks.database);
expect(mocks.refreshFocus).toHaveBeenCalledWith(
mocks.database,
'focus-1',
);
expect(mocks.recordIsacAudit).toHaveBeenCalledWith({
actorId: 'operator-1',
category: 'game_data',
action: testCase.auditAction,
entityId: testCase.entityId,
before: testCase.before,
after: testCase.after,
database: mocks.database,
});
expect(mocks.database.exec.mock.calls).toEqual([
['BEGIN IMMEDIATE'],
['COMMIT'],
]);
});
it(`${testCase.actionName} rolls back and does not commit when audit fails`, async () => {
const auditError = new Error('audit insert failed');
testCase.getProfiles
.mockResolvedValueOnce(testCase.before)
.mockResolvedValueOnce(testCase.after);
mocks.recordIsacAudit.mockRejectedValueOnce(auditError);
await expect(
actions[testCase.actionName]?.({
cookies: {} as never,
request: actionRequest(
testCase.actionName,
testCase.payload,
testCase.before,
),
} as never),
).rejects.toThrow(auditError);
expect(mocks.database.exec.mock.calls).toEqual([
['BEGIN IMMEDIATE'],
['ROLLBACK'],
]);
expect(mocks.database.exec).not.toHaveBeenCalledWith('COMMIT');
});
it(`${testCase.actionName} rejects stale baseProfiles without replacing or auditing`, async () => {
testCase.getProfiles.mockResolvedValueOnce(testCase.before);
const result = await actions[testCase.actionName]?.({
cookies: {} as never,
request: actionRequest(
testCase.actionName,
testCase.payload,
profiles(['stale-profile'], primaryQualities),
),
} as never);
expect(result).toMatchObject({ status: 409 });
expect(testCase.replaceProfiles).not.toHaveBeenCalled();
expect(mocks.refreshFocus).not.toHaveBeenCalled();
expect(mocks.recordIsacAudit).not.toHaveBeenCalled();
expect(mocks.database.exec.mock.calls).toEqual([
['BEGIN IMMEDIATE'],
['ROLLBACK'],
]);
});
}
it('does not open a database when data.manage permission is denied', async () => {
const permissionError = new Error('Forbidden');
mocks.requirePermission.mockRejectedValueOnce(permissionError);
await expect(
actions.savePrimaryProfile?.({
cookies: {} as never,
request: actionRequest('savePrimaryProfile', primaryAfter, []),
} as never),
).rejects.toThrow(permissionError);
expect(mocks.requirePermission).toHaveBeenCalledWith(
{ id: 'operator-1' },
'data.manage',
);
expect(mocks.getDatabase).not.toHaveBeenCalled();
expect(mocks.database.exec).not.toHaveBeenCalled();
});
});
@@ -0,0 +1,225 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
const mocks = vi.hoisted(() => {
const events: string[] = [];
return {
events,
talentExists: true,
auditRecord: vi.fn(),
conditionalRuleSnapshot: vi.fn(),
gameDataIconMetadata: vi.fn(async () => null),
recordIsacAudit: vi.fn(async () => {
events.push('audit');
}),
database: {
exec: vi.fn(async (sql: string) => {
events.push(sql);
}),
prepare: vi.fn(),
},
};
});
vi.mock('$lib/server/auth/session', () => ({
getSessionUser: vi.fn(async () => ({ id: 'operator-1' })),
}));
vi.mock('$lib/server/auth/authorization', () => ({
requireDataView: vi.fn(async (user) => user),
requirePermission: vi.fn(async (user) => user),
}));
vi.mock('$lib/server/database', () => ({
getDatabase: vi.fn(async () => mocks.database),
}));
vi.mock('$lib/server/audit', () => ({
auditRecord: mocks.auditRecord,
recordIsacAudit: mocks.recordIsacAudit,
}));
vi.mock('$lib/server/data-moderation', () => ({
getPendingEntities: vi.fn(async () => []),
}));
vi.mock('$lib/server/game-data-icons', () => ({
applyIconMutation: vi.fn(),
gameDataIconMetadata: mocks.gameDataIconMetadata,
gameDataIconUrl: vi.fn(() => ''),
GameDataIconValidationError: class extends Error {},
readIconMutation: vi.fn(async () => ({})),
}));
vi.mock('$lib/server/managed-images', () => ({
applyGameDataRender: vi.fn(),
ManagedImageValidationError: class extends Error {},
managedImageUrl: vi.fn(() => ''),
processManagedImage: vi.fn(),
readGameDataRenderMutation: vi.fn(async () => ({})),
}));
vi.mock('$lib/server/specialization-readiness', () => ({
refreshAbilityFocuses: vi.fn(),
refreshSpecializationFocusReady: vi.fn(),
}));
vi.mock('$lib/server/conditional-effects', () => ({
conditionalRuleSnapshot: mocks.conditionalRuleSnapshot,
loadConditionalRuleDraft: vi.fn(),
parseConditionalRulePayload: vi.fn(),
saveConditionalRuleInTransaction: vi.fn(),
}));
import { actions } from './+page.server';
const beforeRecord = {
id: 'set-1',
name: 'Pulse Matrix',
specialization_ability_id: 'ability-1',
active: 1,
};
const afterRecord = { ...beforeRecord, updated_at: '2026-08-02T12:00:00.000Z' };
const existingTalent = {
id: 'talent-1',
skill_chip_set_id: 'set-1',
name: 'Signal Cascade',
description_template: 'Existing Talent',
};
const twoPiece = {
skill_chip_set_id: 'set-1',
stat_attribute_id: 'skill-duration',
min_value: 5,
max_value: 10,
};
const talentEffects = [
{
id: 'effect-1',
talent_id: 'talent-1',
stat_attribute_id: 'skill-intensity',
min_value: 1,
max_value: 2,
sort_order: 1,
},
];
const conditional = { id: 'rule-1', source_id: 'talent-1' };
const saveRequest = (confirmRemoval = false) => {
const form = new FormData();
form.set('id', 'set-1');
form.set('name', 'Pulse Matrix');
form.set('abilityId', 'ability-1');
form.set('twoPieceStatId', 'skill-duration');
form.set('twoPieceMin', '5');
form.set('twoPieceMax', '10');
form.set('hasTalent', '0');
if (confirmRemoval) form.set('confirmTalentRemoval', 'REMOVE');
return new Request('http://localhost/isac/skillchips?/save', {
method: 'POST',
body: form,
});
};
const runSave = (confirmRemoval = false) =>
actions.save?.({
cookies: {} as never,
request: saveRequest(confirmRemoval),
} as never);
describe('Skill Chip Talent removal', () => {
beforeEach(() => {
vi.clearAllMocks();
mocks.events.length = 0;
mocks.talentExists = true;
mocks.auditRecord
.mockResolvedValueOnce(beforeRecord)
.mockResolvedValueOnce(afterRecord);
mocks.conditionalRuleSnapshot.mockResolvedValue(conditional);
mocks.database.prepare.mockImplementation(async (sql: string) => ({
get: vi.fn(async (...args: unknown[]) => {
if (sql.includes('FROM specialization_abilities'))
return { id: 'ability-1' };
if (sql.includes('COUNT(*) AS count FROM skill_chip_sets'))
return { count: 2 };
if (sql.includes('FROM skill_chip_sets WHERE name=')) return null;
if (sql.includes('FROM skill_chip_talents WHERE skill_chip_set_id='))
return mocks.talentExists ? existingTalent : null;
if (sql.includes('COUNT(*) AS count FROM stat_attributes'))
return { count: args.length };
if (sql.includes('FROM managed_images')) return null;
if (sql.includes('FROM skill_chip_two_piece_bonuses')) return twoPiece;
return null;
}),
all: vi.fn(async () => {
if (sql.includes('FROM skill_chip_talent_effects'))
return talentEffects;
return [];
}),
run: vi.fn(async () => {
if (sql.includes('DELETE FROM conditional_effect_rules'))
mocks.events.push('delete conditional rule');
if (sql.includes('DELETE FROM skill_chip_talents')) {
mocks.events.push('delete talent');
mocks.talentExists = false;
}
}),
}));
});
it('requires explicit confirmation before removing an existing Talent', async () => {
const result = await runSave();
expect(result).toMatchObject({
status: 400,
data: {
skillChipError:
'Confirm removal of the existing 3-piece Talent and mechanic program.',
},
});
expect(mocks.database.exec).not.toHaveBeenCalled();
expect(mocks.events).not.toContain('delete conditional rule');
expect(mocks.events).not.toContain('delete talent');
expect(mocks.recordIsacAudit).not.toHaveBeenCalled();
});
it('removes the Talent and mechanic in one audited transaction after confirmation', async () => {
const result = await runSave(true);
expect(result).toMatchObject({
skillChipSaved: true,
skillChipMode: 'edit',
resultToken: expect.any(String),
});
expect(mocks.events).toEqual([
'BEGIN',
'delete conditional rule',
'delete talent',
'audit',
'COMMIT',
]);
expect(mocks.recordIsacAudit).toHaveBeenCalledWith({
actorId: 'operator-1',
category: 'game_data',
action: 'skill_chip.updated',
entityId: 'set-1',
before: {
record: beforeRecord,
icon: null,
render: null,
twoPiece,
talent: existingTalent,
talentEffects,
conditional,
},
after: {
record: afterRecord,
icon: null,
render: null,
twoPiece,
talent: null,
talentEffects: [],
conditional: null,
},
database: mocks.database,
});
});
});