/// <reference lib="dom" />
import * as Fs from "node:fs/promises";
import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("Windows drive and UNC metadata aliases retain case and separator folding in the running client", WEB, async ({t3,app,browser,screen})=>{
 const project=await t3.project("windows-proof",{Docs:"hello world"});
 await t3.open("/");
 await screen.getByRole("button","New thread in windows-proof",{exact:true}).first().waitFor();
 const thread=await t3.thread({project,title:"Windows metadata aliases",messages:["Check real Windows filesystem metadata and shared client cache aliases."]});
 await t3.open(thread);
 await screen.getByText("first fixture turn complete",{exact:true}).waitFor({timeout:60000});
 const observed=await browser.evaluate(async(project)=>{
   const load=(path:string)=>import(/* @vite-ignore */ path);
   const {projectEnvironment}=await load("/src/state/projects.ts");
   const {appAtomRegistry:registry}=await load("/src/rpc/atomRegistry.ts");
   const {primaryEnvironmentIdAtom}=await load("/src/state/primaryEnvironment.ts");
   const environmentId=registry.get(primaryEnvironmentIdAtom);
   const drive=[`${project}\\Docs`,`  ${project.toUpperCase().replaceAll('\\','/')}\\DOCS/  `];
   const unc=["\\\\LOCALHOST\\Proof\\Docs\\","  \\\\localhost/proof/docs/  "];
   const query=(path:string)=>projectEnvironment.fileMetadata({environmentId,input:{path}});
   const driveAtoms=drive.map(query); const uncAtoms=unc.map(query);
   const read=(atom:unknown)=>new Promise(resolve=>{
     const stop=registry.subscribe(atom,(result:{_tag:string;value?:unknown;waiting?:boolean})=>{
       if(result._tag==="Success"&&!result.waiting) { resolve(result.value); queueMicrotask(()=>stop()); }
     },{immediate:true});
   });
   return {drive,unc,driveAliasesShareAtom:driveAtoms[0]===driveAtoms[1],uncAliasesShareAtom:uncAtoms[0]===uncAtoms[1],driveMetadata:await read(driveAtoms[0]),uncMetadata:await read(uncAtoms[0])};
 },project);
 const text=["REAL WINDOWS APP / SHARED CLIENT RUNTIME", "", "Drive aliases: case, slashes, outer whitespace, trailing separator", "Both inputs share the same cache atom: "+observed.driveAliasesShareAtom, "Real Windows file metadata: "+JSON.stringify(observed.driveMetadata), "", "UNC input 1: "+JSON.stringify(observed.unc[0]), "UNC input 2: "+JSON.stringify(observed.unc[1]), "Both inputs share the same cache atom: "+observed.uncAliasesShareAtom, "UNC result: "+JSON.stringify(observed.uncMetadata), "Network metadata is intentionally refused by the real server."].join("\n");
 await Fs.writeFile(`${project}/proof.txt`,text+"\n");
 await screen.getByRole("button","Toggle terminal drawer").tap();
 const terminal=screen.getByLabel("Terminal input");
 await expect.poll(async()=>{await terminal.focus();await browser.keyboard.type("echo ready > shell-ready\n");return Fs.stat(`${project}/shell-ready`).then(()=>true).catch(()=>false);},{timeout:30000}).toBe(true);
 await terminal.focus();await browser.keyboard.type("function prompt { '$ ' }; Clear-Host; Get-Content proof.txt; Set-Content output-ready yes\n");
 await expect.poll(()=>Fs.stat(`${project}/output-ready`).then(()=>true).catch(()=>false)).toBe(true);
 await browser.evaluate(()=>{(document.activeElement as HTMLElement)?.blur();return null;});
 await app.screenshot("windows-drive-and-unc-runtime");
 await browser.evaluate(()=>new Promise(resolve=>setTimeout(()=>resolve(null),500)));
 await app.screenshot("windows-stable-confirmation");
 expect(observed.driveAliasesShareAtom).toBe(true);
 expect(observed.uncAliasesShareAtom).toBe(true);
 expect(observed.driveMetadata).toMatchObject({kind:"file",byteLength:11});
 expect(observed.uncMetadata).toBe(null);
});
