# Test icon mapping
assert FILE_TYPE_ICONS[FileType.CODE] == "file-code"
assert FILE_TYPE_ICONS[FileType.AUDIO] == "file-music"
assert BROWSER_ICONS["folder"] == "folder"
print("Icon mapping tests passed!")Icon mapping tests passed!
Maps file types to Lucide icon names for consistent iconography.
from fasthtml.common import to_xml
# Test icon rendering - verify icons are generated without errors
folder = FileInfo(name="test", path="/test", is_directory=True)
icon = _get_file_icon(folder)
html = to_xml(icon)
assert "<svg" in html.lower() # SVG element present
assert "text-warning" in html # Folder color class
py_file = FileInfo(name="test.py", path="/test.py", is_directory=False, file_type=FileType.CODE)
icon = _get_file_icon(py_file)
html = to_xml(icon)
assert "<svg" in html.lower()
assert "text-info" in html # Code file color class
print("Icon rendering tests passed!")Icon rendering tests passed!
The create_file_cell_renderer factory returns a render_cell callback for the virtual collection. The callback dispatches on ctx.column.key to render each column’s content.
def create_file_cell_renderer(
config:FileBrowserConfig, # Browser config (for selection state access)
get_selection:Callable, # () -> BrowserSelection, returns current selection
select_url:str='', # URL for checkbox click selection route
)->Callable: # render_cell(item: FileInfo, ctx: CellRenderContext) -> Any
Create a render_cell callback for the virtual collection.
Returns a closure that renders cell content based on column key.
from cjm_fasthtml_virtual_collection.core.models import ColumnDef
from cjm_fasthtml_file_browser.core.models import BrowserSelection
config = FileBrowserConfig()
selection = BrowserSelection()
render_cell = create_file_cell_renderer(config, lambda: selection, select_url="/sel")
# Test name column
name_col = ColumnDef(key="name", header="Name")
folder = FileInfo(name="Documents", path="/docs", is_directory=True)
ctx = CellRenderContext(row_index=0, column=name_col, total_items=5)
html = to_xml(render_cell(folder, ctx))
assert "Documents" in html
assert "<svg" in html.lower()
# Test size column
size_col = ColumnDef(key="size", header="Size")
f = FileInfo(name="a.txt", path="/a.txt", is_directory=False, size=1024)
ctx = CellRenderContext(row_index=0, column=size_col, total_items=5)
html = to_xml(render_cell(f, ctx))
assert "1.0 KB" in html or "1 KB" in html
# Test select column — file is selectable
sel_col = ColumnDef(key="select", header="")
ctx = CellRenderContext(row_index=0, column=sel_col, total_items=5)
html = to_xml(render_cell(f, ctx))
assert "checkbox" in html
assert "/sel" in html
# Test select column — folder not selectable (default config: files only)
html = to_xml(render_cell(folder, ctx))
assert "checkbox" not in html
# Test modified column
mod_col = ColumnDef(key="modified", header="Modified")
ctx = CellRenderContext(row_index=0, column=mod_col, total_items=5)
html = to_xml(render_cell(f, ctx))
assert isinstance(html, str)
print("Cell render callback tests passed!")Cell render callback tests passed!
Components displayed by the virtual collection when a directory is empty or inaccessible.
Render error state for when directory access fails.
Render empty state for when a directory has no matching items.
# Test empty state
empty = render_empty_state("This folder is empty")
html = to_xml(empty)
assert "This folder is empty" in html
assert "<svg" in html.lower()
# Test error state
error = render_error_state("Permission denied")
html = to_xml(error)
assert "Permission denied" in html
assert "text-error" in html
print("Empty/error state tests passed!")