mirror of
https://github.com/simstudioai/sim.git
synced 2026-09-24 15:45:35 +08:00
fix(folders): stop archived rows skewing sortOrder, hide them from admin, and cover the two untested modules (#6062)
* fix(folders): stop archived rows skewing sortOrder, hide them from admin, and cover the two untested modules nextFolderSortOrder returned min - 1 over ALL rows including soft-deleted ones, so every delete ratcheted the floor further negative and never recovered — an archived folder at -400 forced the next new folder to -401 forever. Both minima (folders and child resources) now see only rows a user can still see, which is how the Files path has always worked. The admin workspace-folders endpoint counted and paginated soft-deleted folders, so an operator saw phantom folders and an inflated total, disagreeing with every user-facing list. Adds naming.test.ts and queries.test.ts. Both modules had zero tests and are mocked at every call site, so their bodies executed in no test anywhere. That left unasserted the two bug classes that caused real defects in the folder migration: the suffix sequence (must start at (1) and skip taken suffixes) and resourceType scoping on the id-keyed lookups, where a missing clause silently files a knowledge base under a table folder. Every assertion is mutation-checked. The first version of the sortOrder test was vacuous — for a root folder the parent condition is itself an isNull node, so a presence-only check passed with the soft-delete filter deleted; it now asserts the specific column. * refactor(testing): share the drizzle condition-tree helpers Asserting on WHERE clauses is the only way to pin a filter the row-queue mocks cannot enforce — a mock returns whatever was queued regardless of the predicate — so this pattern spreads to every test that guards a query's scoping. It had reached five local copies of the same flatten/has pair, four of them added by the tests in this branch. Moved to @sim/testing beside createMockSqlOperators, whose output shape they parse, so the helper and the node types it depends on live together.
This commit is contained in:
@@ -421,3 +421,33 @@ export const drizzleOrmMock = {
|
||||
getTableColumns: vi.fn((table: Record<string, unknown>) => ({ ...table })),
|
||||
...createMockSqlOperators(),
|
||||
}
|
||||
|
||||
/**
|
||||
* Condition nodes produced by `createMockSqlOperators` — `{ type: 'eq', left, right }`,
|
||||
* `{ type: 'isNull', column }`, and so on.
|
||||
*/
|
||||
export type MockCondition = Record<string, unknown>
|
||||
|
||||
/**
|
||||
* Flattens the nested `and(...)` trees `createMockSqlOperators` builds into a flat node list.
|
||||
*
|
||||
* Tests assert on WHERE clauses to pin filters the row-queue mocks cannot enforce — a mock
|
||||
* returns whatever was queued regardless of the predicate, so "the query filters on X" is only
|
||||
* testable by inspecting the condition tree. `and()` nests arbitrarily, hence the flatten.
|
||||
*/
|
||||
export function flattenMockConditions(condition: unknown): MockCondition[] {
|
||||
if (!condition || typeof condition !== 'object') return []
|
||||
const node = condition as MockCondition
|
||||
if (node.type === 'and' && Array.isArray(node.conditions)) {
|
||||
return node.conditions.flatMap(flattenMockConditions)
|
||||
}
|
||||
return [node]
|
||||
}
|
||||
|
||||
/** True when any node in `condition` satisfies `predicate`. */
|
||||
export function hasMockCondition(
|
||||
condition: unknown,
|
||||
predicate: (node: MockCondition) => boolean
|
||||
): boolean {
|
||||
return flattenMockConditions(condition).some(predicate)
|
||||
}
|
||||
|
||||
@@ -45,6 +45,9 @@ export {
|
||||
dbChainMock,
|
||||
dbChainMockFns,
|
||||
drizzleOrmMock,
|
||||
flattenMockConditions,
|
||||
hasMockCondition,
|
||||
type MockCondition,
|
||||
queueTableRows,
|
||||
resetDbChainMock,
|
||||
} from './database.mock'
|
||||
|
||||
Reference in New Issue
Block a user