@@ -51,6 +51,8 @@ import {
5151 MacPackagedAppMissingError ,
5252 stageBrowserSecret ,
5353 validateLinuxPackagedPayload ,
54+ validateLinuxPackagedFiles ,
55+ LinuxPackagedPayloadValidationError ,
5456 validateMacPackagedPayload ,
5557 validateWindowsPackagedPayload ,
5658 verifyPackagedBundleIsSelfContained ,
@@ -74,6 +76,8 @@ import {
7476import * as HostProcess from "@t3tools/shared/HostProcess" ;
7577import { symlinksSupported } from "@t3tools/shared/testing/symlinks" ;
7678
79+ import gnomeCaptureBundle from "../apps/desktop/gnome-extension/bundle.json" with { type : "json" } ;
80+
7781const encodeModuleUrl = Schema . encodeEffect ( Schema . fromJsonString ( Schema . String ) ) ;
7882const decodePackageVersion = Schema . decodeUnknownEffect (
7983 Schema . fromJsonString ( Schema . Struct ( { version : Schema . String } ) ) ,
@@ -241,6 +245,90 @@ const makePackagedAppFixture = Effect.fn("test.makePackagedAppFixture")(function
241245 return { stageDistDir } as const ;
242246} ) ;
243247
248+ const linuxExecutables = [
249+ "t3code" ,
250+ "resources/resource-monitor/t3-resource-monitor" ,
251+ "resources/kde-capture/t3-kde-snap-shot" ,
252+ "resources/hyprland-capture/t3-hyprland-snap-shot" ,
253+ "resources/browser-secret/t3-browser-secret" ,
254+ ] ;
255+ const linuxAsarEntries = [
256+ "package.json" ,
257+ "apps/desktop/dist-electron/boot.cjs" ,
258+ "apps/desktop/dist-electron/main.cjs" ,
259+ "apps/desktop/dist-electron/preload.cjs" ,
260+ "apps/server/dist/bin.mjs" ,
261+ "apps/server/dist/client/index.html" ,
262+ ] ;
263+ const linuxNativeEntries = ( arch : "arm64" | "x64" ) => [
264+ "node_modules/node-pty/build/Release/pty.node" ,
265+ `node_modules/@napi-rs/keyring/keyring.linux-${ arch } -gnu.node` ,
266+ `node_modules/@ff-labs/fff-bin-linux-${ arch } -gnu/libfff_c.so` ,
267+ ] ;
268+ const linuxElfHeader = ( arch : "arm64" | "x64" ) => {
269+ const header = new Uint8Array ( 64 ) ;
270+ header . set ( [ 0x7f , 0x45 , 0x4c , 0x46 , 2 , 1 , 1 ] ) ;
271+ header [ 18 ] = arch === "arm64" ? 183 : 62 ;
272+ return header ;
273+ } ;
274+ const makeLinuxPayloadFixture = Effect . fn ( "test.makeLinuxPayloadFixture" ) ( function * ( input : {
275+ readonly arch : "arm64" | "x64" ;
276+ readonly omitEntry ?: string ;
277+ readonly serverEntrySource ?: string ;
278+ readonly emptyEntry ?: string ;
279+ readonly unpackNatives ?: boolean ;
280+ } ) {
281+ const fs = yield * FileSystem . FileSystem ;
282+ const path = yield * Path . Path ;
283+ const root = yield * fs . makeTempDirectoryScoped ( { prefix : "t3-linux-payload-test-" } ) ;
284+ const packagedAppDir = path . join (
285+ root ,
286+ input . arch === "x64" ? "linux-unpacked" : "linux-arm64-unpacked" ,
287+ ) ;
288+ const source = path . join ( root , "source" ) ;
289+ for ( const entry of [
290+ ...linuxAsarEntries ,
291+ ...linuxNativeEntries ( input . arch ) ,
292+ "node_modules/extra/addon.node" ,
293+ ] ) {
294+ if ( entry === input . omitEntry ) continue ;
295+ const filePath = path . join ( source , entry ) ;
296+ yield * fs . makeDirectory ( path . dirname ( filePath ) , { recursive : true } ) ;
297+ yield * fs . writeFile (
298+ filePath ,
299+ entry === input . emptyEntry
300+ ? new Uint8Array ( )
301+ : entry . startsWith ( "node_modules/" )
302+ ? linuxElfHeader ( input . arch )
303+ : new TextEncoder ( ) . encode (
304+ entry === "apps/server/dist/bin.mjs"
305+ ? ( input . serverEntrySource ?? "console.log('1.2.3');" )
306+ : "{}" ,
307+ ) ,
308+ ) ;
309+ }
310+ yield * fs . makeDirectory ( path . join ( packagedAppDir , "resources" ) , { recursive : true } ) ;
311+ yield * Effect . promise ( ( ) =>
312+ createPackageWithOptions (
313+ source ,
314+ path . join ( packagedAppDir , "resources/app.asar" ) ,
315+ input . unpackNatives === false ? { } : { unpack : "{**/*.node,**/*.so}" } ,
316+ ) ,
317+ ) ;
318+ for ( const entry of linuxExecutables ) {
319+ const filePath = path . join ( packagedAppDir , entry ) ;
320+ yield * fs . makeDirectory ( path . dirname ( filePath ) , { recursive : true } ) ;
321+ yield * fs . writeFile ( filePath , linuxElfHeader ( input . arch ) ) ;
322+ yield * fs . chmod ( filePath , 0o755 ) ;
323+ }
324+ for ( const entry of gnomeCaptureBundle . files ) {
325+ const filePath = path . join ( packagedAppDir , "resources/gnome-extension" , entry ) ;
326+ yield * fs . makeDirectory ( path . dirname ( filePath ) , { recursive : true } ) ;
327+ yield * fs . writeFileString ( filePath , "{}" ) ;
328+ }
329+ return { stageDistDir : root , packagedAppDir } ;
330+ } ) ;
331+
244332it . layer ( NodeServices . layer ) ( "build-desktop-artifact" , ( it ) => {
245333 it . effect ( "rejects overlapping builds from another process until restoration finishes" , ( ) =>
246334 Effect . gen ( function * ( ) {
@@ -1435,14 +1523,131 @@ it.layer(NodeServices.layer)("build-desktop-artifact", (it) => {
14351523 ) ,
14361524 ) ;
14371525
1526+ it . effect ( "validates static Linux payloads cross-host for both release architectures" , ( ) =>
1527+ Effect . scoped (
1528+ Effect . gen ( function * ( ) {
1529+ for ( const arch of [ "arm64" , "x64" ] as const ) {
1530+ const fixture = yield * makeLinuxPayloadFixture ( {
1531+ arch,
1532+ serverEntrySource : 'import "t3code-deliberately-missing-package";' ,
1533+ } ) ;
1534+ const fs = yield * FileSystem . FileSystem ;
1535+ const path = yield * Path . Path ;
1536+ yield * fs . copy (
1537+ fixture . packagedAppDir ,
1538+ path . join ( fixture . stageDistDir , "linux-extra-unpacked" ) ,
1539+ ) ;
1540+ yield * validateLinuxPackagedFiles ( {
1541+ stageDistDir : fixture . stageDistDir ,
1542+ targetArch : arch ,
1543+ } ) ;
1544+ }
1545+ } ) ,
1546+ ) . pipe ( Effect . provideService ( HostProcess . Platform , "darwin" ) ) ,
1547+ ) ;
1548+
1549+ it . effect ( "rejects incomplete Linux payloads and invalid ELF files" , ( ) =>
1550+ Effect . scoped (
1551+ Effect . gen ( function * ( ) {
1552+ const fs = yield * FileSystem . FileSystem ;
1553+ const path = yield * Path . Path ;
1554+ const fixture = yield * makeLinuxPayloadFixture ( { arch : "arm64" } ) ;
1555+ const expectRejected = Effect . fn ( "test.expectLinuxPayloadRejected" ) ( function * (
1556+ file : string ,
1557+ ) {
1558+ const error = yield * validateLinuxPackagedFiles ( {
1559+ stageDistDir : fixture . stageDistDir ,
1560+ targetArch : "arm64" ,
1561+ } ) . pipe ( Effect . flip ) ;
1562+ assert . instanceOf ( error , LinuxPackagedPayloadValidationError ) ;
1563+ assert . include ( error . filePath , file ) ;
1564+ } ) ;
1565+
1566+ for ( const file of [
1567+ "resources/app.asar" ,
1568+ "resources/app.asar.unpacked/node_modules/extra/addon.node" ,
1569+ "resources/browser-secret/t3-browser-secret" ,
1570+ "resources/gnome-extension/extension.js" ,
1571+ ] ) {
1572+ const filePath = path . join ( fixture . packagedAppDir , file ) ;
1573+ yield * fs . rename ( filePath , `${ filePath } .saved` ) ;
1574+ yield * expectRejected ( file ) ;
1575+ yield * fs . rename ( `${ filePath } .saved` , filePath ) ;
1576+ }
1577+
1578+ const nativePath = path . join (
1579+ fixture . packagedAppDir ,
1580+ "resources/app.asar.unpacked/node_modules/node-pty/build/Release/pty.node" ,
1581+ ) ;
1582+ for ( const bytes of [
1583+ new Uint8Array ( ) ,
1584+ new Uint8Array ( 64 ) ,
1585+ linuxElfHeader ( "arm64" ) . with ( 4 , 1 ) ,
1586+ linuxElfHeader ( "arm64" ) . with ( 5 , 2 ) ,
1587+ linuxElfHeader ( "x64" ) ,
1588+ ] ) {
1589+ yield * fs . writeFile ( nativePath , bytes ) ;
1590+ yield * expectRejected ( nativePath ) ;
1591+ }
1592+ yield * fs . writeFile ( nativePath , linuxElfHeader ( "arm64" ) ) ;
1593+
1594+ const helperPath = path . join ( fixture . packagedAppDir , "t3code" ) ;
1595+ yield * fs . chmod ( helperPath , 0o644 ) ;
1596+ yield * expectRejected ( helperPath ) ;
1597+ yield * fs . chmod ( helperPath , 0o755 ) ;
1598+ yield * fs . writeFile ( helperPath , linuxElfHeader ( "x64" ) ) ;
1599+ yield * expectRejected ( helperPath ) ;
1600+ yield * fs . writeFile ( helperPath , linuxElfHeader ( "arm64" ) ) ;
1601+
1602+ const extraAppDir = path . join ( fixture . stageDistDir , "linux-extra-unpacked" ) ;
1603+ yield * fs . copy ( fixture . packagedAppDir , extraAppDir ) ;
1604+ const extraHelper = path . join ( extraAppDir , "resources/browser-secret/t3-browser-secret" ) ;
1605+ yield * fs . remove ( extraHelper ) ;
1606+ yield * expectRejected ( extraHelper ) ;
1607+
1608+ for ( const fixtureInput of [
1609+ { omitEntry : "apps/server/dist/bin.mjs" } ,
1610+ { emptyEntry : "apps/desktop/dist-electron/main.cjs" } ,
1611+ { unpackNatives : false } ,
1612+ ] ) {
1613+ const invalid = yield * makeLinuxPayloadFixture ( { arch : "arm64" , ...fixtureInput } ) ;
1614+ const error = yield * validateLinuxPackagedFiles ( {
1615+ stageDistDir : invalid . stageDistDir ,
1616+ targetArch : "arm64" ,
1617+ } ) . pipe ( Effect . flip ) ;
1618+ assert . instanceOf ( error , LinuxPackagedPayloadValidationError ) ;
1619+ assert . include (
1620+ error . filePath ,
1621+ fixtureInput . omitEntry ?? fixtureInput . emptyEntry ?? "pty.node" ,
1622+ ) ;
1623+ }
1624+ const emptyStage = yield * fs . makeTempDirectoryScoped ( { prefix : "t3-linux-empty-" } ) ;
1625+ const missing = yield * validateLinuxPackagedFiles ( {
1626+ stageDistDir : emptyStage ,
1627+ targetArch : "arm64" ,
1628+ } ) . pipe ( Effect . flip ) ;
1629+ assert . instanceOf ( missing , LinuxPackagedAppMissingError ) ;
1630+ } ) ,
1631+ ) ,
1632+ ) ;
1633+
14381634 it . effect ( "rejects a Linux app whose packaged server bundle cannot resolve" , ( ) =>
14391635 Effect . scoped (
14401636 Effect . gen ( function * ( ) {
14411637 const fs = yield * FileSystem . FileSystem ;
1638+ const path = yield * Path . Path ;
14421639 const fixture = yield * makePackagedAppFixture ( {
14431640 resourcesPath : "linux-arm64-unpacked/resources" ,
1641+ } ) ;
1642+ yield * validateLinuxPackagedPayload ( { stageDistDir : fixture . stageDistDir } ) ;
1643+ const broken = yield * makePackagedAppFixture ( {
1644+ resourcesPath : "linux-unpacked/resources" ,
14441645 serverEntrySource : 'import "t3code-deliberately-missing-package";\n' ,
14451646 } ) ;
1647+ yield * fs . copy (
1648+ path . join ( broken . stageDistDir , "linux-unpacked" ) ,
1649+ path . join ( fixture . stageDistDir , "linux-unpacked" ) ,
1650+ ) ;
14461651 const error = yield * validateLinuxPackagedPayload ( {
14471652 stageDistDir : fixture . stageDistDir ,
14481653 } ) . pipe ( Effect . flip ) ;
@@ -1454,6 +1659,11 @@ it.layer(NodeServices.layer)("build-desktop-artifact", (it) => {
14541659 Effect . flip ,
14551660 ) ;
14561661 assert . instanceOf ( missing , LinuxPackagedAppMissingError ) ;
1662+ yield * fs . makeDirectory ( path . join ( emptyStage , "linux-unpacked" ) ) ;
1663+ const missingArchive = yield * validateLinuxPackagedPayload ( {
1664+ stageDistDir : emptyStage ,
1665+ } ) . pipe ( Effect . flip ) ;
1666+ assert . instanceOf ( missingArchive , BundleNotSelfContainedError ) ;
14571667 } ) ,
14581668 ) ,
14591669 ) ;
0 commit comments