///
import {
Cell,
compileAndRun,
computed,
fetchProgram,
NAME,
pattern,
UI,
} from "commontools";
/**
* Test pattern for fetchProgram builtin.
* Fetches a program from a URL and compiles it.
*/
export default pattern(() => {
// URL to a simple pattern file
const url = Cell.of(
"https://raw.githubusercontent.com/commontoolsinc/labs/main/packages/patterns/counter.tsx",
);
// Step 1: Fetch the program from URL
const { pending: fetchPending, result: program, error: fetchError } =
fetchProgram({ url });
// Step 2: Compile and run the fetched program
// Explicitly map program fields to compileAndRun params
const compileParams = computed(() => ({
files: program?.files ?? [],
main: program?.main ?? "",
input: { value: 10 },
}));
const { pending: compilePending, result, error: compileError } =
compileAndRun(compileParams);
return {
[NAME]: "Fetch Program Test",
[UI]: (
Fetch Program Test
{fetchPending &&
Fetching program...
}
{compilePending &&
Compiling...
}
{fetchError &&
Fetch error: {fetchError}
}
{compileError && (
Compile error: {compileError}
)}
{result && (
Successfully compiled recipe! Charm ID: {result}
{computed(() => JSON.stringify(result, null, 2))}
)}
),
url,
program,
result,
};
});