Skip to content

Commit 97eaac7

Browse files
authored
fix: Adjust metadata ID resolution order in Deepnote data handling (#338)
* fix: Adjust metadata ID resolution order in Deepnote data handling Updated the order of ID resolution in the DeepnoteDataConverter, DeepnoteFileChangeWatcher, and Pocket modules to prioritize '__deepnoteBlockId' over 'id'. Added a test to ensure updates are not applied when cells lack block IDs and no fallback is available. * test: Update snapshot reading logic in deepnoteFileChangeWatcher unit tests Replaced the setTimeout with a waitFor function to ensure that readSnapshot is called at least once after the snapshot change event is fired. This improves the reliability of the test by directly waiting for the expected condition.
1 parent 58dde51 commit 97eaac7

4 files changed

Lines changed: 31 additions & 3 deletions

File tree

src/notebooks/deepnote/deepnoteDataConverter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ export class DeepnoteDataConverter {
437437

438438
private createFallbackBlock(cell: NotebookCellData, index: number): DeepnoteBlock {
439439
const meta = cell.metadata as Record<string, unknown> | undefined;
440-
const preservedId = (meta?.id ?? meta?.__deepnoteBlockId ?? meta?.deepnoteBlockId) as string | undefined;
440+
const preservedId = (meta?.__deepnoteBlockId ?? meta?.id ?? meta?.deepnoteBlockId) as string | undefined;
441441
const preservedSortingKey = (meta?.sortingKey ?? meta?.deepnoteSortingKey) as string | undefined;
442442
const preservedBlockGroup = meta?.blockGroup as string | undefined;
443443

src/notebooks/deepnote/deepnoteFileChangeWatcher.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ export class DeepnoteFileChangeWatcher implements IExtensionSyncActivationServic
520520
}
521521

522522
private getBlockIdFromMetadata(metadata: Record<string, unknown> | undefined): string | undefined {
523-
return (metadata?.id ?? metadata?.__deepnoteBlockId) as string | undefined;
523+
return (metadata?.__deepnoteBlockId ?? metadata?.id) as string | undefined;
524524
}
525525

526526
private handleFileChange(uri: Uri): void {

src/notebooks/deepnote/deepnoteFileChangeWatcher.unit.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,34 @@ project:
10521052
execOnDidCreate.dispose();
10531053
});
10541054

1055+
test('should not apply updates when cells have no block IDs and no fallback', async () => {
1056+
const snapshotUri = Uri.file('/workspace/snapshots/my-project_project-1_latest.snapshot.deepnote');
1057+
const notebook = createMockNotebook({
1058+
uri: Uri.file('/workspace/test.deepnote'),
1059+
cells: [
1060+
{
1061+
metadata: {},
1062+
outputs: [],
1063+
kind: NotebookCellKind.Code,
1064+
document: { getText: () => 'print("hello")' }
1065+
}
1066+
]
1067+
});
1068+
1069+
when(mockedVSCodeNamespaces.workspace.notebookDocuments).thenReturn([notebook]);
1070+
1071+
snapshotOnDidChange.fire(snapshotUri);
1072+
1073+
await waitFor(() => readSnapshotCallCount >= 1);
1074+
1075+
assert.isAtLeast(readSnapshotCallCount, 1, 'readSnapshot should be called');
1076+
assert.strictEqual(
1077+
snapshotApplyEditCount,
1078+
0,
1079+
'applyEdit should NOT be called when no block IDs can be resolved'
1080+
);
1081+
});
1082+
10551083
test('should fall back to replaceCells when no kernel is active', async () => {
10561084
const fbDisposables: IDisposableRegistry = [];
10571085
const fbOnDidChange = new EventEmitter<Uri>();

src/platform/deepnote/pocket.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export function createBlockFromPocket(cell: NotebookCellData, index: number): De
7676
const metadata = cell.metadata ? { ...cell.metadata } : undefined;
7777
// Get id from top-level metadata before cleaning it up
7878
// Check both 'id' and backup '__deepnoteBlockId' in case VS Code modifies 'id'
79-
const cellId = (metadata?.id as string | undefined) || (metadata?.__deepnoteBlockId as string | undefined);
79+
const cellId = (metadata?.__deepnoteBlockId as string | undefined) || (metadata?.id as string | undefined);
8080

8181
logger.debug(
8282
`[Pocket] createBlockFromPocket index=${index}: cell.metadata.id=${metadata?.id}, __deepnoteBlockId=${metadata?.__deepnoteBlockId}, using cellId=${cellId}, metadata keys=${

0 commit comments

Comments
 (0)