A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
refactor: adjust cell matching for --watch (#6127)
Dylan Madisetti8c0a13dc3d5b
diff --git a/marimo/_ast/cell_manager.py b/marimo/_ast/cell_manager.pyindex b6643f4beb..e54f5d36e1 100644--- a/marimo/_ast/cell_manager.py+++ b/marimo/_ast/cell_manager.py@@ -32,6 +32,7 @@ from marimo._schemas.serialization import (SetupCell,)from marimo._types.ids import CellId_t+from marimo._utils.cell_matching import match_cell_ids_by_similarityif TYPE_CHECKING:from collections.abc import Iterable@@ -318,6 +319,17 @@ class CellManager:for cell_data in self._cell_data.values():yield cell_data.code+ def code_lookup(self) -> dict[CellId_t, str]:+ """Get a dict for cell codes.++ Returns:+ dict[CellId_t, str]: Dictionary mapping cell to their source code+ """+ return {+ cell_id: cell_data.code+ for cell_id, cell_data in self._cell_data.items()+ }+def configs(self) -> Iterable[CellConfig]:"""Get an iterator over all cell configurations.@@ -474,17 +486,10 @@ class CellManager:This mutates the current cell manager."""- prev_ids = list(prev_cell_manager.cell_ids())- prev_codes = [data.code for data in prev_cell_manager.cell_data()]- current_ids = list(self._cell_data.keys())- current_codes = [data.code for data in self.cell_data()]- sorted_ids = _match_cell_ids_by_similarity(- prev_ids, prev_codes, current_ids, current_codes- )- assert len(sorted_ids) == len(list(self.cell_ids()))-+ prev_codes = prev_cell_manager.code_lookup()+ current_codes = self.code_lookup()# Create mapping from new to old ids- id_mapping = dict(zip(sorted_ids, current_ids))+ id_mapping = match_cell_ids_by_similarity(prev_codes, current_codes)# Update the cell data in placenew_cell_data: dict[CellId_t, CellData] = {}@@ -496,131 +501,9 @@ class CellManager:self._cell_data = new_cell_data# Add the new ids to the set, so we don't reuse them in the future- for _id in sorted_ids:+ for _id in id_mapping.keys():self._cell_id_generator.seen_ids.add(_id)@propertydef seen_ids(self) -> set[CellId_t]:return self._cell_id_generator.seen_ids---def _match_cell_ids_by_similarity(- prev_ids: list[CellId_t],- prev_codes: list[str],- next_ids: list[CellId_t],- next_codes: list[str],-) -> list[CellId_t]:- """Match cell IDs based on code similarity.-- Args:- prev_ids: List of previous cell IDs, used as the set of possible IDs- prev_codes: List of previous cell codes- next_ids: List of next cell IDs, used only when more cells than prev_ids- next_codes: List of next cell codes-- Returns:- List of cell IDs matching next_codes, using prev_ids where possible- """- assert len(prev_codes) == len(prev_ids)- assert len(next_codes) == len(next_ids)-- # Initialize result and tracking sets- result: list[Optional[CellId_t]] = [None] * len(next_codes)- used_positions: set[int] = set()- used_prev_ids: set[CellId_t] = set()-- # Track which next_ids are new (not in prev_ids)- new_next_ids = [p_id for p_id in next_ids if p_id not in prev_ids]- new_id_idx = 0-- # First pass: exact matches using hash map- next_code_to_idx: dict[str, list[int]] = {}- for idx, code in enumerate(next_codes):- next_code_to_idx.setdefault(code, []).append(idx)-- for prev_idx, prev_code in enumerate(prev_codes):- if prev_ids[prev_idx] in used_prev_ids:- continue- if prev_code in next_code_to_idx:- # Use first available matching position- for next_idx in next_code_to_idx[prev_code]:- if next_idx not in used_positions:- result[next_idx] = prev_ids[prev_idx]- used_positions.add(next_idx)- used_prev_ids.add(prev_ids[prev_idx])- break-- # If all positions filled, we're done- if len(used_positions) == len(next_codes):- return [_id for _id in result if _id is not None] # type: ignore-- def similarity_score(s1: str, s2: str) -> int:- """Fast similarity score based on common prefix and suffix.- Returns lower score for more similar strings."""- # Find common prefix length- prefix_len = 0- for c1, c2 in zip(s1, s2):- if c1 != c2:- break- prefix_len += 1-- # Find common suffix length if strings differ in middle- if prefix_len < min(len(s1), len(s2)):- s1_rev = s1[::-1]- s2_rev = s2[::-1]- suffix_len = 0- for c1, c2 in zip(s1_rev, s2_rev):- if c1 != c2:- break- suffix_len += 1- else:- suffix_len = 0-- # Return inverse similarity - shorter common affix means higher score- return len(s1) + len(s2) - 2 * (prefix_len + suffix_len)-- # Filter out used positions and ids for similarity matrix- remaining_prev_indices = [- i for i, pid in enumerate(prev_ids) if pid not in used_prev_ids- ]- remaining_next_indices = [- i for i in range(len(next_codes)) if i not in used_positions- ]-- # Create similarity matrix only for remaining cells- similarity_matrix: list[list[int]] = []- for prev_idx in remaining_prev_indices:- row: list[int] = []- for next_idx in remaining_next_indices:- score = similarity_score(- prev_codes[prev_idx], next_codes[next_idx]- )- row.append(score)- similarity_matrix.append(row)-- # Second pass: best matches for remaining positions- for matrix_prev_idx, prev_idx in enumerate(remaining_prev_indices):- # Find best match among unused positions- min_score = float("inf") # type: ignore- best_next_matrix_idx = None- for matrix_next_idx, score in enumerate(- similarity_matrix[matrix_prev_idx]- ):- if score < min_score:- min_score = score- best_next_matrix_idx = matrix_next_idx-- if best_next_matrix_idx is not None:- next_idx = remaining_next_indices[best_next_matrix_idx]- result[next_idx] = prev_ids[prev_idx]- used_positions.add(next_idx)- used_prev_ids.add(prev_ids[prev_idx])-- # Fill remaining positions with new next_ids- for i in range(len(next_codes)):- if result[i] is None:- if new_id_idx < len(new_next_ids):- result[i] = new_next_ids[new_id_idx]- new_id_idx += 1-- return [_id for _id in result if _id is not None] # type: ignorediff --git a/marimo/_utils/cell_matching.py b/marimo/_utils/cell_matching.pynew file mode 100644index 0000000000..2468eee6de--- /dev/null+++ b/marimo/_utils/cell_matching.py@@ -0,0 +1,302 @@+# Copyright 2024 Marimo. All rights reserved.+from __future__ import annotations++from typing import TYPE_CHECKING, Optional++from marimo._types.ids import CellId_t++if TYPE_CHECKING:+ from collections.abc import Sequence+++def similarity_score(s1: str, s2: str) -> float:+ """Fast similarity score based on common prefix and suffix.+ Returns lower score for more similar strings."""+ # Find common prefix length+ prefix_len = 0+ for c1, c2 in zip(s1, s2):+ if c1 != c2:+ break+ prefix_len += 1++ # Find common suffix length if strings differ in middle+ if prefix_len < min(len(s1), len(s2)):+ s1_rev = s1[::-1]+ s2_rev = s2[::-1]+ suffix_len = 0+ for c1, c2 in zip(s1_rev, s2_rev):+ if c1 != c2:+ break+ suffix_len += 1+ else:+ suffix_len = 0++ # Return inverse similarity - shorter common affix means higher score+ return len(s1) + len(s2) - 2.0 * (prefix_len + suffix_len)+++def group_lookup(+ ids: Sequence[CellId_t], codes: Sequence[str]+) -> dict[str, list[tuple[int, CellId_t]]]:+ lookup: dict[str, list[tuple[int, CellId_t]]] = {}+ for idx, (cell_id, code) in enumerate(zip(ids, codes)):+ lookup.setdefault(code, []).append((idx, cell_id))+ return lookup+++def extract_order(+ codes: list[str], lookup: dict[str, list[tuple[int, CellId_t]]]+) -> list[list[int]]:+ offset = 0+ order: list[list[int]] = [[]] * len(codes)+ for i, code in enumerate(codes):+ dupes = len(lookup[code])+ order[i] = [offset + j for j in range(dupes)]+ offset += dupes+ return order+++def get_unique(+ codes: Sequence[str], available: dict[str, list[tuple[int, CellId_t]]]+) -> list[str]:+ # Order matters, required opposed to using set()+ seen = set(codes) - set(available.keys())+ unique_codes = []+ for code in codes:+ if code not in seen:+ seen.add(code)+ unique_codes.append(code)+ return unique_codes+++def pop_local(available: list[tuple[int, CellId_t]], idx: int) -> CellId_t:+ """Find and pop the index that is closest to idx"""+ # NB. by min implementation a preference is given to the lower index when equidistant+ best_idx = min(+ range(len(available)), key=lambda i: abs(available[i][0] - idx)+ )+ return available.pop(best_idx)[1]+++def _hungarian_algorithm(scores: list[list[float]]) -> list[int]:+ """Implements the Hungarian algorithm to find the best matching.++ In general this class of problem is known as the assignment problem and is+ pretty well studied. This is a textbook implementation to avoid additional+ dependencies. Links:+ - https://en.wikipedia.org/wiki/Hungarian_algorithm+ """+ score_matrix = [row[:] for row in scores]+ n = len(score_matrix)++ # Step 1: Subtract row minima+ for i in range(n):+ min_value = min(score_matrix[i])+ for j in range(n):+ score_matrix[i][j] -= min_value++ # Step 2: Subtract column minima+ for j in range(n):+ min_value = min(score_matrix[i][j] for i in range(n))+ for i in range(n):+ score_matrix[i][j] -= min_value++ # Step 3: Find initial assignment+ row_assignment = [-1] * n+ col_assignment = [-1] * n++ # Find independent zeros+ for i in range(n):+ for j in range(n):+ if (+ score_matrix[i][j] == 0+ and row_assignment[i] == -1+ and col_assignment[j] == -1+ ):+ row_assignment[i] = j+ col_assignment[j] = i++ # Step 4: Improve assignment iteratively+ while True:+ assigned_count = sum(1 for x in row_assignment if x != -1)+ if assigned_count == n:+ break++ # Find minimum uncovered value+ min_uncovered = float("inf")+ for i in range(n):+ for j in range(n):+ if row_assignment[i] == -1 and col_assignment[j] == -1:+ min_uncovered = min(min_uncovered, score_matrix[i][j])++ if min_uncovered == float("inf"):+ break++ # Update matrix+ for i in range(n):+ for j in range(n):+ if row_assignment[i] == -1 and col_assignment[j] == -1:+ score_matrix[i][j] -= min_uncovered+ elif row_assignment[i] != -1 and col_assignment[j] != -1:+ score_matrix[i][j] += min_uncovered++ # Try to find new assignments+ for i in range(n):+ if row_assignment[i] == -1:+ for j in range(n):+ if score_matrix[i][j] == 0 and col_assignment[j] == -1:+ row_assignment[i] = j+ col_assignment[j] = i+ break++ # Convert to result format+ result = [-1] * n+ for i in range(n):+ if row_assignment[i] != -1:+ result[row_assignment[i]] = i++ return result+++def _match_cell_ids_by_similarity(+ prev_ids: Sequence[CellId_t],+ prev_codes: Sequence[str],+ next_ids: Sequence[CellId_t],+ next_codes: Sequence[str],+) -> list[CellId_t]:+ """Match cell IDs based on code similarity."""+ assert len(prev_codes) == len(prev_ids)+ assert len(next_codes) == len(next_ids)++ # ids that are not in prev_ids but in next_ids+ id_pool = set(next_ids) - set(prev_ids)++ def get_next_available_id(idx: int) -> CellId_t:+ cell_id = next_ids[idx]+ # Use the id from the pool if available+ if cell_id in id_pool:+ id_pool.remove(cell_id)+ elif id_pool:+ # Otherwise just use the next available id+ cell_id = id_pool.pop()+ else:+ # If no ids are available, we could generate a new one+ # but this should never run.+ raise RuntimeError(+ "No available IDs left to assign. This should not happen."+ )+ return cell_id++ def filter_and_backfill() -> list[CellId_t]:+ for idx, _ in enumerate(next_ids):+ if result[idx] is None:+ # If we have a None, we need to fill it with an available ID+ result[idx] = get_next_available_id(idx)+ # Only needed to appease the type checker. We just filled all None+ # values.+ return [_id for _id in result if _id is not None]++ # Hash matching to capture permutations+ # covers next is a subset of prev (i.e. next - prev == {})+ previous_lookup = group_lookup(prev_ids, prev_codes)+ next_lookup = group_lookup(next_ids, next_codes)++ result: list[Optional[CellId_t]] = [None] * len(next_codes)+ filled = 0+ for idx, code in enumerate(next_codes):+ if code in previous_lookup:+ # If we have an exact match, use it+ filled += 1+ result[idx] = pop_local(previous_lookup[code], idx)+ if not previous_lookup[code]:+ del previous_lookup[code]+ # Clean up the next_lookup match too.+ if code in next_lookup:+ pop_local(next_lookup[code], idx)+ if not next_lookup[code]:+ del next_lookup[code]++ # If we filled all positions, return the result+ # or if prev is a subset of next, then prev has been dequeued and emptied,+ # we can just backfill and return.+ if filled == len(next_codes) or not previous_lookup:+ return filter_and_backfill()++ # The remaining case is (next - prev) is not empty.+ # Establish specific order of remaining unique codes so we can match them+ added_code = get_unique(next_codes, next_lookup)+ deleted_code = get_unique(prev_codes, previous_lookup)++ # Build order mappings for the Hungarian algorithm+ next_order = extract_order(added_code, next_lookup)+ prev_order = extract_order(deleted_code, previous_lookup)++ # grab indices for lookup+ next_inverse = {code: i for i, code in enumerate(added_code)}+ # and inverse mapping for prev+ inverse_order = {+ idx: i for i, idxs in enumerate(prev_order) for idx in idxs+ }++ # Pad the scores matrix to ensure it is square+ n = max(len(next_codes) - filled, len(prev_codes) - filled)+ scores = [[0.0] * n for _ in range(n)]+ # Fill matrix, accounting for dupes+ for i, code in enumerate(added_code):+ for j, prev_code in enumerate(deleted_code):+ score = similarity_score(prev_code, code)+ for x in next_order[i]:+ for y in prev_order[j]:+ # NB. transposed indices for Hungarian+ scores[y][x] = score++ # Use Hungarian algorithm to find the best matching+ matches = _hungarian_algorithm(scores)+ for idx, code in enumerate(next_codes):+ if result[idx] is None:+ match_idx = next_order[next_inverse[code]].pop(0)+ if match_idx != -1 and matches[match_idx] in inverse_order:+ prev_idx = inverse_order[matches[match_idx]]+ prev_code = deleted_code[prev_idx]+ result[idx] = pop_local(previous_lookup[prev_code], idx)++ return filter_and_backfill()+++def match_cell_ids_by_similarity(+ prev_data: dict[CellId_t, str], next_data: dict[CellId_t, str]+) -> dict[CellId_t, CellId_t]:+ """Match cell IDs based on code similarity.++ NB. There is similar code in the front end that matches session results to+ cells, but there are a few caveats for why the logic is different:+ - Session matching is inherent order dependent. If the order is wrong,+ there is no match. Moreover, the code must be an exact match for a+ session to be paired.+ - Cell matching in this context is not order dependent, we assume the+ notebook can be totally scrambled and we still want to match. Lose cell+ matching is also allowed.+ As such, in the frontend case a Lavenshtein edit is used to match cells to+ session results based on code.+ While here we can naively use a direct match, and non-matching cells are still+ attempted to match based on some similarity metric.++ Args:+ prev_data: Mapping of previous cell IDs to code+ next_data: Mapping of next cell IDs to code++ Returns:+ A map of old ids to new ids, using prev_ids where possible+ """++ prev_ids, prev_codes = zip(*prev_data.items())+ next_ids, next_codes = zip(*next_data.items())++ sorted_ids = _match_cell_ids_by_similarity(+ prev_ids,+ prev_codes,+ next_ids,+ next_codes,+ )++ return dict(zip(sorted_ids, next_ids))diff --git a/tests/_ast/test_cell_manager.py b/tests/_ast/test_cell_manager.pyindex df6f167929..7442c0fe2e 100644--- a/tests/_ast/test_cell_manager.py+++ b/tests/_ast/test_cell_manager.py@@ -3,10 +3,13 @@ from __future__ import annotationsimport pytestfrom marimo._ast.cell import Cell, CellConfig-from marimo._ast.cell_manager import CellManager, _match_cell_ids_by_similarity+from marimo._ast.cell_manager import (+ CellManager,+)from marimo._ast.compiler import compile_cellfrom marimo._ast.names import DEFAULT_CELL_NAMEfrom marimo._types.ids import CellId_t+from marimo._utils.cell_matching import _match_cell_ids_by_similarity# Test cell ID constantsCELL_A = CellId_t("a")@@ -23,6 +26,9 @@ TEST_CELL2 = CellId_t("test_cell2")UNUSED_A = CellId_t("unused_a")UNUSED_B = CellId_t("unused_b")UNUSED_C = CellId_t("unused_c")+UNUSED_X = CellId_t("unused_x")+UNUSED_Y = CellId_t("unused_y")+UNUSED_Z = CellId_t("unused_z")@pytest.fixture@@ -30,429 +36,531 @@ def cell_manager():return CellManager(prefix="test_")-def test_create_cell_id(cell_manager: CellManager) -> None:- # Test deterministic behavior with fixed seed- cell_id1 = cell_manager.create_cell_id()- cell_id2 = cell_manager.create_cell_id()-- assert cell_id1.startswith("test_")- assert len(cell_id1) == 9 # "test_" + 4 random letters- assert cell_id1 != cell_id2---def test_register_cell(cell_manager: CellManager) -> None:- cell_id = CellId_t("test_cell")- code = "print('hello')"- config = CellConfig()-- cell_manager.register_cell(- cell_id=cell_id,- code=code,- config=config,- name=DEFAULT_CELL_NAME,- )-- assert cell_manager.has_cell(cell_id)- cell_data = cell_manager.cell_data_at(cell_id)- assert cell_data.code == code- assert cell_data.config == config- assert cell_data.name == DEFAULT_CELL_NAME---def test_register_cell_auto_id(cell_manager: CellManager) -> None:- code = "print('hello')"- config = CellConfig()-- cell_manager.register_cell(- cell_id=None,- code=code,- config=config,- )-- # Should have created one cell with an auto-generated ID- assert len(list(cell_manager.cell_ids())) == 1- cell_id = next(iter(cell_manager.cell_ids()))- assert cell_id.startswith("test_")---def test_ensure_one_cell(cell_manager: CellManager) -> None:- assert len(list(cell_manager.cell_ids())) == 0- cell_manager.ensure_one_cell()- assert len(list(cell_manager.cell_ids())) == 1-- # Calling again shouldn't add another cell- cell_manager.ensure_one_cell()- assert len(list(cell_manager.cell_ids())) == 1---def test_cell_queries(cell_manager: CellManager) -> None:- code1 = "print('hello')"- code2 = "print('world')"- config1 = CellConfig(column=1)- config2 = CellConfig(disabled=True)-- cell_manager.register_cell(TEST_CELL1, code1, config1, name="cell1")- cell_manager.register_cell(TEST_CELL2, code2, config2, name="cell2")-- assert list(cell_manager.names()) == ["cell1", "cell2"]- assert list(cell_manager.codes()) == [code1, code2]- assert list(cell_manager.configs()) == [config1, config2]- assert list(cell_manager.cell_ids()) == [TEST_CELL1, TEST_CELL2]- assert cell_manager.config_map() == {- TEST_CELL1: config1,- TEST_CELL2: config2,- }---def test_get_cell_id_by_code(cell_manager: CellManager) -> None:- code = "print('unique')"- cell_manager.register_cell(TEST_CELL1, code, CellConfig())- cell_manager.register_cell(TEST_CELL2, "different_code", CellConfig())-- assert cell_manager.get_cell_id_by_code(code) == TEST_CELL1- assert cell_manager.get_cell_id_by_code("nonexistent") is None---def test_get_cell_code(cell_manager: CellManager) -> None:- code = "print('hello')"- cell_manager.register_cell(TEST_CELL1, code, CellConfig())- assert cell_manager.get_cell_code(TEST_CELL1) == code- assert cell_manager.get_cell_code(CellId_t("nonexistent")) is None---def test_get_cell_data(cell_manager: CellManager) -> None:- code = "print('hello')"- config = CellConfig(column=1, disabled=True)- name = "test_cell"-- cell_manager.register_cell(TEST_CELL1, code, config, name=name)-- # Test getting existing cell data- cell_data = cell_manager.get_cell_data(TEST_CELL1)- assert cell_data is not None- assert cell_data.cell_id == TEST_CELL1- assert cell_data.code == code- assert cell_data.config == config- assert cell_data.name == name-- # Test getting non-existent cell data- assert cell_manager.get_cell_data(CellId_t("nonexistent")) is None---def test_register_unparsable_cell(cell_manager: CellManager) -> None:- code = """- def unparsable():- return "test"- """- config = CellConfig()-- cell_manager.register_unparsable_cell(- code=code,- name="unparsable",- cell_config=config,- )-- cell_data = next(iter(cell_manager.cell_data()))- assert cell_data.name == "unparsable"- assert "def unparsable():" in cell_data.code- assert cell_data.cell is None # Unparsable cells have no Cell object---def test_valid_cells(cell_manager: CellManager) -> None:- # Register a mix of valid and invalid cells- cell1 = Cell(_name="_", _cell=compile_cell("print('valid')", TEST_CELL1))- cell_manager.register_cell(- TEST_CELL1, "print('valid')", CellConfig(), cell=cell1- )- cell_manager.register_cell(- TEST_CELL2, "print('invalid')", CellConfig(), cell=None- )-- valid_cells = list(cell_manager.valid_cells())- assert len(valid_cells) == 1- assert valid_cells[0][0] == TEST_CELL1- assert valid_cells[0][1] == cell1-- valid_ids = list(cell_manager.valid_cell_ids())- assert valid_ids == [TEST_CELL1]---def test_match_cell_ids_by_similarity():- # Test exact matches- assert _match_cell_ids_by_similarity(- prev_ids=[CELL_A, CELL_B, CELL_C],- prev_codes=["abc", "def", "ghi"],- next_ids=[UNUSED_A, UNUSED_B, UNUSED_C],- next_codes=["abc", "def", "ghi"],- ) == [CELL_A, CELL_B, CELL_C]-- # Test with reordered codes- assert _match_cell_ids_by_similarity(- prev_ids=[CELL_A, CELL_B, CELL_C],- prev_codes=["abc", "def", "ghi"],- next_ids=[UNUSED_A, UNUSED_B, UNUSED_C],- next_codes=["def", "ghi", "abc"],- ) == [CELL_B, CELL_C, CELL_A]-- # Test with similar but not exact matches- assert _match_cell_ids_by_similarity(- prev_ids=[CELL_A, CELL_B, CELL_C],- prev_codes=["abc", "def", "ghi"],- next_ids=[UNUSED_A, UNUSED_B, UNUSED_C],- next_codes=["ghij", "abcd", "defg"],- ) == [CELL_C, CELL_A, CELL_B]-- # Test with fewer next cells- result = _match_cell_ids_by_similarity(- prev_ids=[CELL_A, CELL_B, CELL_C],- prev_codes=["abc", "def", "ghi"],- next_ids=[UNUSED_A, UNUSED_B],- next_codes=["abc", "ghi"],- )- assert len(result) == 2- assert result == [CELL_A, CELL_C]-- # Test with more next cells- result = _match_cell_ids_by_similarity(- prev_ids=[CELL_A, CELL_B],- prev_codes=["abc", "def"],- next_ids=[CELL_A, CELL_B, CELL_C],- next_codes=["def", "ghi", "abc"],- )- assert len(result) == 3- assert result == [CELL_B, CELL_C, CELL_A]-- # Test with completely different codes- result = _match_cell_ids_by_similarity(- prev_ids=[CELL_A, CELL_B],- prev_codes=["abc", "def"],- next_ids=[UNUSED_A, UNUSED_B],- next_codes=["xyz", "123"],- )- assert len(result) == 2-- # Test empty lists- assert _match_cell_ids_by_similarity([], [], [], []) == []-- # Test with empty strings- assert _match_cell_ids_by_similarity(- prev_ids=[CELL_A],- prev_codes=[""],- next_ids=[UNUSED_A],- next_codes=[""],- ) == [CELL_A]---def test_match_cell_ids_by_similarity_edge_cases():- # Test with multiple identical codes in prev- result = _match_cell_ids_by_similarity(- prev_ids=[CELL_A, CELL_B, CELL_C],- prev_codes=["same", "same", "diff"],- next_ids=[CELL_X, CELL_Y],- next_codes=["same", "diff"],- )- assert len(result) == 2- assert result[0] in [CELL_A, CELL_B]- assert result[1] == CELL_C-- # Test with multiple identical codes in next- result = _match_cell_ids_by_similarity(- prev_ids=[CELL_A, CELL_B],- prev_codes=["code1", "code2"],- next_ids=[CELL_X, CELL_Y, CELL_Z],- next_codes=["code1", "code1", "code2"],- )- assert len(result) == 3- assert result == [CELL_A, CELL_X, CELL_B]-- # Test with very long common prefixes/suffixes- long_prefix = "x" * 1000- result = _match_cell_ids_by_similarity(- prev_ids=[CELL_A, CELL_B],- prev_codes=[long_prefix + "1", long_prefix + "2"],- next_ids=[CELL_X, CELL_Y],- next_codes=[long_prefix + "2", long_prefix + "1"],- )- assert result == [CELL_B, CELL_A]-- # Test with Unicode and special characters- result = _match_cell_ids_by_similarity(- prev_ids=[CELL_A, CELL_B, CELL_C],- prev_codes=["🔥", "∑∫", "\n\t\r"],- next_ids=[CELL_X, CELL_Y, CELL_Z],- next_codes=["∑∫", "🔥", "\n\t\r"],- )- assert result == [CELL_B, CELL_A, CELL_C]-- # Test with mixed case sensitivity- result = _match_cell_ids_by_similarity(- prev_ids=[CELL_A, CELL_B],- prev_codes=["ABC", "def"],- next_ids=[CELL_X, CELL_Y],- next_codes=["abc", "DEF"],- )- assert len(result) == 2-- # Test with whitespace variations- result = _match_cell_ids_by_similarity(- prev_ids=[CELL_A, CELL_B],- prev_codes=["x y", "a\nb"],- next_ids=[CELL_X, CELL_Y],- next_codes=["x y", "a b"],- )- assert result == [CELL_A, CELL_B]-- # Test with all codes being substrings of each other- result = _match_cell_ids_by_similarity(- prev_ids=[CELL_A, CELL_B, CELL_C],- prev_codes=["x", "xy", "xyz"],- next_ids=[CELL_P, CELL_Q, CELL_R],- next_codes=["xyz", "xy", "x"],- )- assert result == [CELL_C, CELL_B, CELL_A]-- # Test with maximum length differences- result = _match_cell_ids_by_similarity(- prev_ids=[CELL_A, CELL_B],- prev_codes=["x", "x" * 10000],- next_ids=[CELL_Y, CELL_Z],- next_codes=["x" * 10000, "x"],- )- assert result == [CELL_B, CELL_A]-- # Test with empty strings- result = _match_cell_ids_by_similarity(- prev_ids=[CELL_A, CELL_B],- prev_codes=["", "x"],- next_ids=[CELL_Y, CELL_Z],- next_codes=["x", ""],- )- assert result == [CELL_B, CELL_A]-- # Test with identical codes- result = _match_cell_ids_by_similarity(- prev_ids=[CELL_A, CELL_B, CELL_C],- prev_codes=["same", "same", "same"],- next_ids=[CELL_X, CELL_Y, CELL_Z],- next_codes=["same", "same", "same"],- )- assert len(result) == 3- assert set(result) == {CELL_A, CELL_B, CELL_C}-- # Test with completely different codes- result = _match_cell_ids_by_similarity(- prev_ids=[CELL_A, CELL_B],- prev_codes=["abc", "def"],- next_ids=[CELL_X, CELL_Y],- next_codes=["123", "456"],- )- assert len(result) == 2- # This is probably ok since they are completely different.- # Not sure what the best behavior is.- assert result == [CELL_B, CELL_X]-- # Test with special Python syntax- result = _match_cell_ids_by_similarity(- prev_ids=[CELL_A, CELL_B],- prev_codes=["def foo():", "class Bar:"],- next_ids=[CELL_X, CELL_Y],- next_codes=["class Bar:", "def foo():"],- )- assert result == [CELL_B, CELL_A]---def test_sort_cell_ids_by_similarity_reorder():- # Test simple reorder- prev_manager = CellManager()- prev_manager.register_cell(CELL_A, "code1", CellConfig())- prev_manager.register_cell(CELL_B, "code2", CellConfig())-- curr_manager = CellManager()- curr_manager.register_cell(CELL_X, "code2", CellConfig())- curr_manager.register_cell(CELL_Y, "code1", CellConfig())-- # Save original seen_ids- original_seen_ids = curr_manager.seen_ids.copy()-- curr_manager.sort_cell_ids_by_similarity(prev_manager)- assert list(curr_manager.cell_ids()) == [CELL_B, CELL_A]- assert curr_manager.cell_data_at(CELL_B).code == "code2"- assert curr_manager.cell_data_at(CELL_A).code == "code1"-- # Check seen_ids were updated- assert curr_manager.seen_ids == original_seen_ids | {CELL_A, CELL_B}---def test_sort_cell_ids_by_similarity_reorder_same_ids():- # Test simple reorder- prev_manager = CellManager()- prev_manager.register_cell(CELL_A, "code1", CellConfig())- prev_manager.register_cell(CELL_B, "code2", CellConfig())-- curr_manager = CellManager()- curr_manager.register_cell(CELL_A, "code2", CellConfig())- curr_manager.register_cell(CELL_B, "code1", CellConfig())-- # Save original seen_ids- original_seen_ids = curr_manager.seen_ids.copy()-- curr_manager.sort_cell_ids_by_similarity(prev_manager)- assert list(curr_manager.cell_ids()) == [CELL_B, CELL_A]- assert curr_manager.cell_data_at(CELL_B).code == "code2"- assert curr_manager.cell_data_at(CELL_A).code == "code1"-- # Check seen_ids were updated- assert curr_manager.seen_ids == original_seen_ids | {CELL_A, CELL_B}---def test_sort_cell_ids_by_similarity_less_cells():- # Test less cells than before- prev_manager = CellManager()- prev_manager.register_cell(CELL_A, "code1", CellConfig())- prev_manager.register_cell(CELL_B, "code2", CellConfig())- prev_manager.register_cell(CELL_C, "code3", CellConfig())-- curr_manager = CellManager()- curr_manager.register_cell(CELL_X, "code2", CellConfig())- curr_manager.register_cell(CELL_Y, "code1", CellConfig())-- original_seen_ids = curr_manager.seen_ids.copy()-- curr_manager.sort_cell_ids_by_similarity(prev_manager)- assert list(curr_manager.cell_ids()) == [CELL_B, CELL_A]- assert curr_manager.seen_ids == original_seen_ids | {CELL_A, CELL_B}- assert curr_manager.cell_data_at(CELL_B).code == "code2"- assert curr_manager.cell_data_at(CELL_A).code == "code1"---def test_sort_cell_ids_by_similarity_more_cells():- # Test more cells than before- prev_manager = CellManager()- prev_manager.register_cell(CELL_A, "code1", CellConfig())- prev_manager.register_cell(CELL_B, "code2", CellConfig())-- curr_manager = CellManager()- curr_manager.register_cell(CELL_X, "code2", CellConfig())- curr_manager.register_cell(CELL_Y, "code1", CellConfig())- curr_manager.register_cell(CELL_Z, "code3", CellConfig())-- original_seen_ids = curr_manager.seen_ids.copy()-- curr_manager.sort_cell_ids_by_similarity(prev_manager)- assert list(curr_manager.cell_ids()) == [CELL_B, CELL_A, CELL_X]- assert curr_manager.seen_ids == original_seen_ids | {- CELL_A,- CELL_B,- CELL_X,- }- assert curr_manager.cell_data_at(CELL_B).code == "code2"- assert curr_manager.cell_data_at(CELL_A).code == "code1"- assert curr_manager.cell_data_at(CELL_X).code == "code3"---def test_create_cell_id_1000():- manager = CellManager()- ids: set[CellId_t] = set()- for _ in range(1000):- ids.add(manager.create_cell_id())- assert len(ids) == 1000+class TestCellManager:+ """Test class for CellManager functionality."""++ def test_create_cell_id(self, cell_manager: CellManager) -> None:+ # Test deterministic behavior with fixed seed+ cell_id1 = cell_manager.create_cell_id()+ cell_id2 = cell_manager.create_cell_id()++ assert cell_id1.startswith("test_")+ assert len(cell_id1) == 9 # "test_" + 4 random letters+ assert cell_id1 != cell_id2++ def test_register_cell(self, cell_manager: CellManager) -> None:+ cell_id = CellId_t("test_cell")+ code = "print('hello')"+ config = CellConfig()++ cell_manager.register_cell(+ cell_id=cell_id,+ code=code,+ config=config,+ name=DEFAULT_CELL_NAME,+ )++ assert cell_manager.has_cell(cell_id)+ cell_data = cell_manager.cell_data_at(cell_id)+ assert cell_data.code == code+ assert cell_data.config == config+ assert cell_data.name == DEFAULT_CELL_NAME++ def test_register_cell_auto_id(self, cell_manager: CellManager) -> None:+ code = "print('hello')"+ config = CellConfig()++ cell_manager.register_cell(+ cell_id=None,+ code=code,+ config=config,+ )++ # Should have created one cell with an auto-generated ID+ assert len(list(cell_manager.cell_ids())) == 1+ cell_id = next(iter(cell_manager.cell_ids()))+ assert cell_id.startswith("test_")++ def test_ensure_one_cell(self, cell_manager: CellManager) -> None:+ assert len(list(cell_manager.cell_ids())) == 0+ cell_manager.ensure_one_cell()+ assert len(list(cell_manager.cell_ids())) == 1++ # Calling again shouldn't add another cell+ cell_manager.ensure_one_cell()+ assert len(list(cell_manager.cell_ids())) == 1++ def test_cell_queries(self, cell_manager: CellManager) -> None:+ code1 = "print('hello')"+ code2 = "print('world')"+ config1 = CellConfig(column=1)+ config2 = CellConfig(disabled=True)++ cell_manager.register_cell(TEST_CELL1, code1, config1, name="cell1")+ cell_manager.register_cell(TEST_CELL2, code2, config2, name="cell2")++ assert list(cell_manager.names()) == ["cell1", "cell2"]+ assert list(cell_manager.codes()) == [code1, code2]+ assert list(cell_manager.configs()) == [config1, config2]+ assert list(cell_manager.cell_ids()) == [TEST_CELL1, TEST_CELL2]+ assert cell_manager.config_map() == {+ TEST_CELL1: config1,+ TEST_CELL2: config2,+ }++ def test_get_cell_id_by_code(self, cell_manager: CellManager) -> None:+ code = "print('unique')"+ cell_manager.register_cell(TEST_CELL1, code, CellConfig())+ cell_manager.register_cell(TEST_CELL2, "different_code", CellConfig())++ assert cell_manager.get_cell_id_by_code(code) == TEST_CELL1+ assert cell_manager.get_cell_id_by_code("nonexistent") is None++ def test_get_cell_code(self, cell_manager: CellManager) -> None:+ code = "print('hello')"+ cell_manager.register_cell(TEST_CELL1, code, CellConfig())+ assert cell_manager.get_cell_code(TEST_CELL1) == code+ assert cell_manager.get_cell_code(CellId_t("nonexistent")) is None++ def test_get_cell_data(self, cell_manager: CellManager) -> None:+ code = "print('hello')"+ config = CellConfig(column=1, disabled=True)+ name = "test_cell"++ cell_manager.register_cell(TEST_CELL1, code, config, name=name)++ # Test getting existing cell data+ cell_data = cell_manager.get_cell_data(TEST_CELL1)+ assert cell_data is not None+ assert cell_data.cell_id == TEST_CELL1+ assert cell_data.code == code+ assert cell_data.config == config+ assert cell_data.name == name++ # Test getting non-existent cell data+ assert cell_manager.get_cell_data(CellId_t("nonexistent")) is None++ def test_register_unparsable_cell(self, cell_manager: CellManager) -> None:+ code = """+ def unparsable():+ return "test"+ """+ config = CellConfig()++ cell_manager.register_unparsable_cell(+ code=code,+ name="unparsable",+ cell_config=config,+ )++ cell_data = next(iter(cell_manager.cell_data()))+ assert cell_data.name == "unparsable"+ assert "def unparsable():" in cell_data.code+ assert cell_data.cell is None # Unparsable cells have no Cell object++ def test_valid_cells(self, cell_manager: CellManager) -> None:+ # Register a mix of valid and invalid cells+ cell1 = Cell(+ _name="_", _cell=compile_cell("print('valid')", TEST_CELL1)+ )+ cell_manager.register_cell(+ TEST_CELL1, "print('valid')", CellConfig(), cell=cell1+ )+ cell_manager.register_cell(+ TEST_CELL2, "print('invalid')", CellConfig(), cell=None+ )++ valid_cells = list(cell_manager.valid_cells())+ assert len(valid_cells) == 1+ assert valid_cells[0][0] == TEST_CELL1+ assert valid_cells[0][1] == cell1++ valid_ids = list(cell_manager.valid_cell_ids())+ assert valid_ids == [TEST_CELL1]++ def test_create_cell_id_1000(self) -> None:+ manager = CellManager()+ ids: set[CellId_t] = set()+ for _ in range(1000):+ ids.add(manager.create_cell_id())+ assert len(ids) == 1000+++class TestCellMatching:+ """Test class for cell matching functionality."""++ def test_exact_matches(self) -> None:+ """Test exact code matches."""+ result = _match_cell_ids_by_similarity(+ prev_ids=[CELL_A, CELL_B, CELL_C],+ prev_codes=["abc", "def", "ghi"],+ next_ids=[UNUSED_A, UNUSED_B, UNUSED_C],+ next_codes=["abc", "def", "ghi"],+ )+ assert result == [CELL_A, CELL_B, CELL_C]++ def test_reordered_codes(self) -> None:+ """Test with reordered codes."""+ result = _match_cell_ids_by_similarity(+ prev_ids=[CELL_A, CELL_B, CELL_C],+ prev_codes=["abc", "def", "ghi"],+ next_ids=[UNUSED_A, UNUSED_B, UNUSED_C],+ next_codes=["def", "ghi", "abc"],+ )+ assert result == [CELL_B, CELL_C, CELL_A]++ def test_similar_but_not_exact_matches(self) -> None:+ """Test with similar but not exact matches."""+ result = _match_cell_ids_by_similarity(+ prev_ids=[CELL_A, CELL_B, CELL_C],+ prev_codes=["abc", "def", "ghi"],+ next_ids=[UNUSED_A, UNUSED_B, UNUSED_C],+ next_codes=["ghij", "abcd", "defg"],+ )+ assert result == [CELL_C, CELL_A, CELL_B]++ def test_similar_but_not_exact_matches_with_dupes(self) -> None:+ """Test with similar but not exact matches and duplicates."""+ result = _match_cell_ids_by_similarity(+ prev_ids=[CELL_A, CELL_B, CELL_C, CELL_X],+ prev_codes=["abc", "def", "ghi", "ghi"],+ next_ids=[UNUSED_A, UNUSED_B, UNUSED_C, UNUSED_X],+ next_codes=["ghij", "abcd", "defg", "ghij"],+ )+ assert result == [CELL_C, CELL_A, CELL_B, CELL_X]++ def test_left_inexact_matches_with_dupes(self) -> None:+ """Test left inexact matches and duplicates."""+ result = _match_cell_ids_by_similarity(+ prev_ids=[CELL_A, CELL_B, CELL_C],+ prev_codes=["abc", "def", "ghi"],+ next_ids=[UNUSED_A, UNUSED_B, UNUSED_C, CELL_X],+ next_codes=["ghij", "abcd", "defg", "ghij"],+ )+ assert result == [CELL_C, CELL_A, CELL_B, CELL_X]++ def test_right_inexact_matches_with_dupes(self) -> None:+ """Test right inexact matches and duplicates."""+ result = _match_cell_ids_by_similarity(+ prev_ids=[CELL_A, CELL_B, CELL_C, CELL_X],+ prev_codes=["abc", "def", "ghi", "ghi"],+ next_ids=[UNUSED_A, UNUSED_B, UNUSED_C],+ next_codes=["ghij", "abcd", "defg"],+ )+ assert result == [CELL_C, CELL_A, CELL_B]++ def test_outer_inexact_matches(self) -> None:+ """Test outer inexact matches and duplicates."""+ result = _match_cell_ids_by_similarity(+ prev_ids=[CELL_A, CELL_B, CELL_C, CELL_X],+ prev_codes=["abc", "def", "ghi", "123"],+ next_ids=[UNUSED_A, UNUSED_B, UNUSED_C, UNUSED_X],+ next_codes=["ghij", "abcd", "defg", "abc"],+ )+ assert result == [CELL_C, CELL_X, CELL_B, CELL_A]++ def test_outer_inexact_matches_with_dupes(self) -> None:+ """Test outer inexact matches and duplicates."""+ result = _match_cell_ids_by_similarity(+ prev_ids=[CELL_A, CELL_B, CELL_C, CELL_X, CELL_Y, CELL_Z],+ prev_codes=["abc", "def", "ghi", "def", "abc", "123"],+ next_ids=[+ UNUSED_A,+ UNUSED_B,+ UNUSED_C,+ UNUSED_X,+ UNUSED_Y,+ UNUSED_Z,+ ],+ next_codes=["ghij", "abcd", "defg", "defg", "ghij", "abc"],+ )+ # NB. Locality given preference when dequeuing dupes.+ assert result == [CELL_C, CELL_A, CELL_B, CELL_X, CELL_Z, CELL_Y]++ def test_fewer_next_cells(self) -> None:+ """Test with fewer next cells than previous."""+ result = _match_cell_ids_by_similarity(+ prev_ids=[CELL_A, CELL_B, CELL_C],+ prev_codes=["abc", "def", "ghi"],+ next_ids=[UNUSED_A, UNUSED_B],+ next_codes=["abc", "ghi"],+ )+ assert len(result) == 2+ assert result == [CELL_A, CELL_C]++ def test_more_next_cells(self) -> None:+ """Test with more next cells than previous."""+ result = _match_cell_ids_by_similarity(+ prev_ids=[CELL_A, CELL_B],+ prev_codes=["abc", "def"],+ next_ids=[CELL_A, CELL_B, CELL_C],+ next_codes=["def", "ghi", "abc"],+ )+ assert len(result) == 3+ assert result == [CELL_B, CELL_C, CELL_A]++ def test_completely_different_codes(self) -> None:+ """Test with completely different codes."""+ result = _match_cell_ids_by_similarity(+ prev_ids=[CELL_A, CELL_B],+ prev_codes=["abc", "def"],+ next_ids=[UNUSED_A, UNUSED_B],+ next_codes=["xyz", "123"],+ )+ assert len(result) == 2++ def test_empty_lists(self) -> None:+ """Test with empty lists."""+ assert _match_cell_ids_by_similarity([], [], [], []) == []++ def test_empty_strings(self) -> None:+ """Test with empty strings."""+ assert _match_cell_ids_by_similarity(+ prev_ids=[CELL_A],+ prev_codes=[""],+ next_ids=[UNUSED_A],+ next_codes=[""],+ ) == [CELL_A]+++class TestCellMatchingEdgeCases:+ """Test class for edge cases in cell matching."""++ def test_multiple_identical_codes_in_prev(self) -> None:+ """Test with multiple identical codes in previous cells."""+ result = _match_cell_ids_by_similarity(+ prev_ids=[CELL_A, CELL_B, CELL_C],+ prev_codes=["same", "same", "diff"],+ next_ids=[CELL_X, CELL_Y],+ next_codes=["same", "diff"],+ )+ assert len(result) == 2+ assert result[0] in [CELL_A, CELL_B]+ assert result[1] == CELL_C++ def test_multiple_identical_codes_in_next(self) -> None:+ """Test with multiple identical codes in next cells."""+ result = _match_cell_ids_by_similarity(+ prev_ids=[CELL_A, CELL_B],+ prev_codes=["code1", "code2"],+ next_ids=[CELL_X, CELL_Y, CELL_Z],+ next_codes=["code1", "code1", "code2"],+ )+ assert len(result) == 3+ assert result == [CELL_A, CELL_Y, CELL_B]++ def test_very_long_common_prefixes_suffixes(self) -> None:+ """Test with very long common prefixes/suffixes."""+ long_prefix = "x" * 1000+ result = _match_cell_ids_by_similarity(+ prev_ids=[CELL_A, CELL_B],+ prev_codes=[long_prefix + "1", long_prefix + "2"],+ next_ids=[CELL_X, CELL_Y],+ next_codes=[long_prefix + "2", long_prefix + "1"],+ )+ assert result == [CELL_B, CELL_A]++ def test_unicode_and_special_characters(self) -> None:+ """Test with Unicode and special characters."""+ result = _match_cell_ids_by_similarity(+ prev_ids=[CELL_A, CELL_B, CELL_C],+ prev_codes=["🔥", "∑∫", "\n\t\r"],+ next_ids=[CELL_X, CELL_Y, CELL_Z],+ next_codes=["∑∫", "🔥", "\n\t\r"],+ )+ assert result == [CELL_B, CELL_A, CELL_C]++ def test_mixed_case_sensitivity(self) -> None:+ """Test with mixed case sensitivity."""+ result = _match_cell_ids_by_similarity(+ prev_ids=[CELL_A, CELL_B],+ prev_codes=["ABC", "def"],+ next_ids=[CELL_X, CELL_Y],+ next_codes=["abc", "DEF"],+ )+ assert result == [CELL_A, CELL_B]++ def test_whitespace_variations(self) -> None:+ """Test with whitespace variations."""+ result = _match_cell_ids_by_similarity(+ prev_ids=[CELL_A, CELL_B],+ prev_codes=["x y", "a\nb"],+ next_ids=[CELL_X, CELL_Y],+ next_codes=["x y", "a b"],+ )+ assert result == [CELL_A, CELL_B]++ def test_all_codes_being_substrings(self) -> None:+ """Test with all codes being substrings of each other."""+ result = _match_cell_ids_by_similarity(+ prev_ids=[CELL_A, CELL_B, CELL_C],+ prev_codes=["x", "xy", "xyz"],+ next_ids=[CELL_P, CELL_Q, CELL_R],+ next_codes=["xyz", "xy", "x"],+ )+ assert result == [CELL_C, CELL_B, CELL_A]++ def test_maximum_length_differences(self) -> None:+ """Test with maximum length differences."""+ result = _match_cell_ids_by_similarity(+ prev_ids=[CELL_A, CELL_B],+ prev_codes=["x", "x" * 10000],+ next_ids=[CELL_Y, CELL_Z],+ next_codes=["x" * 10000, "x"],+ )+ assert result == [CELL_B, CELL_A]++ def test_empty_strings_edge_case(self) -> None:+ """Test with empty strings edge case."""+ result = _match_cell_ids_by_similarity(+ prev_ids=[CELL_A, CELL_B],+ prev_codes=["", "x"],+ next_ids=[CELL_Y, CELL_Z],+ next_codes=["x", ""],+ )+ assert result == [CELL_B, CELL_A]++ def test_identical_codes(self) -> None:+ """Test with identical codes."""+ result = _match_cell_ids_by_similarity(+ prev_ids=[CELL_A, CELL_B, CELL_C],+ prev_codes=["same", "same", "same"],+ next_ids=[CELL_X, CELL_Y, CELL_Z],+ next_codes=["same", "same", "same"],+ )+ assert len(result) == 3+ assert set(result) == {CELL_A, CELL_B, CELL_C}++ def test_completely_different_codes_edge_case(self) -> None:+ """Test with completely different codes edge case."""+ result = _match_cell_ids_by_similarity(+ prev_ids=[CELL_A, CELL_B],+ prev_codes=["abc", "def"],+ next_ids=[CELL_X, CELL_Y],+ next_codes=["123", "456"],+ )+ assert len(result) == 2+ assert result == [CELL_A, CELL_B]++ def test_special_python_syntax(self) -> None:+ """Test with special Python syntax."""+ result = _match_cell_ids_by_similarity(+ prev_ids=[CELL_A, CELL_B],+ prev_codes=["def foo():", "class Bar:"],+ next_ids=[CELL_X, CELL_Y],+ next_codes=["class Bar:", "def foo():"],+ )+ assert result == [CELL_B, CELL_A]++ def test_similar_reduction(self) -> None:+ """test with similar and reduction."""+ result = _match_cell_ids_by_similarity(+ prev_ids=[CELL_A, CELL_B],+ prev_codes=["y = x + 1", "x = 1"],+ next_ids=[CELL_A],+ next_codes=["y = 2 + 1"],+ )+ assert len(result) == 1+ assert result == [CELL_A]+++class TestSortCellIdsBySimilarity:+ """Test class for sorting cell IDs by similarity."""++ def test_simple_reorder(self) -> None:+ """Test simple reorder."""+ prev_manager = CellManager()+ prev_manager.register_cell(CELL_A, "code1", CellConfig())+ prev_manager.register_cell(CELL_B, "code2", CellConfig())++ curr_manager = CellManager()+ curr_manager.register_cell(CELL_X, "code2", CellConfig())+ curr_manager.register_cell(CELL_Y, "code1", CellConfig())++ # Save original seen_ids+ original_seen_ids = curr_manager.seen_ids.copy()++ curr_manager.sort_cell_ids_by_similarity(prev_manager)+ assert list(curr_manager.cell_ids()) == [CELL_B, CELL_A]+ assert curr_manager.cell_data_at(CELL_B).code == "code2"+ assert curr_manager.cell_data_at(CELL_A).code == "code1"++ # Check seen_ids were updated+ assert curr_manager.seen_ids == original_seen_ids | {CELL_A, CELL_B}++ def test_reorder_same_ids(self) -> None:+ """Test reorder with same IDs."""+ prev_manager = CellManager()+ prev_manager.register_cell(CELL_A, "code1", CellConfig())+ prev_manager.register_cell(CELL_B, "code2", CellConfig())++ curr_manager = CellManager()+ curr_manager.register_cell(CELL_A, "code2", CellConfig())+ curr_manager.register_cell(CELL_B, "code1", CellConfig())++ # Save original seen_ids+ original_seen_ids = curr_manager.seen_ids.copy()++ curr_manager.sort_cell_ids_by_similarity(prev_manager)+ assert list(curr_manager.cell_ids()) == [CELL_B, CELL_A]+ assert curr_manager.cell_data_at(CELL_B).code == "code2"+ assert curr_manager.cell_data_at(CELL_A).code == "code1"++ # Check seen_ids were updated+ assert curr_manager.seen_ids == original_seen_ids | {CELL_A, CELL_B}++ def test_less_cells(self) -> None:+ """Test with fewer cells than before."""+ prev_manager = CellManager()+ prev_manager.register_cell(CELL_A, "code1", CellConfig())+ prev_manager.register_cell(CELL_B, "code2", CellConfig())+ prev_manager.register_cell(CELL_C, "code3", CellConfig())++ curr_manager = CellManager()+ curr_manager.register_cell(CELL_X, "code2", CellConfig())+ curr_manager.register_cell(CELL_Y, "code1", CellConfig())++ original_seen_ids = curr_manager.seen_ids.copy()++ curr_manager.sort_cell_ids_by_similarity(prev_manager)+ assert list(curr_manager.cell_ids()) == [CELL_B, CELL_A]+ assert curr_manager.seen_ids == original_seen_ids | {CELL_A, CELL_B}+ assert curr_manager.cell_data_at(CELL_B).code == "code2"+ assert curr_manager.cell_data_at(CELL_A).code == "code1"++ def test_more_cells(self) -> None:+ """Test with more cells than before."""+ prev_manager = CellManager()+ prev_manager.register_cell(CELL_A, "code1", CellConfig())+ prev_manager.register_cell(CELL_B, "code2", CellConfig())++ curr_manager = CellManager()+ curr_manager.register_cell(CELL_X, "code2", CellConfig())+ curr_manager.register_cell(CELL_Y, "code1", CellConfig())+ curr_manager.register_cell(CELL_Z, "code3", CellConfig())++ original_seen_ids = curr_manager.seen_ids.copy()++ curr_manager.sort_cell_ids_by_similarity(prev_manager)+ assert list(curr_manager.cell_ids()) == [CELL_B, CELL_A, CELL_Z]+ assert curr_manager.seen_ids == original_seen_ids | {+ CELL_A,+ CELL_B,+ CELL_Z,+ }+ assert curr_manager.cell_data_at(CELL_B).code == "code2"+ assert curr_manager.cell_data_at(CELL_A).code == "code1"+ assert curr_manager.cell_data_at(CELL_Z).code == "code3"++ def test_similar_reduction(self) -> None:+ # Same as above but using CellManager+ prev_manager = CellManager()+ prev_manager.register_cell(CELL_A, "y = x + 1", CellConfig())+ prev_manager.register_cell(CELL_B, "x = 1", CellConfig())+ curr_manager = CellManager()+ curr_manager.register_cell(CELL_A, "y = 2 + 1", CellConfig())+ original_seen_ids = curr_manager.seen_ids.copy()+ curr_manager.sort_cell_ids_by_similarity(prev_manager)+ assert list(curr_manager.cell_ids()) == [CELL_A]+ assert curr_manager.seen_ids == {CELL_A}+ assert curr_manager.cell_data_at(CELL_A).code == "y = 2 + 1"diff --git a/tests/_server/test_file_manager.py b/tests/_server/test_file_manager.pyindex b48b9c0bc8..8cae82003d 100644--- a/tests/_server/test_file_manager.py+++ b/tests/_server/test_file_manager.py@@ -14,6 +14,7 @@ from marimo._server.api.status import HTTPException, HTTPStatusfrom marimo._server.file_manager import AppFileManagerfrom marimo._server.models.models import SaveNotebookRequestfrom marimo._types.ids import CellId_t+from marimo._utils.cell_matching import similarity_scoreif TYPE_CHECKING:from collections.abc import Generator@@ -612,20 +613,33 @@ if __name__ == "__main__":""")+ # The result is that cell1 is removed since similarity_score is closer to+ # cell2+ # NB. Lower is better for similarity_score+ new_code = "y = 2 + 1"+ assert similarity_score(+ manager.app.cell_manager.get_cell_code(cell_one), new_code+ ) > similarity_score(+ manager.app.cell_manager.get_cell_code(cell_two), new_code+ )+# Reload the appchanged_cell_ids = manager.reload()- assert changed_cell_ids == {cell_one}+ assert changed_cell_ids == {cell_two}# Verify cell_manager has only one cellcell_ids = list(manager.app.cell_manager.cell_ids())- assert cell_ids == [cell_one]+ assert cell_ids == [cell_two]# Verify graph has only one cellgraph_ids = list(manager.app.graph.cells)- assert graph_ids == [cell_one]+ assert graph_ids == [cell_two]# Check that the graph was updated- assert manager.app.graph.get_defining_cells("y") == {cell_one}+ assert manager.app.graph.get_defining_cells("y") == {cell_two}++ # Check the contents of the cell+ assert manager.app.cell_manager.get_cell_code(cell_two) == new_code# Clean upos.remove(tmp_file)