diff --git a/packages/core/src/asset/asset.test.ts b/packages/core/src/asset/asset.test.ts index 8a5738c9..840c3d15 100644 --- a/packages/core/src/asset/asset.test.ts +++ b/packages/core/src/asset/asset.test.ts @@ -1152,6 +1152,134 @@ describe('AssetService', () => { expect(updatedA?.sortIndex && updatedA.sortIndex > 'a1').toBe(true) }) + it('verifies that sortIndex uses C collation to prevent fractional indexing mismatch', async () => { + const { user, project } = await setupBasicAssets() + + // Create a parent folder to contain our assets + const folder = await prisma.asset.create({ + data: { + name: 'Parent Folder', + type: AssetType.folder, + projectId: project.id, + creatorId: user.id, + status: 'uploaded', + }, + }) + + // Create Asset A: In ASCII, "YjnsD0vZ" > "YjNuyGpV" because "n" (110) > "N" (78). + // In default Postgres locale collation, "YjnsD0vZ" < "YjNuyGpV". + const assetA = await prisma.asset.create({ + data: { + name: 'Asset A', + type: AssetType.file, + parentId: folder.id, + projectId: project.id, + creatorId: user.id, + sortIndex: 'YjnsD0vZ', + status: 'uploaded', + }, + }) + + // Create Asset B + const assetB = await prisma.asset.create({ + data: { + name: 'Asset B', + type: AssetType.file, + parentId: folder.id, + projectId: project.id, + creatorId: user.id, + sortIndex: 'YjNuyGpV', + status: 'uploaded', + }, + }) + + // Create Asset C + const assetC = await prisma.asset.create({ + data: { + name: 'Asset C', + type: AssetType.file, + parentId: folder.id, + projectId: project.id, + creatorId: user.id, + sortIndex: 'Yjzaaa', + status: 'uploaded', + }, + }) + + // Verify ASCII ordering in JavaScript: assetA.sortIndex > assetB.sortIndex + expect(assetA.sortIndex! > assetB.sortIndex!).toBe(true) + + // Verify Postgres sorting behavior: + // With C collation, it must follow ASCII ordering. Therefore, searching for + // sortIndex < assetB.sortIndex must NOT find assetA. + const prevAsset = await prisma.asset.findFirst({ + where: { + parentId: folder.id, + sortIndex: { lt: assetB.sortIndex! }, + id: { not: assetC.id }, + }, + orderBy: { sortIndex: 'desc' }, + }) + + // Assert that the database collation is correctly "C" (returns null for prevAsset) + expect(prevAsset).toBeNull() + + // Assert that updateAssetOrder succeeds without throwing a collation error + const updatedC = await assetService.updateAssetOrder(assetC.id, { + beforeIndex: assetB.sortIndex!, + }) + expect(updatedC.sortIndex).toBeDefined() + expect(updatedC.sortIndex! < assetB.sortIndex!).toBe(true) + }) + + it('correctly bounds the new sortIndex when moving an asset after another asset in the middle of the list', async () => { + const { user, project } = await setupBasicAssets() + + // Create three assets: A, B, C with specific sort indexes + const assetA = await prisma.asset.create({ + data: { + name: 'Asset A', + type: AssetType.file, + projectId: project.id, + creatorId: user.id, + sortIndex: 'a0', + status: 'uploaded', + }, + }) + + const assetB = await prisma.asset.create({ + data: { + name: 'Asset B', + type: AssetType.file, + projectId: project.id, + creatorId: user.id, + sortIndex: 'a1', + status: 'uploaded', + }, + }) + + const assetC = await prisma.asset.create({ + data: { + name: 'Asset C', + type: AssetType.file, + projectId: project.id, + creatorId: user.id, + sortIndex: 'a11', + status: 'uploaded', + }, + }) + + // Move Asset A to be after Asset B (it should be placed between B ("a1") and C ("a11")) + const updatedA = await assetService.updateAssetOrder(assetA.id, { + afterIndex: assetB.sortIndex!, + }) + + // Verify it is placed after B + expect(updatedA.sortIndex! > assetB.sortIndex!).toBe(true) + // Verify it is placed before C (this fails under the buggy code because C's bound is ignored, generating a key greater than "a11") + expect(updatedA.sortIndex! < assetC.sortIndex!).toBe(true) + }) + describe('Symlinks', () => { it('toAssetInfo resolves symlink target metadata', async () => { const { project, user } = await setupBasicAssets() @@ -1266,6 +1394,7 @@ describe('AssetService', () => { creatorId: user.id, status: 'uploaded', sizeByte: 100, + sortIndex: 'abc', }, }) @@ -1297,6 +1426,7 @@ describe('AssetService', () => { expect(res.data).toHaveLength(1) const stackAsset = res.data[0] expect(stackAsset.type).toBe(AssetType.version_stack) + expect(stackAsset.sortIndex).toBe('abc') expect(stackAsset.versionStack).toBeDefined() expect(stackAsset.versionStack?.versions).toHaveLength(2) const versions = stackAsset.versionStack!.versions diff --git a/packages/core/src/asset/asset.ts b/packages/core/src/asset/asset.ts index 0baf168a..afae531f 100644 --- a/packages/core/src/asset/asset.ts +++ b/packages/core/src/asset/asset.ts @@ -533,6 +533,7 @@ export class AssetService { parentId: stackParentId, creatorId: creatorId, name: '', + sortIndex: destFile.sortIndex, }, }) @@ -973,7 +974,15 @@ export class AssetService { afterSortIndex = prevAsset?.sortIndex || null } else if (req.afterIndex) { afterSortIndex = req.afterIndex - beforeSortIndex = null + const nextAsset = await this.prismaClient.asset.findFirst({ + where: { + parentId: asset.parentId, + sortIndex: { gt: req.afterIndex }, + id: { not: id }, + }, + orderBy: { sortIndex: 'asc' }, + }) + beforeSortIndex = nextAsset?.sortIndex || null } const newSortIndex = generateKeyBetween(afterSortIndex, beforeSortIndex) diff --git a/packages/core/src/versionStack/versionStack.test.ts b/packages/core/src/versionStack/versionStack.test.ts index 9a74b833..ec748b3f 100644 --- a/packages/core/src/versionStack/versionStack.test.ts +++ b/packages/core/src/versionStack/versionStack.test.ts @@ -56,6 +56,7 @@ describe('VersionStackService', () => { creatorId: user.id, status: 'uploaded', sizeByte: 100, + sortIndex: 'abc', }, }) @@ -85,6 +86,7 @@ describe('VersionStackService', () => { expect(stack.fileCount).toBe(2) expect(stack.sizeByte).toBe(300) expect(stack.parentId).toBe(parent.id) + expect(stack.sortIndex).toBe('abc') // Verify Parent file count const updatedParent = await prisma.asset.findUnique({ where: { id: parent.id } }) diff --git a/packages/core/src/versionStack/versionStack.ts b/packages/core/src/versionStack/versionStack.ts index e8d1b3f0..ab5fb746 100644 --- a/packages/core/src/versionStack/versionStack.ts +++ b/packages/core/src/versionStack/versionStack.ts @@ -58,6 +58,9 @@ export class VersionStackService { throw new Error('parent not found') } + const firstFile = files[0] + const sortIndex = firstFile?.sortIndex || null + const stack = await tx.asset.create({ data: { type: AssetType.version_stack, @@ -67,6 +70,7 @@ export class VersionStackService { projectId: projectId, creatorId: creatorId, parentId: parentId, + sortIndex: sortIndex, }, }) diff --git a/packages/db/prisma/migrations/20260615115103_add_c_collation_to_sort_index/migration.sql b/packages/db/prisma/migrations/20260615115103_add_c_collation_to_sort_index/migration.sql new file mode 100644 index 00000000..38c3aaa5 --- /dev/null +++ b/packages/db/prisma/migrations/20260615115103_add_c_collation_to_sort_index/migration.sql @@ -0,0 +1,6 @@ +-- AlterTable: Change sort_index column collation to "C" +ALTER TABLE "assets" ALTER COLUMN "sort_index" TYPE TEXT COLLATE "C"; + +-- Recreate index on sort_index with "C" collation +DROP INDEX IF EXISTS "assets_sort_index_idx"; +CREATE INDEX "assets_sort_index_idx" ON "assets" ("sort_index" COLLATE "C"); \ No newline at end of file diff --git a/packages/webui/components/file-browser/file-browser.tsx b/packages/webui/components/file-browser/file-browser.tsx index e174d3c1..1cd12260 100644 --- a/packages/webui/components/file-browser/file-browser.tsx +++ b/packages/webui/components/file-browser/file-browser.tsx @@ -853,6 +853,7 @@ export function FileBrowser({