2026-07-26 15:36:14 +08:00
"use strict" ;
const test = require ( "node:test" ) ;
const assert = require ( "node:assert/strict" ) ;
const fs = require ( "node:fs" ) ;
const os = require ( "node:os" ) ;
const path = require ( "node:path" ) ;
2026-07-29 23:37:26 +08:00
const { receiveBundle , resolveRepository } = require ( "./repo-push-broker" ) ;
test ( "repository resolution is exact and returns an explicit unregistered receipt" , ( ) => {
const registry = {
"bingshuo/hololake-platform" : {
branches : [ "main" , "feat/hldp-runtime-browser" ] ,
remote : "file:///srv/forgejo/hololake-platform.git" ,
source _urls : [
"https://guanghulab.com/fifth-domain/bingshuo/hololake-platform.git" ,
] ,
} ,
} ;
assert . deepEqual (
resolveRepository (
"https://guanghulab.com/fifth-domain/bingshuo/hololake-platform.git" ,
registry ,
) ,
{
ok : true ,
diagnostic _code : "repository_registered" ,
repo : "bingshuo/hololake-platform" ,
branches : [ "main" , "feat/hldp-runtime-browser" ] ,
} ,
) ;
assert . deepEqual ( resolveRepository ( "" , registry ) , {
ok : false ,
diagnostic _code : "local_repository_remote_missing" ,
} ) ;
assert . deepEqual (
resolveRepository ( "https://example.invalid/unknown.git" , registry ) ,
{
ok : false ,
diagnostic _code : "repository_not_registered" ,
} ,
) ;
} ) ;
2026-07-26 15:36:14 +08:00
test ( "receiver permits only an allowlisted fast-forward bundle with an exact base" , async ( ) => {
const uploadDir = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "lake-lamp-upload-" ) ) ;
const bundle = path . join ( uploadDir , "one.bundle" ) ; fs . writeFileSync ( bundle , "bundle" ) ;
const calls = [ ] ;
const base = "a" . repeat ( 40 ) , incoming = "b" . repeat ( 40 ) ;
const run = async args => { calls . push ( args ) ; if ( args . includes ( "rev-parse" ) ) return { ok : true , stdout : ` ${ incoming } \n ` } ; if ( args [ 0 ] === "ls-remote" ) return { ok : true , stdout : ` ${ base } \t refs/heads/main \n ` } ; return { ok : true , stdout : "" } ; } ;
try {
2026-07-29 23:37:26 +08:00
const result = await receiveBundle ( { repo : "bingshuo/guanghu-ice-heart" , branch : "main" , expected _head : base , bundle _path : bundle } , { uploadDir , registry : { "bingshuo/guanghu-ice-heart" : { branch : "main" , remote : "/srv/local/code.git" , verification _url : "https://example.invalid/commits/main" } } , run } ) ;
2026-07-26 15:36:14 +08:00
assert . equal ( result . ok , true ) ; assert . equal ( result . commit _sha , incoming ) ;
2026-07-29 23:37:26 +08:00
assert . ok ( calls . some ( args => args . includes ( "update-ref" ) ) ) ;
2026-07-26 15:36:14 +08:00
} finally { fs . rmSync ( uploadDir , { recursive : true , force : true } ) ; }
} ) ;
test ( "receiver refuses a changed remote base before it can push" , async ( ) => {
const uploadDir = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "lake-lamp-upload-" ) ) ;
const bundle = path . join ( uploadDir , "one.bundle" ) ; fs . writeFileSync ( bundle , "bundle" ) ;
const base = "a" . repeat ( 40 ) , changed = "c" . repeat ( 40 ) ;
2026-07-29 23:37:26 +08:00
const run = async args => { if ( args . includes ( "rev-parse" ) ) return { ok : true , stdout : ` ${ "b" . repeat ( 40 ) } \n ` } ; if ( args [ 0 ] === "ls-remote" ) return { ok : true , stdout : ` ${ changed } \t refs/heads/main \n ` } ; if ( args . includes ( "update-ref" ) ) throw new Error ( "must not update" ) ; return { ok : true , stdout : "" } ; } ;
2026-07-26 15:36:14 +08:00
try {
2026-07-29 23:37:26 +08:00
const result = await receiveBundle ( { repo : "bingshuo/guanghu-ice-heart" , branch : "main" , expected _head : base , bundle _path : bundle } , { uploadDir , registry : { "bingshuo/guanghu-ice-heart" : { branch : "main" , remote : "/srv/local/code.git" } } , run } ) ;
2026-07-26 15:36:14 +08:00
assert . equal ( result . diagnostic _code , "expected_head_mismatch" ) ;
} finally { fs . rmSync ( uploadDir , { recursive : true , force : true } ) ; }
} ) ;
2026-07-29 23:37:26 +08:00
test ( "receiver can create only an explicitly allowlisted branch from an exact missing base" , async ( ) => {
const uploadDir = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "lake-lamp-upload-" ) ) ;
const bundle = path . join ( uploadDir , "one.bundle" ) ; fs . writeFileSync ( bundle , "bundle" ) ;
const incoming = "b" . repeat ( 40 ) ;
const calls = [ ] ;
const run = async ( args , options = { } ) => {
calls . push ( args ) ;
if ( args . includes ( "rev-parse" ) ) return { ok : true , stdout : ` ${ incoming } \n ` } ;
if ( args [ 0 ] === "ls-remote" ) return { ok : options . allowFailure === true , stdout : "" } ;
return { ok : true , stdout : "" } ;
} ;
try {
const result = await receiveBundle ( {
repo : "bingshuo/hololake-platform" ,
branch : "feat/hldp-runtime-browser" ,
expected _head : "0" . repeat ( 40 ) ,
bundle _path : bundle ,
} , {
uploadDir ,
registry : {
"bingshuo/hololake-platform" : {
branches : [ "main" , "feat/hldp-runtime-browser" ] ,
remote : "/srv/local/code.git" ,
} ,
} ,
run ,
} ) ;
assert . equal ( result . ok , true ) ;
assert . equal ( result . commit _sha , incoming ) ;
assert . ok ( calls . some ( args => args . includes ( "update-ref" ) ) ) ;
} finally {
fs . rmSync ( uploadDir , { recursive : true , force : true } ) ;
}
} ) ;