@@ -5,13 +5,18 @@ import { EditorView } from "@codemirror/view";
55import { describe , expect , it } from "vitest" ;
66import { cellId , variableName } from "@/__tests__/branded" ;
77import { initialNotebookState , notebookAtom } from "@/core/cells/cells" ;
8+ import { createCell } from "@/core/cells/types" ;
89import { OverridingHotkeyProvider } from "@/core/hotkeys/hotkeys" ;
910import { store } from "@/core/state/jotai" ;
1011import { variablesAtom } from "@/core/variables/state" ;
1112import { MultiColumn } from "@/utils/id-tree" ;
1213import { cellConfigExtension } from "../../config/extension" ;
1314import { adaptiveLanguageConfiguration } from "../../language/extension" ;
14- import { getCodes , getTopologicalCellIds } from "../getCodes" ;
15+ import {
16+ getCodes ,
17+ getTopologicalCellIds ,
18+ topologicalCodesAtom ,
19+ } from "../getCodes" ;
1520
1621const Cells = {
1722 cell1 : cellId ( "cell1" ) ,
@@ -208,4 +213,64 @@ describe("getCodes", () => {
208213 const result = getCodes ( otherCode ) ;
209214 expect ( result ) . toEqual ( "import os\nx = 1\ny = x + 1\nprint('Hello World')" ) ;
210215 } ) ;
216+
217+ it ( "should read code from the store when no editor is mounted" , ( ) => {
218+ const otherCode = "print('Hello World')" ;
219+ store . set ( notebookAtom , {
220+ ...initialNotebookState ( ) ,
221+ cellIds : MultiColumn . from ( [ [ Cells . cell1 , Cells . cell2 ] ] ) ,
222+ cellData : {
223+ [ Cells . cell1 ] : createCell ( { id : Cells . cell1 , code : "import os" } ) ,
224+ [ Cells . cell2 ] : createCell ( { id : Cells . cell2 , code : "x = 1" } ) ,
225+ } ,
226+ } ) ;
227+ const result = getCodes ( otherCode ) ;
228+ expect ( result ) . toEqual ( "import os\nx = 1\nprint('Hello World')" ) ;
229+ } ) ;
230+
231+ it ( "should prefer the mounted editor code over the store code" , ( ) => {
232+ const otherCode = "print('Hello World')" ;
233+ store . set ( notebookAtom , {
234+ ...initialNotebookState ( ) ,
235+ cellIds : MultiColumn . from ( [ [ Cells . cell1 , Cells . cell2 ] ] ) ,
236+ cellData : {
237+ [ Cells . cell1 ] : createCell ( { id : Cells . cell1 , code : "import os" } ) ,
238+ [ Cells . cell2 ] : createCell ( { id : Cells . cell2 , code : "x = 1" } ) ,
239+ } ,
240+ cellHandles : {
241+ [ Cells . cell2 ] : { current : createMockEditorView ( "x = 2" ) } ,
242+ } ,
243+ } ) ;
244+ const result = getCodes ( otherCode ) ;
245+ expect ( result ) . toEqual ( "import os\nx = 2\nprint('Hello World')" ) ;
246+ } ) ;
247+ } ) ;
248+
249+ describe ( "topologicalCodesAtom" , ( ) => {
250+ it ( "should include every cell when no editor is mounted" , ( ) => {
251+ store . set ( notebookAtom , {
252+ ...initialNotebookState ( ) ,
253+ cellIds : MultiColumn . from ( [ [ Cells . cell1 , Cells . cell2 , Cells . cell3 ] ] ) ,
254+ cellData : {
255+ [ Cells . cell1 ] : createCell ( { id : Cells . cell1 , code : "import os" } ) ,
256+ [ Cells . cell2 ] : createCell ( { id : Cells . cell2 , code : "x = 1" } ) ,
257+ [ Cells . cell3 ] : createCell ( { id : Cells . cell3 , code : "y = x + 1" } ) ,
258+ } ,
259+ } ) ;
260+ store . set ( variablesAtom , {
261+ [ Variables . var1 ] : {
262+ name : Variables . var1 ,
263+ declaredBy : [ Cells . cell1 ] ,
264+ usedBy : [ Cells . cell2 , Cells . cell3 ] ,
265+ } ,
266+ } ) ;
267+
268+ const { cellIds, codes } = store . get ( topologicalCodesAtom ) ;
269+ expect ( cellIds ) . toEqual ( [ Cells . cell1 , Cells . cell2 , Cells . cell3 ] ) ;
270+ expect ( codes ) . toEqual ( {
271+ [ Cells . cell1 ] : "import os" ,
272+ [ Cells . cell2 ] : "x = 1" ,
273+ [ Cells . cell3 ] : "y = x + 1" ,
274+ } ) ;
275+ } ) ;
211276} ) ;
0 commit comments