import { assert, describe, it } from "@effect/vitest"; import { MemoryFileSystem } from "@effected/memfs"; import { SchemastoreVersion, defineConfig, isSchemastoreConfig } from "@effected/schemastore"; import { Effect, Layer, Option, Path, Schema } from "effect"; import { ConfigLoadError, ConfigLoader, ConfigNotFoundError } from "../../src/schema/ConfigLoader.js"; const platform = (seed: Record) => Layer.mergeAll(MemoryFileSystem.layerWith(seed), Path.layer); const Config = Schema.Struct({ name: Schema.String }); const config = defineConfig({ name: "test", outputDir: "schemas", baseUrl: "https://x/s", schemas: { a: { schema: Config, versions: ["1.0", "1.1"], catalog: { description: "a", fileMatch: ["a.json"] } }, }, }); describe("ConfigLoader.discover", () => { it.effect("finds schemastore.config.ts in a parent directory", () => Effect.gen(function* () { const found = yield* ConfigLoader.discover("/repo/packages/x/src"); assert.strictEqual(found, "/repo/schemastore.config.ts"); }).pipe(Effect.provide(platform({ "/repo/schemastore.config.ts": "", "/repo/packages/x/src/index.ts": "" }))), ); it.effect("prefers the nearest directory and the first name in CONFIG_NAMES", () => Effect.gen(function* () { const found = yield* ConfigLoader.discover("/repo/packages/x"); assert.strictEqual(found, "/repo/packages/x/schemastore.config.mjs"); }).pipe( Effect.provide(platform({ "/repo/schemastore.config.ts": "", "/repo/packages/x/schemastore.config.mjs": "" })), ), ); it.effect("prefers .ts over .mjs in the same directory", () => Effect.gen(function* () { const found = yield* ConfigLoader.discover("/repo"); assert.strictEqual(found, "/repo/schemastore.config.ts"); }).pipe(Effect.provide(platform({ "/repo/schemastore.config.mjs": "", "/repo/schemastore.config.ts": "" }))), ); it.effect("a ceiling stops the walk after checking its own directory", () => Effect.gen(function* () { const error = yield* Effect.flip(ConfigLoader.discover("/top/repo/a", "/top/repo")); assert.instanceOf(error, ConfigNotFoundError); assert.deepStrictEqual(error.searched, ["/top/repo/a", "/top/repo"]); }).pipe(Effect.provide(platform({ "/top/schemastore.config.ts": "", "/top/repo/a/.keep": "" }))), ); it.effect("a config in the ceiling directory is found", () => Effect.gen(function* () { assert.strictEqual(yield* ConfigLoader.discover("/top/repo/a", "/top/repo"), "/top/repo/schemastore.config.ts"); }).pipe( Effect.provide( platform({ "/top/schemastore.config.ts": "", "/top/repo/schemastore.config.ts": "", "/top/repo/a/.keep": "", }), ), ), ); it.effect("a ceiling reached through a link still stops the walk", () => Effect.gen(function* () { const error = yield* Effect.flip(ConfigLoader.discover("/top/link/a", "/top/repo")); assert.deepStrictEqual(error.searched, ["/top/link/a", "/top/link"]); }).pipe( Effect.provide( Layer.mergeAll( MemoryFileSystem.layerWith({ "/schemastore.config.ts": "", "/top/schemastore.config.ts": "", "/top/repo/a/.keep": "", "/top/link": MemoryFileSystem.symlink("/top/repo"), }), Path.layer, ), ), ), ); it.effect("a start outside the ceiling searches nothing, rather than walking unbounded", () => Effect.gen(function* () { const error = yield* Effect.flip(ConfigLoader.discover("/other/a", "/top/repo")); assert.deepStrictEqual(error.searched, []); }).pipe( Effect.provide(platform({ "/other/schemastore.config.ts": "", "/other/a/.keep": "", "/top/repo/.keep": "" })), ), ); it.effect("load carries the ceiling to discovery", () => Effect.gen(function* () { const error = yield* Effect.flip( ConfigLoader.load({ cwd: "/top/repo", ceiling: "/top/repo", importModule: () => Promise.resolve({ default: config }), }), ); assert.instanceOf(error, ConfigNotFoundError); }).pipe(Effect.provide(platform({ "/top/schemastore.config.ts": "", "/top/repo/.keep": "" }))), ); it.effect("fails typed with the directories searched", () => Effect.gen(function* () { const error = yield* Effect.flip(ConfigLoader.discover("/repo/a/b")); assert.instanceOf(error, ConfigNotFoundError); assert.deepStrictEqual(error.searched, ["/repo/a/b", "/repo/a", "/repo", "/"]); assert.include(error.message, "schemastore.config.ts"); assert.include(error.message, "/repo/a/b"); }).pipe(Effect.provide(platform({ "/repo/a/b/.keep": "" }))), ); }); describe("ConfigLoader.load", () => { it.effect( "uses the explicit path, imports the module, and resolves relative paths against the config directory", () => Effect.gen(function* () { const loaded = yield* ConfigLoader.load({ explicit: "lib/scripts/schemastore.config.ts", cwd: "/repo", importModule: () => Promise.resolve({ default: config }), }); assert.strictEqual(loaded.path, "/repo/lib/scripts/schemastore.config.ts"); assert.strictEqual(loaded.directory, "/repo/lib/scripts"); assert.strictEqual(loaded.config.outputDir, "/repo/lib/scripts/schemas"); assert.strictEqual(loaded.config.catalogDir, "/repo/lib/scripts/schemas/catalogs"); assert.strictEqual(loaded.config.schemas[0]?.target.path, "/repo/lib/scripts/schemas/1.1/a-1.1.json"); assert.strictEqual(loaded.config.schemas[0]?.frozen[0]?.path, "/repo/lib/scripts/schemas/1.0/a-1.0.json"); assert.isTrue(isSchemastoreConfig(loaded.config)); }).pipe(Effect.provide(platform({ "/repo/lib/scripts/schemastore.config.ts": "" }))), ); it.effect("leaves an absolute outputDir alone", () => Effect.gen(function* () { const absoluteConfig = defineConfig({ name: "test", outputDir: "/abs/schemas", baseUrl: "https://x/s", schemas: { a: { schema: Config } }, }); const loaded = yield* ConfigLoader.load({ cwd: "/repo/packages/x", importModule: () => Promise.resolve({ default: absoluteConfig }), }); assert.strictEqual(loaded.config.outputDir, "/abs/schemas"); assert.strictEqual(loaded.config.schemas[0]?.target.path, "/abs/schemas/a.json"); }).pipe(Effect.provide(platform({ "/repo/schemastore.config.ts": "", "/repo/packages/x/.keep": "" }))), ); it.effect("accepts a module whose namespace is the config itself (interopDefault)", () => Effect.gen(function* () { const loaded = yield* ConfigLoader.load({ explicit: "schemastore.config.ts", cwd: "/repo", importModule: () => Promise.resolve(config), }); assert.strictEqual(loaded.config.schemas.length, 1); }).pipe(Effect.provide(platform({ "/repo/schemastore.config.ts": "" }))), ); it.effect("fails typed when the default export is not a defineConfig value", () => Effect.gen(function* () { const error = yield* Effect.flip( ConfigLoader.load({ explicit: "schemastore.config.ts", cwd: "/repo", importModule: () => Promise.resolve({ default: { schemas: [] } }), }), ); assert.instanceOf(error, ConfigLoadError); assert.strictEqual(error.path, "/repo/schemastore.config.ts"); assert.include(error.reason, "defineSchemastore(...) value from @effected/plugin"); assert.include(error.reason, "defineConfig(...) value from @effected/schemastore"); }).pipe(Effect.provide(platform({ "/repo/schemastore.config.ts": "" }))), ); it.effect("fails typed when outputDir is not a string (forged brand)", () => Effect.gen(function* () { const forged = Object.assign({}, config, { outputDir: 123 }) as unknown as typeof config; const error = yield* Effect.flip( ConfigLoader.load({ explicit: "schemastore.config.js", cwd: "/repo", importModule: () => Promise.resolve({ default: forged }), }), ); assert.instanceOf(error, ConfigLoadError); assert.strictEqual(error.reason, 'Expected string at ["outputDir"]'); }).pipe(Effect.provide(platform({ "/repo/schemastore.config.js": "" }))), ); // A forged brand skips defineConfig, so every field the loader and the // Runner dereference is re-checked; each case is one field wrong. const loadForgedValue = (forged: unknown) => Effect.flip( ConfigLoader.load({ explicit: "schemastore.config.js", cwd: "/repo", importModule: () => Promise.resolve({ default: forged as typeof config }), }), ); const loadForged = (overrides: Record) => loadForgedValue(Object.assign({}, config, overrides)); it.effect("fails typed when catalogDir is not a string (forged brand)", () => Effect.gen(function* () { const error = yield* loadForged({ catalogDir: 123 }); assert.instanceOf(error, ConfigLoadError); assert.strictEqual(error.reason, 'Expected string at ["catalogDir"]'); }).pipe(Effect.provide(platform({ "/repo/schemastore.config.js": "" }))), ); it.effect("fails typed when name is missing or not a simple file base name (forged brand)", () => Effect.gen(function* () { for (const name of [undefined, 123]) { const error = yield* loadForged({ name }); assert.instanceOf(error, ConfigLoadError); assert.strictEqual(error.reason, 'Expected string at ["name"]', String(name)); } for (const name of ["", "a/b", "a b"]) { const error = yield* loadForged({ name }); assert.instanceOf(error, ConfigLoadError); assert.strictEqual(error.reason, 'not a simple file base name at ["name"]', name); } }).pipe(Effect.provide(platform({ "/repo/schemastore.config.js": "" }))), ); it.effect("fails typed when catalogDir resolves to outputDir (forged brand)", () => Effect.gen(function* () { const error = yield* loadForged({ catalogDir: "/repo/schemas" }); assert.instanceOf(error, ConfigLoadError); assert.match(error.reason, /^catalogDir "\/repo\/schemas" must not be outputDir/); }).pipe(Effect.provide(platform({ "/repo/schemastore.config.js": "" }))), ); it.effect("fails typed when catalogDir resolves to the merged catalog's path (forged brand)", () => Effect.gen(function* () { const error = yield* loadForged({ catalogDir: "/repo/elsewhere/catalog.json" }); assert.instanceOf(error, ConfigLoadError); assert.strictEqual( error.reason, 'catalogDir "/repo/elsewhere/catalog.json" must not be the merged catalog\'s path (catalog.json in its parent)', ); }).pipe(Effect.provide(platform({ "/repo/schemastore.config.js": "" }))), ); it.effect("fails typed when a document resolves into catalogDir (forged brand)", () => Effect.gen(function* () { const error = yield* loadForged({ catalogDir: "/repo/schemas/1.1" }); assert.instanceOf(error, ConfigLoadError); assert.strictEqual( error.reason, 'output path "/repo/schemas/1.1/a-1.1.json" sits in catalogDir "/repo/schemas/1.1", which holds only catalog slices', ); }).pipe(Effect.provide(platform({ "/repo/schemastore.config.js": "" }))), ); it.effect("fails typed when schemas is not an array (forged brand)", () => Effect.gen(function* () { const forged = Object.assign({}, config, { schemas: { length: 1 } }) as unknown as typeof config; const error = yield* Effect.flip( ConfigLoader.load({ explicit: "schemastore.config.js", cwd: "/repo", importModule: () => Promise.resolve({ default: forged }), }), ); assert.instanceOf(error, ConfigLoadError); assert.strictEqual(error.reason, 'Expected array at ["schemas"]'); }).pipe(Effect.provide(platform({ "/repo/schemastore.config.js": "" }))), ); it.effect("fails typed when a schemas element is not a resolved schema (forged brand)", () => Effect.gen(function* () { const forged = Object.assign({}, config, { schemas: [Object.assign({}, config.schemas[0], { name: 123 })], }) as unknown as typeof config; const error = yield* Effect.flip( ConfigLoader.load({ explicit: "schemastore.config.js", cwd: "/repo", importModule: () => Promise.resolve({ default: forged }), }), ); assert.instanceOf(error, ConfigLoadError); assert.strictEqual(error.reason, 'Expected string at ["schemas"][0]["name"]'); }).pipe(Effect.provide(platform({ "/repo/schemastore.config.js": "" }))), ); it.effect("fails typed when a schemas element's target is not a SchemaTarget (forged brand)", () => Effect.gen(function* () { const forged = Object.assign({}, config, { schemas: [{ ...config.schemas[0], target: { $id: "x" } }], }) as unknown as typeof config; const error = yield* Effect.flip( ConfigLoader.load({ explicit: "schemastore.config.js", cwd: "/repo", importModule: () => Promise.resolve({ default: forged }), }), ); assert.instanceOf(error, ConfigLoadError); // errors: "all" — every missing target field is named, not just the first. assert.include(error.reason, 'Missing key at ["schemas"][0]["target"]["schema"]'); assert.include(error.reason, 'Missing key at ["schemas"][0]["target"]["path"]'); assert.include(error.reason, 'Missing key at ["schemas"][0]["target"]["published"]'); }).pipe(Effect.provide(platform({ "/repo/schemastore.config.js": "" }))), ); it.effect("fails typed when a frozen version is not a frozen version (forged brand)", () => Effect.gen(function* () { const forged = Object.assign({}, config, { schemas: [{ ...config.schemas[0], frozen: [{ version: "1.0" }] }], }) as unknown as typeof config; const error = yield* Effect.flip( ConfigLoader.load({ explicit: "schemastore.config.js", cwd: "/repo", importModule: () => Promise.resolve({ default: forged }), }), ); assert.instanceOf(error, ConfigLoadError); assert.include(error.reason, 'Missing key at ["schemas"][0]["frozen"][0]["path"]'); assert.include(error.reason, 'Missing key at ["schemas"][0]["frozen"][0]["url"]'); }).pipe(Effect.provide(platform({ "/repo/schemastore.config.js": "" }))), ); it.effect("fails typed when a schemas element's catalog is not a catalog entry (forged null)", () => Effect.gen(function* () { const forged = Object.assign({}, config, { schemas: [{ ...config.schemas[0], catalog: null }], }) as unknown as typeof config; const error = yield* Effect.flip( ConfigLoader.load({ explicit: "schemastore.config.js", cwd: "/repo", importModule: () => Promise.resolve({ default: forged }), }), ); assert.instanceOf(error, ConfigLoadError); assert.strictEqual(error.reason, 'Expected object at ["schemas"][0]["catalog"]'); }).pipe(Effect.provide(platform({ "/repo/schemastore.config.js": "" }))), ); it.effect("fails typed when a schemas element's catalog is missing fields (forged brand)", () => Effect.gen(function* () { const forged = Object.assign({}, config, { schemas: [{ ...config.schemas[0], catalog: { name: "a" } }], }) as unknown as typeof config; const error = yield* Effect.flip( ConfigLoader.load({ explicit: "schemastore.config.js", cwd: "/repo", importModule: () => Promise.resolve({ default: forged }), }), ); assert.instanceOf(error, ConfigLoadError); assert.include(error.reason, 'Missing key at ["schemas"][0]["catalog"]["description"]'); assert.include(error.reason, 'Missing key at ["schemas"][0]["catalog"]["url"]'); }).pipe(Effect.provide(platform({ "/repo/schemastore.config.js": "" }))), ); // The cases the field-by-field checker let through or never looked at. const schemaWith = (overrides: Record) => ({ schemas: [Object.assign({}, config.schemas[0], overrides)], }); it.effect("reports every fault at once, not only the first (errors: all)", () => Effect.gen(function* () { const error = yield* loadForged({ outputDir: 1, schemas: [{ ...config.schemas[0], frozen: [{ ...config.schemas[0]?.frozen[0], $id: 2 }] }], }); assert.instanceOf(error, ConfigLoadError); assert.include(error.reason, 'Expected string at ["outputDir"]'); // The non-first fault: a decode stopping at the first issue never reports it. assert.include(error.reason, 'Expected string at ["schemas"][0]["frozen"][0]["$id"]'); }).pipe(Effect.provide(platform({ "/repo/schemastore.config.js": "" }))), ); it.effect("fails typed when onDrift is absent — the drift gate must not switch off silently", () => Effect.gen(function* () { // The key is deleted, not set to undefined: the absent-key path. const { onDrift: _omitted, ...withoutOnDrift } = config; assert.isFalse(Object.hasOwn(withoutOnDrift, "onDrift")); const missing = yield* loadForgedValue(withoutOnDrift); assert.instanceOf(missing, ConfigLoadError); assert.include(missing.reason, 'Missing key at ["onDrift"]'); }).pipe(Effect.provide(platform({ "/repo/schemastore.config.js": "" }))), ); it.effect("fails typed when onDrift is present but undefined or bogus", () => Effect.gen(function* () { const undefinedValue = yield* loadForged({ onDrift: undefined }); assert.instanceOf(undefinedValue, ConfigLoadError); assert.include(undefinedValue.reason, '["onDrift"]'); const bogus = yield* loadForged({ onDrift: "ignore" }); assert.instanceOf(bogus, ConfigLoadError); assert.include(bogus.reason, '["onDrift"]'); }).pipe(Effect.provide(platform({ "/repo/schemastore.config.js": "" }))), ); it.effect("fails typed when a schema's drift is not a drift tolerance", () => Effect.gen(function* () { const error = yield* loadForged(schemaWith({ drift: "loose" })); assert.instanceOf(error, ConfigLoadError); assert.include(error.reason, 'at ["schemas"][0]["drift"]'); }).pipe(Effect.provide(platform({ "/repo/schemastore.config.js": "" }))), ); it.effect("rejects a PRESENT catalog: undefined — only a forged config can carry one", () => Effect.gen(function* () { const error = yield* loadForged(schemaWith({ catalog: undefined })); assert.instanceOf(error, ConfigLoadError); assert.include(error.reason, '["schemas"][0]["catalog"]'); }).pipe(Effect.provide(platform({ "/repo/schemastore.config.js": "" }))), ); it.effect("rejects a catalog whose fileMatch holds a non-string", () => Effect.gen(function* () { const catalog = { name: "a", description: "a", fileMatch: [1], url: "https://x/s/a.json" }; const error = yield* loadForged(schemaWith({ catalog })); assert.instanceOf(error, ConfigLoadError); assert.strictEqual(error.reason, 'Expected string at ["schemas"][0]["catalog"]["fileMatch"][0]'); }).pipe(Effect.provide(platform({ "/repo/schemastore.config.js": "" }))), ); it.effect("rejects a catalog versions record holding a non-string URL", () => Effect.gen(function* () { // The old field-by-field checker never looked at versions; the // CatalogEntry decode does. const catalog = { name: "a", description: "a", fileMatch: ["a.json"], url: "https://x/s/a.json", versions: { "1": 1 }, }; const error = yield* loadForged(schemaWith({ catalog })); assert.instanceOf(error, ConfigLoadError); assert.strictEqual(error.reason, 'Expected string at ["schemas"][0]["catalog"]["versions"]["1"]'); }).pipe(Effect.provide(platform({ "/repo/schemastore.config.js": "" }))), ); it.effect("rejects a target.schema that is not an Effect Schema", () => Effect.gen(function* () { const error = yield* loadForged(schemaWith({ target: { ...config.schemas[0]?.target, schema: "Config" } })); assert.instanceOf(error, ConfigLoadError); assert.strictEqual(error.reason, 'Expected an Effect Schema at ["schemas"][0]["target"]["schema"]'); }).pipe(Effect.provide(platform({ "/repo/schemastore.config.js": "" }))), ); it.effect("keeps the defineConfig brand after resolvePaths", () => Effect.gen(function* () { const loaded = yield* ConfigLoader.load({ explicit: "schemastore.config.ts", cwd: "/repo", importModule: () => Promise.resolve({ default: config }), }); assert.isTrue(isSchemastoreConfig(loaded.config)); }).pipe(Effect.provide(platform({ "/repo/schemastore.config.ts": "" }))), ); it.effect("keeps the stamp of the copy that made the config through resolvePaths", () => Effect.gen(function* () { const stamped = Object.defineProperty({ ...config }, Symbol.for("@effected/schemastore/version"), { value: "9.9.9", }); const loaded = yield* ConfigLoader.load({ explicit: "schemastore.config.ts", cwd: "/repo", importModule: () => Promise.resolve({ default: stamped }), }); assert.deepStrictEqual(SchemastoreVersion.of(loaded.config), Option.some("9.9.9")); }).pipe(Effect.provide(platform({ "/repo/schemastore.config.ts": "" }))), ); it.effect("leaves a config with no stamp unstamped", () => Effect.gen(function* () { const loaded = yield* ConfigLoader.load({ explicit: "schemastore.config.ts", cwd: "/repo", importModule: () => Promise.resolve({ default: { ...config } }), }); assert.isTrue(Option.isNone(SchemastoreVersion.of(loaded.config))); }).pipe(Effect.provide(platform({ "/repo/schemastore.config.ts": "" }))), ); it.effect("fails typed when two outputs resolve to one absolute path", () => Effect.gen(function* () { // A schema keyed `catalog` derives `schemas/catalog.json`; the forged // absolute catalogDir is lexically unrelated, but its merged catalog // resolves to that same absolute path. const flat = defineConfig({ name: "test", outputDir: "schemas", baseUrl: "https://x/s", catalogDir: "elsewhere/catalogs", schemas: { catalog: { schema: Config } }, }); const colliding = Object.assign({}, flat, { catalogDir: "/repo/schemas/catalogs" }) as unknown as typeof config; const error = yield* Effect.flip( ConfigLoader.load({ explicit: "schemastore.config.ts", cwd: "/repo", importModule: () => Promise.resolve({ default: colliding }), }), ); assert.instanceOf(error, ConfigLoadError); assert.strictEqual(error.reason, 'output path "/repo/schemas/catalog.json" is declared twice after resolution'); }).pipe(Effect.provide(platform({ "/repo/schemastore.config.ts": "" }))), ); it.effect("fails typed when the module throws on import", () => Effect.gen(function* () { const error = yield* Effect.flip( ConfigLoader.load({ explicit: "schemastore.config.ts", cwd: "/repo", importModule: () => Promise.reject(new Error("boom")), }), ); assert.instanceOf(error, ConfigLoadError); assert.match(error.reason, /boom/); }).pipe(Effect.provide(platform({ "/repo/schemastore.config.ts": "" }))), ); it.effect( "reads a rejected import's message through the guarded read, so a cross-realm-like plain { message } renders", () => Effect.gen(function* () { const foreign = { name: "Error", message: "foreign realm import failed" }; assert.isFalse(foreign instanceof Error); const error = yield* Effect.flip( ConfigLoader.load({ explicit: "schemastore.config.ts", cwd: "/repo", importModule: () => Promise.reject(foreign), }), ); assert.instanceOf(error, ConfigLoadError); assert.strictEqual(error.reason, "foreign realm import failed"); }).pipe(Effect.provide(platform({ "/repo/schemastore.config.ts": "" }))), ); it.effect( "prefers a rejected import's stack over an empty message, because the stack names the module that threw", () => Effect.gen(function* () { const empty = new Error(""); empty.stack = "Error\n at /repo/schemastore.config.ts:3:1"; const error = yield* Effect.flip( ConfigLoader.load({ explicit: "schemastore.config.ts", cwd: "/repo", importModule: () => Promise.reject(empty), }), ); assert.instanceOf(error, ConfigLoadError); assert.strictEqual(error.reason, empty.stack); assert.notInclude(error.message, "undefined"); }).pipe(Effect.provide(platform({ "/repo/schemastore.config.ts": "" }))), ); it.effect("falls back to the value's string form when a rejected import carries neither stack nor message", () => Effect.gen(function* () { const error = yield* Effect.flip( ConfigLoader.load({ explicit: "schemastore.config.ts", cwd: "/repo", importModule: () => Promise.reject({ message: " ", stack: "" }), }), ); assert.instanceOf(error, ConfigLoadError); assert.strictEqual(error.reason, "[object Object]"); }).pipe(Effect.provide(platform({ "/repo/schemastore.config.ts": "" }))), ); it.effect("adds the install line to the first line when @effected/plugin cannot be resolved", () => Effect.gen(function* () { for (const text of [ "Cannot find module '@effected/plugin'", "Cannot find package '@effected/plugin' imported from /repo/x.ts", "Cannot find module '@effected/plugin/sub'", ]) { const missing = new Error(text); missing.stack = `Error: ${text}\n at /repo/schemastore.config.ts:1:1`; const error = yield* Effect.flip( ConfigLoader.load({ explicit: "schemastore.config.ts", cwd: "/repo", importModule: () => Promise.reject(missing), }), ); assert.instanceOf(error, ConfigLoadError); const [first, ...rest] = error.reason.split("\n"); assert.include(first ?? "", text); assert.include(first ?? "", "pnpm add -D @effected/plugin"); assert.deepStrictEqual(rest, [" at /repo/schemastore.config.ts:1:1"]); } }).pipe(Effect.provide(platform({ "/repo/schemastore.config.ts": "" }))), ); it.effect("adds the version hint when defineConfig refused a hosted value", () => Effect.gen(function* () { const refused = new Error('defineConfig: schema "a" Expected a HostedSchema at ["hosted"]'); refused.stack = `Error: ${refused.message}\n at /repo/schemastore.config.ts:1:1`; const error = yield* Effect.flip( ConfigLoader.load({ explicit: "schemastore.config.ts", cwd: "/repo", importModule: () => Promise.reject(refused), }), ); assert.instanceOf(error, ConfigLoadError); const [first, ...rest] = error.reason.split("\n"); assert.include(first ?? "", 'Expected a HostedSchema at ["hosted"]'); assert.include(first ?? "", "@effected/schemastore may be older than the tool's"); assert.include(first ?? "", "from @effected/plugin, which is the tool's own copy"); assert.include(first ?? "", "catalog:effected"); assert.deepStrictEqual(rest, [" at /repo/schemastore.config.ts:1:1"]); }).pipe(Effect.provide(platform({ "/repo/schemastore.config.ts": "" }))), ); it.effect("adds no version hint to any other import failure", () => Effect.gen(function* () { const error = yield* Effect.flip( ConfigLoader.load({ explicit: "schemastore.config.ts", cwd: "/repo", importModule: () => Promise.reject({ message: 'defineConfig: Expected string at ["baseUrl"]' }), }), ); assert.instanceOf(error, ConfigLoadError); assert.notInclude(error.reason, "may be older"); }).pipe(Effect.provide(platform({ "/repo/schemastore.config.ts": "" }))), ); it.effect("adds no install line for another package that cannot be resolved", () => Effect.gen(function* () { for (const name of ["@effected/plugin-extra", "left-pad"]) { const error = yield* Effect.flip( ConfigLoader.load({ explicit: "schemastore.config.ts", cwd: "/repo", importModule: () => Promise.reject({ message: `Cannot find module '${name}'` }), }), ); assert.instanceOf(error, ConfigLoadError); assert.notInclude(error.reason, "pnpm add"); } }).pipe(Effect.provide(platform({ "/repo/schemastore.config.ts": "" }))), ); it.effect("fails typed when the default importer cannot read the config", () => Effect.gen(function* () { // A directory wearing the config's name: it exists, and reading it fails. const error = yield* Effect.flip(ConfigLoader.load({ explicit: "schemastore.config.ts", cwd: "/repo" })); assert.instanceOf(error, ConfigLoadError); assert.strictEqual(error.path, "/repo/schemastore.config.ts"); }).pipe(Effect.provide(platform({ "/repo/schemastore.config.ts/.keep": "" }))), ); it.effect("fails ConfigNotFoundError when the explicit path does not exist", () => Effect.gen(function* () { const error = yield* Effect.flip( ConfigLoader.load({ explicit: "nope.ts", cwd: "/repo", importModule: () => Promise.resolve({}) }), ); assert.instanceOf(error, ConfigNotFoundError); assert.deepStrictEqual(error.searched, ["/repo/nope.ts"]); }).pipe(Effect.provide(platform({ "/repo/.keep": "" }))), ); });