|
1 | 1 | import { assert } from 'chai'; |
| 2 | +import * as yaml from 'js-yaml'; |
2 | 3 |
|
3 | 4 | import { DeepnoteNotebookSerializer } from './deepnoteSerializer'; |
4 | 5 | import { DeepnoteNotebookManager } from './deepnoteNotebookManager'; |
5 | 6 | import { DeepnoteDataConverter } from './deepnoteDataConverter'; |
6 | | -import type { DeepnoteProject } from '../../platform/deepnote/deepnoteTypes'; |
| 7 | +import type { DeepnoteFile, DeepnoteProject } from '../../platform/deepnote/deepnoteTypes'; |
7 | 8 |
|
8 | 9 | suite('DeepnoteNotebookSerializer', () => { |
9 | 10 | let serializer: DeepnoteNotebookSerializer; |
@@ -59,6 +60,14 @@ suite('DeepnoteNotebookSerializer', () => { |
59 | 60 | serializer = new DeepnoteNotebookSerializer(manager); |
60 | 61 | }); |
61 | 62 |
|
| 63 | + /** |
| 64 | + * Helper function to convert a DeepnoteProject object with version to YAML format |
| 65 | + */ |
| 66 | + function projectToYaml(projectData: DeepnoteFile): Uint8Array { |
| 67 | + const yamlString = yaml.dump(projectData); |
| 68 | + return new TextEncoder().encode(yamlString); |
| 69 | + } |
| 70 | + |
62 | 71 | suite('deserializeNotebook', () => { |
63 | 72 | test('should deserialize valid project with selected notebook', async () => { |
64 | 73 | // Set up the manager to select the first notebook |
@@ -313,4 +322,231 @@ project: |
313 | 322 | assert.instanceOf(testManager, DeepnoteNotebookManager); |
314 | 323 | }); |
315 | 324 | }); |
| 325 | + |
| 326 | + suite('default notebook selection', () => { |
| 327 | + test('should not select Init notebook when other notebooks are available', async () => { |
| 328 | + const projectData: DeepnoteFile = { |
| 329 | + version: '1.0', |
| 330 | + metadata: { |
| 331 | + createdAt: '2023-01-01T00:00:00Z', |
| 332 | + modifiedAt: '2023-01-02T00:00:00Z' |
| 333 | + }, |
| 334 | + project: { |
| 335 | + id: 'project-with-init', |
| 336 | + name: 'Project with Init', |
| 337 | + initNotebookId: 'init-notebook', |
| 338 | + notebooks: [ |
| 339 | + { |
| 340 | + id: 'init-notebook', |
| 341 | + name: 'Init', |
| 342 | + blocks: [ |
| 343 | + { |
| 344 | + id: 'block-init', |
| 345 | + content: 'print("init")', |
| 346 | + sortingKey: 'a0', |
| 347 | + type: 'code' |
| 348 | + } |
| 349 | + ], |
| 350 | + executionMode: 'block', |
| 351 | + isModule: false |
| 352 | + }, |
| 353 | + { |
| 354 | + id: 'main-notebook', |
| 355 | + name: 'Main', |
| 356 | + blocks: [ |
| 357 | + { |
| 358 | + id: 'block-main', |
| 359 | + content: 'print("main")', |
| 360 | + sortingKey: 'a0', |
| 361 | + type: 'code' |
| 362 | + } |
| 363 | + ], |
| 364 | + executionMode: 'block', |
| 365 | + isModule: false |
| 366 | + } |
| 367 | + ], |
| 368 | + settings: {} |
| 369 | + } |
| 370 | + }; |
| 371 | + |
| 372 | + const content = projectToYaml(projectData); |
| 373 | + const result = await serializer.deserializeNotebook(content, {} as any); |
| 374 | + |
| 375 | + // Should select the Main notebook, not the Init notebook |
| 376 | + assert.strictEqual(result.metadata?.deepnoteNotebookId, 'main-notebook'); |
| 377 | + assert.strictEqual(result.metadata?.deepnoteNotebookName, 'Main'); |
| 378 | + }); |
| 379 | + |
| 380 | + test('should select Init notebook when it is the only notebook', async () => { |
| 381 | + const projectData: DeepnoteFile = { |
| 382 | + version: '1.0', |
| 383 | + metadata: { |
| 384 | + createdAt: '2023-01-01T00:00:00Z', |
| 385 | + modifiedAt: '2023-01-02T00:00:00Z' |
| 386 | + }, |
| 387 | + project: { |
| 388 | + id: 'project-only-init', |
| 389 | + name: 'Project with only Init', |
| 390 | + initNotebookId: 'init-notebook', |
| 391 | + notebooks: [ |
| 392 | + { |
| 393 | + id: 'init-notebook', |
| 394 | + name: 'Init', |
| 395 | + blocks: [ |
| 396 | + { |
| 397 | + id: 'block-init', |
| 398 | + content: 'print("init")', |
| 399 | + sortingKey: 'a0', |
| 400 | + type: 'code' |
| 401 | + } |
| 402 | + ], |
| 403 | + executionMode: 'block', |
| 404 | + isModule: false |
| 405 | + } |
| 406 | + ], |
| 407 | + settings: {} |
| 408 | + } |
| 409 | + }; |
| 410 | + |
| 411 | + const content = projectToYaml(projectData); |
| 412 | + const result = await serializer.deserializeNotebook(content, {} as any); |
| 413 | + |
| 414 | + // Should select the Init notebook since it's the only one |
| 415 | + assert.strictEqual(result.metadata?.deepnoteNotebookId, 'init-notebook'); |
| 416 | + assert.strictEqual(result.metadata?.deepnoteNotebookName, 'Init'); |
| 417 | + }); |
| 418 | + |
| 419 | + test('should select alphabetically first notebook when no initNotebookId', async () => { |
| 420 | + const projectData: DeepnoteFile = { |
| 421 | + version: '1.0', |
| 422 | + metadata: { |
| 423 | + createdAt: '2023-01-01T00:00:00Z', |
| 424 | + modifiedAt: '2023-01-02T00:00:00Z' |
| 425 | + }, |
| 426 | + project: { |
| 427 | + id: 'project-alphabetical', |
| 428 | + name: 'Project Alphabetical', |
| 429 | + notebooks: [ |
| 430 | + { |
| 431 | + id: 'zebra-notebook', |
| 432 | + name: 'Zebra Notebook', |
| 433 | + blocks: [ |
| 434 | + { |
| 435 | + id: 'block-z', |
| 436 | + content: 'print("zebra")', |
| 437 | + sortingKey: 'a0', |
| 438 | + type: 'code' |
| 439 | + } |
| 440 | + ], |
| 441 | + executionMode: 'block', |
| 442 | + isModule: false |
| 443 | + }, |
| 444 | + { |
| 445 | + id: 'alpha-notebook', |
| 446 | + name: 'Alpha Notebook', |
| 447 | + blocks: [ |
| 448 | + { |
| 449 | + id: 'block-a', |
| 450 | + content: 'print("alpha")', |
| 451 | + sortingKey: 'a0', |
| 452 | + type: 'code' |
| 453 | + } |
| 454 | + ], |
| 455 | + executionMode: 'block', |
| 456 | + isModule: false |
| 457 | + }, |
| 458 | + { |
| 459 | + id: 'bravo-notebook', |
| 460 | + name: 'Bravo Notebook', |
| 461 | + blocks: [ |
| 462 | + { |
| 463 | + id: 'block-b', |
| 464 | + content: 'print("bravo")', |
| 465 | + sortingKey: 'a0', |
| 466 | + type: 'code' |
| 467 | + } |
| 468 | + ], |
| 469 | + executionMode: 'block', |
| 470 | + isModule: false |
| 471 | + } |
| 472 | + ], |
| 473 | + settings: {} |
| 474 | + } |
| 475 | + }; |
| 476 | + |
| 477 | + const content = projectToYaml(projectData); |
| 478 | + const result = await serializer.deserializeNotebook(content, {} as any); |
| 479 | + |
| 480 | + // Should select the alphabetically first notebook |
| 481 | + assert.strictEqual(result.metadata?.deepnoteNotebookId, 'alpha-notebook'); |
| 482 | + assert.strictEqual(result.metadata?.deepnoteNotebookName, 'Alpha Notebook'); |
| 483 | + }); |
| 484 | + |
| 485 | + test('should sort Init notebook last when multiple notebooks exist', async () => { |
| 486 | + const projectData: DeepnoteFile = { |
| 487 | + version: '1.0', |
| 488 | + metadata: { |
| 489 | + createdAt: '2023-01-01T00:00:00Z', |
| 490 | + modifiedAt: '2023-01-02T00:00:00Z' |
| 491 | + }, |
| 492 | + project: { |
| 493 | + id: 'project-multiple', |
| 494 | + name: 'Project with Multiple', |
| 495 | + initNotebookId: 'init-notebook', |
| 496 | + notebooks: [ |
| 497 | + { |
| 498 | + id: 'charlie-notebook', |
| 499 | + name: 'Charlie', |
| 500 | + blocks: [ |
| 501 | + { |
| 502 | + id: 'block-c', |
| 503 | + content: 'print("charlie")', |
| 504 | + sortingKey: 'a0', |
| 505 | + type: 'code' |
| 506 | + } |
| 507 | + ], |
| 508 | + executionMode: 'block', |
| 509 | + isModule: false |
| 510 | + }, |
| 511 | + { |
| 512 | + id: 'init-notebook', |
| 513 | + name: 'Init', |
| 514 | + blocks: [ |
| 515 | + { |
| 516 | + id: 'block-init', |
| 517 | + content: 'print("init")', |
| 518 | + sortingKey: 'a0', |
| 519 | + type: 'code' |
| 520 | + } |
| 521 | + ], |
| 522 | + executionMode: 'block', |
| 523 | + isModule: false |
| 524 | + }, |
| 525 | + { |
| 526 | + id: 'alpha-notebook', |
| 527 | + name: 'Alpha', |
| 528 | + blocks: [ |
| 529 | + { |
| 530 | + id: 'block-a', |
| 531 | + content: 'print("alpha")', |
| 532 | + sortingKey: 'a0', |
| 533 | + type: 'code' |
| 534 | + } |
| 535 | + ], |
| 536 | + executionMode: 'block', |
| 537 | + isModule: false |
| 538 | + } |
| 539 | + ], |
| 540 | + settings: {} |
| 541 | + } |
| 542 | + }; |
| 543 | + |
| 544 | + const content = projectToYaml(projectData); |
| 545 | + const result = await serializer.deserializeNotebook(content, {} as any); |
| 546 | + |
| 547 | + // Should select Alpha, not Init even though "Init" comes before "Alpha" alphabetically when in upper case |
| 548 | + assert.strictEqual(result.metadata?.deepnoteNotebookId, 'alpha-notebook'); |
| 549 | + assert.strictEqual(result.metadata?.deepnoteNotebookName, 'Alpha'); |
| 550 | + }); |
| 551 | + }); |
316 | 552 | }); |
0 commit comments