# retoor from pathlib import Path from devplacepy.services.jobs.isslop.analysis.engine import _javascript_alias_prefixes from devplacepy.services.jobs.isslop.analysis.signals import FileContext, RepoContext from devplacepy.services.jobs.isslop.analysis.signals.hallucination import detect_hallucination def _context(text: str, aliases: frozenset = frozenset(), deps: frozenset = frozenset({"next", "react", "@clerk/nextjs"})): repo = RepoContext( root=Path("/tmp"), python_dependencies=frozenset(), javascript_dependencies=deps, local_python_modules=frozenset(), has_python_manifest=False, has_javascript_manifest=True, javascript_alias_prefixes=aliases, ) return FileContext( path=Path("/tmp/x.tsx"), relative="src/x.tsx", language="typescript", text=text, lines=text.splitlines(), comments=[], metrics=None, repo=repo, fingerprint_only=False, ) def test_path_aliases_are_never_unresolved(): text = ( "import { Sponsors } from '@/components/Sponsors';\n" "import config from '~/config';\n" "import db from '#app/db';\n" "import { page } from '$app/stores';\n" ) assert detect_hallucination(_context(text)) == [] def test_tsconfig_alias_prefixes_are_respected(): text = "import { helper } from 'src/utils/helper';\n" assert len(detect_hallucination(_context(text))) == 1 assert detect_hallucination(_context(text, aliases=frozenset({"src/"}))) == [] def test_declared_package_subpaths_resolve(): text = ( "import Image from 'next/image';\n" "import { auth } from '@clerk/nextjs/server';\n" "import { useState } from 'react';\n" ) assert detect_hallucination(_context(text)) == [] def test_node_builtins_and_schemes_resolve(): text = ( "import fs from 'node:fs';\n" "import hooks from 'async_hooks';\n" "import content from 'virtual:generated';\n" ) assert detect_hallucination(_context(text)) == [] def test_genuinely_missing_package_is_flagged(): findings = detect_hallucination(_context("import magic from 'left-pad-ultra';\n")) assert len(findings) == 1 assert findings[0].code == "DEP_UNRESOLVED" assert "left-pad-ultra" in findings[0].title def test_alias_prefix_parser_reads_jsonc(tmp_path): (tmp_path / "tsconfig.json").write_text( """{ // path aliases "compilerOptions": { "paths": { "@/*": ["./src/*"], "~lib/*": ["./lib/*"], /* legacy */ }, }, }""", encoding="utf-8", ) assert _javascript_alias_prefixes(tmp_path) == frozenset({"@/", "~lib/"})