From 3629e89e87d02f5f000dac837f36fb1b99759b72 Mon Sep 17 00:00:00 2001 From: Gulum Date: Fri, 5 Jun 2026 23:47:11 +0200 Subject: [PATCH] GEN-1: expose CATALOG seed view (Name/CanonicalGenotype/SortOrder) for DATA-2 + round-trip test Co-Authored-By: Claude Opus 4.8 (1M context) --- .../src/genetics/__tests__/genetics.test.ts | 25 +++++++++++- gerbil-manager-web/src/genetics/catalog.ts | 40 ++++++++++++++++++- gerbil-manager-web/src/genetics/index.ts | 4 +- 3 files changed, 65 insertions(+), 4 deletions(-) diff --git a/gerbil-manager-web/src/genetics/__tests__/genetics.test.ts b/gerbil-manager-web/src/genetics/__tests__/genetics.test.ts index 12710ce..7f761ea 100644 --- a/gerbil-manager-web/src/genetics/__tests__/genetics.test.ts +++ b/gerbil-manager-web/src/genetics/__tests__/genetics.test.ts @@ -17,7 +17,14 @@ import { wildType, } from '../genotype' import { combineLocus } from '../punnett' -import { farbschlagFor, genotypeToFarbschlag, CATALOG_SIZE } from '../catalog' +import { + farbschlagFor, + genotypeToFarbschlag, + representativeGenotype, + BASE_COLORS, + CATALOG, + CATALOG_SIZE, +} from '../catalog' import { breed } from '../breed' import { GeneticsWarningCode } from '../warnings' @@ -170,6 +177,22 @@ describe('Farbschlag catalog', () => { expect(CATALOG_SIZE).toBe(18) }) + it('every catalog entry round-trips: its representative genotype maps back to its own name', () => { + // Guards the DB seed (DATA-2): a representative genotype that resolved to a + // DIFFERENT (earlier) variety would mean overlapping/mis-ordered patterns. + for (const entry of BASE_COLORS) { + expect(genotypeToFarbschlag(representativeGenotype(entry))).toBe(entry.name) + } + }) + + it('CATALOG seed view mirrors the ColorVariety table shape', () => { + expect(CATALOG).toHaveLength(CATALOG_SIZE) + expect(CATALOG[0]).toMatchObject({ name: 'Pink Eyed White (PEW)', sortOrder: 0 }) + // Every row has a non-empty canonical genotype display string and unique name. + expect(new Set(CATALOG.map((c) => c.name)).size).toBe(CATALOG.length) + expect(CATALOG.every((c) => /^[A-Za-z?]+( [A-Za-z?]+){7}$/.test(c.canonicalGenotype))).toBe(true) + }) + it('genotypeToFarbschlag (DATA-1 denormalization contract) returns the plain name', () => { expect(genotypeToFarbschlag(wildType())).toBe('Agouti') expect(genotypeToFarbschlag(fromDisplayString('aa CC DD EE GG pp spsp rere'))).toBe('Platin') diff --git a/gerbil-manager-web/src/genetics/catalog.ts b/gerbil-manager-web/src/genetics/catalog.ts index f7f5e45..1865091 100644 --- a/gerbil-manager-web/src/genetics/catalog.ts +++ b/gerbil-manager-web/src/genetics/catalog.ts @@ -14,8 +14,8 @@ * - rennmaus-info.jimdoweb.com (loci & colourpoint series) * - clan-of-topolino.ch, rennmauswelten.jimdofree.com (Zobel, Schimmel) */ -import type { LocusKey } from './loci' -import type { Genotype } from './genotype' +import { LOCUS_ORDER, type LocusKey } from './loci' +import { makeGenotype, toDisplayString, wildType, type AllelePair, type Genotype } from './genotype' import { phenotypeTokens, type PhenotypeTokens } from './phenotype' export interface FarbschlagEntry { @@ -105,5 +105,41 @@ export function genotypeToFarbschlag(g: Genotype): string { return farbschlagFor(g).name } +/** + * A representative full genotype for a catalog entry: each specified locus is + * homozygous for its token allele; unspecified loci take the wild-type allele. + * This is the entry's CanonicalGenotype for DB seeding. + */ +export function representativeGenotype(entry: FarbschlagEntry): Genotype { + const base = wildType() + const out = {} as Record + for (const locus of LOCUS_ORDER) { + const token = entry.tokens[locus] + out[locus] = token ? [token, token] : base[locus] + } + return makeGenotype(out) +} + +/** + * DB seed view for DATA-2's ColorVariety table. Each row mirrors the table + * shape (Name, CanonicalGenotype, SortOrder). SortOrder = catalog position. + * + * IMPORTANT: `name` values become DB keys the UI filters on — renames are + * BREAKING changes and must be routed through god. Adding new varieties is safe. + */ +export interface ColorVarietySeed { + readonly name: string + readonly english?: string + readonly canonicalGenotype: string + readonly sortOrder: number +} + +export const CATALOG: readonly ColorVarietySeed[] = BASE_COLORS.map((entry, i) => ({ + name: entry.name, + english: entry.english, + canonicalGenotype: toDisplayString(representativeGenotype(entry)), + sortOrder: i, +})) + /** Number of base-colour varieties currently catalogued. */ export const CATALOG_SIZE = BASE_COLORS.length diff --git a/gerbil-manager-web/src/genetics/index.ts b/gerbil-manager-web/src/genetics/index.ts index 2648365..97801e0 100644 --- a/gerbil-manager-web/src/genetics/index.ts +++ b/gerbil-manager-web/src/genetics/index.ts @@ -33,11 +33,13 @@ export type { PhenotypeTokens } from './phenotype' export { farbschlagFor, genotypeToFarbschlag, + representativeGenotype, BASE_COLORS, + CATALOG, CATALOG_SIZE, UNKNOWN_FARBSCHLAG, } from './catalog' -export type { FarbschlagEntry, FarbschlagMatch } from './catalog' +export type { FarbschlagEntry, FarbschlagMatch, ColorVarietySeed } from './catalog' export { GeneticsWarningCode } from './warnings' export type { GeneticsWarning } from './warnings'