Files
cell/tests/cellfs_test.ce
2026-02-25 17:43:01 -06:00

252 lines
6.2 KiB
Plaintext

// Test: cellfs mounting, sync access, and async requestors
//
// Known limitation:
// - is_directory() uses raw path instead of res.path for fs mounts,
// so @-prefixed paths fail (e.g. '@mount/dir'). Works via stat().
// - cellfs.slurp() has no http code path; use cellfs.get() for http mounts.
var cellfs = use('cellfs')
var fd = use('fd')
var pkg_dir = '.cell/packages/gitea.pockle.world/john/prosperon'
// Mount the prosperon package directory as 'prosperon'
cellfs.mount(pkg_dir, 'prosperon')
// --- exists ---
var found = cellfs.exists('@prosperon/color.cm')
if (!found) {
log.error("exists('@prosperon/color.cm') returned false")
disrupt
}
log.console("exists: ok")
// exists returns false for missing files
var missing = cellfs.exists('@prosperon/no_such_file.cm')
if (missing) {
log.error("exists returned true for missing file")
disrupt
}
log.console("exists (missing): ok")
// --- slurp ---
var data = cellfs.slurp('@prosperon/color.cm')
if (!is_blob(data)) {
log.error("slurp did not return a blob")
disrupt
}
if (length(data) == 0) {
log.error("slurp returned empty blob")
disrupt
}
log.console(`slurp: ok (${length(data)} bits)`)
// --- enumerate ---
var files = cellfs.enumerate('@prosperon', false)
if (!is_array(files)) {
log.error("enumerate did not return an array")
disrupt
}
if (length(files) == 0) {
log.error("enumerate returned empty array")
disrupt
}
// color.cm should be in the listing
var found_color = false
arrfor(files, function(f) {
if (f == 'color.cm') {
found_color = true
return true
}
}, false, true)
if (!found_color) {
log.error("enumerate did not include color.cm")
disrupt
}
log.console(`enumerate: ok (${length(files)} entries, found color.cm)`)
// enumerate recursive
var rfiles = cellfs.enumerate('@prosperon', true)
if (length(rfiles) <= length(files)) {
log.error("recursive enumerate should return more entries")
disrupt
}
log.console(`enumerate recursive: ok (${length(rfiles)} entries)`)
// --- stat ---
var st = cellfs.stat('@prosperon/color.cm')
if (!is_object(st)) {
log.error("stat did not return an object")
disrupt
}
if (st.filesize == null || st.filesize == 0) {
log.error("stat filesize missing or zero")
disrupt
}
log.console(`stat: ok (size=${st.filesize}, mtime=${st.modtime})`)
// stat on a directory
var dir_st = cellfs.stat('@prosperon/docs')
if (!dir_st.isDirectory) {
log.error("stat('@prosperon/docs').isDirectory returned false")
disrupt
}
log.console("stat (directory): ok")
// --- searchpath ---
var sp = cellfs.searchpath()
if (!is_array(sp)) {
log.error("searchpath did not return an array")
disrupt
}
if (length(sp) == 0) {
log.error("searchpath returned empty array")
disrupt
}
log.console(`searchpath: ok (${length(sp)} mounts)`)
// --- resolve ---
var res = cellfs.resolve('@prosperon/color.cm')
if (!is_object(res)) {
log.error("resolve did not return an object")
disrupt
}
if (res.mount.name != 'prosperon') {
log.error("resolve returned wrong mount name")
disrupt
}
if (res.path != 'color.cm') {
log.error("resolve returned wrong path")
disrupt
}
log.console("resolve: ok")
// --- realdir ---
var rd = cellfs.realdir('@prosperon/color.cm')
if (!is_text(rd)) {
log.error("realdir did not return text")
disrupt
}
if (!ends_with(rd, 'color.cm')) {
log.error("realdir does not end with color.cm")
disrupt
}
log.console(`realdir: ok (${rd})`)
// --- unmount and re-mount ---
cellfs.unmount('prosperon')
var after_unmount = cellfs.searchpath()
var unmount_ok = true
arrfor(after_unmount, function(m) {
if (m.name == 'prosperon') {
unmount_ok = false
return true
}
}, false, true)
if (!unmount_ok) {
log.error("unmount failed, mount still present")
disrupt
}
log.console("unmount: ok")
// re-mount for further tests
cellfs.mount(pkg_dir, 'prosperon')
// --- match (wildstar) ---
var m1 = cellfs.match('color.cm', '*.cm')
if (!m1) {
log.error("match('color.cm', '*.cm') returned false")
disrupt
}
var m2 = cellfs.match('color.cm', '*.ce')
if (m2) {
log.error("match('color.cm', '*.ce') returned true")
disrupt
}
log.console("match: ok")
// --- globfs ---
var cm_files = cellfs.globfs(['*.cm'], '@prosperon')
if (!is_array(cm_files)) {
log.error("globfs did not return an array")
disrupt
}
if (length(cm_files) == 0) {
log.error("globfs returned empty array")
disrupt
}
// all results should end in .cm
var all_cm = true
arrfor(cm_files, function(f) {
if (!ends_with(f, '.cm')) {
all_cm = false
return true
}
}, false, true)
if (!all_cm) {
log.error("globfs returned non-.cm files")
disrupt
}
log.console(`globfs: ok (${length(cm_files)} .cm files)`)
log.console("--- sync tests passed ---")
// --- Requestor tests ---
// get requestor for a local fs mount
var get_color = cellfs.get('@prosperon/color.cm')
get_color(function(result, reason) {
if (reason != null) {
log.error(`get color.cm failed: ${reason}`)
disrupt
}
if (!is_blob(result)) {
log.error("get did not return a blob")
disrupt
}
if (length(result) == 0) {
log.error("get returned empty blob")
disrupt
}
log.console(`get (fs): ok (${length(result)} bits)`)
// parallel requestor test - fetch multiple files at once
var get_core = cellfs.get('@prosperon/core.cm')
var get_ease = cellfs.get('@prosperon/ease.cm')
parallel([get_color, get_core, get_ease])(function(results, reason) {
if (reason != null) {
log.error(`parallel get failed: ${reason}`)
disrupt
}
if (length(results) != 3) {
log.error(`parallel expected 3 results, got ${length(results)}`)
disrupt
}
log.console(`parallel get: ok (${length(results)} files fetched)`)
// HTTP mount test — network may not be available in test env
cellfs.mount('http://example.com', 'web')
var web_res = cellfs.resolve('@web/')
if (web_res.mount.type != 'http') {
log.error("http mount type is not 'http'")
disrupt
}
log.console("http mount: ok (type=http)")
var get_web = cellfs.get('@web/')
get_web(function(body, reason) {
if (reason != null) {
log.console(`get (http): skipped (${reason})`)
} else {
log.console(`get (http): ok (${length(body)} bits)`)
}
log.console("--- requestor tests passed ---")
log.console("all cellfs tests passed")
$stop()
})
})
})