Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions database/scripts/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const db = get_client({ readonly: false })
const data_folder = path.resolve('database', 'data')

const structure_history_file = path.resolve('shared', 'structure.history.json')
const structure_history: Record<string, string> = JSON.parse(
const structure_history: string[] = JSON.parse(
fs.readFileSync(structure_history_file, 'utf8')
)

Expand Down Expand Up @@ -338,13 +338,13 @@ function seed_structures({ type, folder }: { type: StructureType; folder: string
}

/**
* Adds the structure to the history in case it is new.
* Appends the structure to the history in case it is new.
* The history lists structure ids in the order they were added.
*/
function record_structure_addition(id: string) {
if (structure_history[id]) return
if (structure_history.includes(id)) return

const date = new Date().toLocaleDateString('en-CA')
structure_history[id] = date
structure_history.push(id)
structure_history_changed = true
}

Expand Down
24 changes: 24 additions & 0 deletions database/scripts/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import decided_categories from './expected-data/decided-categories.json'
import decided_functors from './expected-data/decided-functors.json'
import decided_morphisms from './expected-data/decided-morphisms.json'
import decided_symmetric_monoidal_categories from './expected-data/decided-symmetric-monoidal-categories.json'
import structure_history from '$shared/structure.history.json'
import { capitalize, devlog, remove_underscores } from '$shared/utils'
import { get_client } from '$shared/db'
import { STRUCTURE_TYPES, type StructureType, PLURALS } from '$shared/config'
Expand All @@ -33,6 +34,9 @@ function execute_tests() {
devlog('\n--- Test link targets ---')
check_link_targets_exist()

devlog('\n--- Test structure history ---')
test_structure_history_ids_exist()

devlog('\n--- Test categories ---')

test_mutual_structure_duals('category')
Expand Down Expand Up @@ -158,6 +162,26 @@ function test_mutual_property_duals(type: StructureType) {
devlog(`✅ ${capitalize(remove_underscores(type))} properties are mutually dual`)
}

/**
* Tests that every ID in the structure history refers to an existing structure.
* If this test fails, a structure has probably been renamed or deleted.
*/
function test_structure_history_ids_exist() {
const structure_ids = new Set(
db.prepare<never[], string>(`SELECT id FROM structures`).pluck().all()
)

const unknown_ids = structure_history.filter((id) => !structure_ids.has(id))

if (unknown_ids.length > 0) {
throw new Error(
`❌ Found unknown structure IDs in structure.history.json:\n${unknown_ids.join(', ')}`
)
}

devlog('✅ All structure IDs in the history exist')
}

/**
* Tests that for a specified list of structures all properties have
* been decided. If this test fails, property assignments or implications are missing.
Expand Down
Loading
Loading