All files / lib/schemas sessions.js

72.72% Statements 8/11
0% Branches 0/2
75% Functions 3/4
72.72% Lines 8/11

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              14x             14x                                               14x           14x         14x                             36x                 36x                 36x    
import { scope, type } from 'arktype';
 
// schemas are imported by scripts that are run by Bun directly, so dont use $lib here
import { keys } from '../utils.js';
import { ID } from './common.js';
import { MetadataValues } from './metadata.js';
 
export const SORT_FIELDS = /** @type {const} */ ({
	metadataValue: { label: 'Métadonnée…', needsMetadata: true },
	metadataConfidence: { label: 'Confiance en…', needsMetadata: true },
	id: { label: 'ID', needsMetadata: false },
	name: { label: 'Nom', needsMetadata: false }
});
 
export const GROUP_FIELDS = /** @type {const} */ ({
	metadataValue: { label: 'Métadonnée…', needsMetadata: true },
	metadataPresence: { label: 'Présence de…', needsMetadata: true },
	metadataConfidence: { label: 'Confiance en…', needsMetadata: true },
	none: { label: 'Aucun regroupement', needsMetadata: false }
});
 
/**
 *
 * @param {"sort" | "group"} task
 * @param {keyof ( typeof SORT_FIELDS & typeof GROUP_FIELDS)} field
 * @returns
 */
export function sortOrGroupFieldNeedsMetadata(task, field) {
	switch (task) {
		// @ts-expect-error
		case 'sort':
			return SORT_FIELDS[field].needsMetadata;
		// @ts-expect-error
		case 'group':
			return GROUP_FIELDS[field].needsMetadata;
	}
}
 
export const SortSettings = type({
	field: type.enumerated(...keys(SORT_FIELDS)),
	'metadata?': ID,
	direction: type.enumerated('asc', 'desc')
});
 
export const GroupSettings = type({
	field: type.enumerated(...keys(GROUP_FIELDS)),
	'metadata?': ID
});
 
export const Session = type({
	id: ID,
	name: 'string',
	// Date is not compatible with JSON Schemas, use a datestring instead
	createdAt: 'string.date.iso',
	openedAt: 'string.date.iso',
	description: 'string',
	protocol: ID,
	metadata: MetadataValues,
	sort: type({
		global: SortSettings,
		// Per-tab
		'import?': SortSettings,
		'crop?': SortSettings,
		'classify?': SortSettings
	}).default(() => ({
		global: { field: 'id', direction: 'asc' }
	})),
	group: type({
		global: GroupSettings,
		// Per-tab
		'import?': GroupSettings,
		'crop?': GroupSettings,
		'classify?': GroupSettings
	}).default(() => ({
		global: { field: 'none' }
	})),
	inferenceModels: scope({ ID })
		.type({
			// -1 is for none selected
			'[ID]': 'number.integer >= -1'
		})
		.describe('Maps metadata IDs to selected model indices')
		.default(() => ({}))
});